题目描述:
代码
class Solution {
public boolean buddyStrings(String A, String B) {
if(A.length() != B.length()){
return false;
}
int tems[] = new int[26];
int math = 0;
int s = 0;
for (int i = 0; i < A.length(); i++) {
tems[A.charAt(i) - 97]++;
if(A.charAt(i) != B.charAt(i)){
if(math == 1){
char tem[] = A.toCharArray();
tem[s] = A.charAt(i);
tem[i] = A.charAt(s);
if(new String(tem).equals(B)){
return true;
}else {
return false;
}
}else {
//记录下位置
s = i;
math ++;
}
}
}
for (int i = 0; i < tems.length; i++) {
if(tems[i] >= 2){
return true;
}
}
return false;
}
}