-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution859.java
35 lines (31 loc) · 967 Bytes
/
Solution859.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2018/12/25
*/
public class Solution859 {
public boolean buddyStrings(String A, String B) {
if (null == A || null == B || A.length() != B.length() || 0 == A.length()) {
return false;
}
int indexA = -1, indexB = -1, diff = 0;
int[] map = new int[26];
boolean duplicate = false;
for (int i = 0; i < A.length(); ++i) {
if (++map[A.charAt(i) - 'a'] >= 2) {
duplicate = true;
}
if (A.charAt(i) != B.charAt(i)) {
++diff;
if (-1 == indexA) {
indexA = i;
} else if (-1 == indexB) {
indexB = i;
}
}
}
return (0 == diff && duplicate) ||
(2 == diff && A.charAt(indexA) == B.charAt(indexB) && A.charAt(indexB) == B.charAt(indexA));
}
}