-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
29 lines (29 loc) · 804 Bytes
/
soln.cpp
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
class Solution {
public:
bool buddyStrings(string A, string B) {
int cnt = 0, nA = A.length(), nB = B.length();
char a = ' ', b = ' ';
for(int i = 0; i < nA; ++i) {
if (A[i] != B[i]) {
if (cnt == 0) {
a = A[i];
b = B[i];
} else {
if (a != B[i] || b != A[i]) return false;
}
++cnt;
if (cnt > 2) return false;
}
}
if (cnt == 2) return true;
else if (cnt == 1) return false;
else {
int chars[256] = {0};
for(auto ch : A) {
++chars[ch];
if (chars[ch] > 1) return true;
}
return false;
}
}
};