File tree Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public final int UNSET_INDEX = -1 ;
3
- public final int SET_INDEX = 101 ;
4
2
public boolean areAlmostEqual (String s1 , String s2 ) {
5
- int swapIndex = UNSET_INDEX ;
3
+ int mismatchIdx = - 1 ;
6
4
for (int i = 0 ; i < s1 .length (); i ++) {
7
5
if (s1 .charAt (i ) != s2 .charAt (i )) {
8
- if (swapIndex == SET_INDEX ) {
9
- return false ;
10
- } else if (swapIndex == UNSET_INDEX ) {
11
- swapIndex = i ;
12
- } else {
13
- if (!(s1 .charAt (swapIndex ) == s2 .charAt (i ) && s1 .charAt (i ) == s2 .charAt (swapIndex ))) {
14
- return false ;
15
- }
16
- swapIndex = SET_INDEX ;
6
+ if (mismatchIdx != -1 ) {
7
+ return s1 .charAt (mismatchIdx ) == s2 .charAt (i ) &&
8
+ s1 .charAt (i ) == s2 .charAt (mismatchIdx ) &&
9
+ s1 .substring (i + 1 ).equals (s2 .substring (i + 1 ));
17
10
}
11
+ mismatchIdx = i ;
18
12
}
19
13
}
20
- return swapIndex == UNSET_INDEX || swapIndex == SET_INDEX ;
14
+ return mismatchIdx == - 1 ;
21
15
}
22
16
}
You can’t perform that action at this time.
0 commit comments