Created
April 25, 2017 03:39
-
-
Save kkuivi/905745ce8bc69f024987454edd367e80 to your computer and use it in GitHub Desktop.
CodeFights: areSimilar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean areSimilar(int[] A, int[] B) { | |
ArrayList<Position> mismatchedPosition = new ArrayList<>(); | |
for(int i = 0; i < A.length; i++){ | |
int a = A[i]; | |
int b = B[i]; | |
if(a != b){ | |
Position p = new Position(i, a, b); | |
mismatchedPosition.add(p); | |
} | |
} | |
if(mismatchedPosition.size() == 0) | |
return true; | |
else if(mismatchedPosition.size() == 2){ | |
Position pos1 = mismatchedPosition.get(0); | |
Position pos2 = mismatchedPosition.get(1); | |
int aPos1 = pos1.aElement; | |
int bPos1 = pos1.bElement; | |
int aPos2 = pos2.aElement; | |
int bPos2 = pos2.bElement; | |
if(aPos1 == bPos2 && bPos1 == aPos2) | |
return true; | |
else | |
return false; | |
} | |
else | |
return false; | |
} | |
class Position{ | |
int position; | |
int aElement; | |
int bElement; | |
Position(int pos, int a, int b) { | |
position = pos; | |
aElement = a; | |
bElement = b; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment