Wednesday, May 31, 2017

are Simmilar

https://codefights.com/arcade/intro/level-4/xYXfzQmnhBvEKJwXP

my solution is  below

what i focus on is when difference is 0 or 2


public static boolean areSimilar(int[] a, int[] b) {

  int difference= 0; 
  int [] p = new int[2];
  int pos = 0;
  
  for (int i = 0; i < a.length; i++) {  
   
   if(a[i] != b[i]){
    if(difference==2){
     return false;
    }    
    p[pos++] = i;
    difference++;
   }
  }
  
  return difference ==0 || (difference ==2 && (a[p[0]] == b[p[1]] && a[p[1]] == b[p[0]]));
 }

No comments:

Post a Comment