-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path859.cpp
24 lines (23 loc) · 808 Bytes
/
859.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
class Solution {
public:
bool buddyStrings(string s, string goal) {
vector<int> cnt(26, 0);
int maxCnt = 0;
vector<int> difference;
if (s.size() != goal.size()) return false;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (s[i] != goal[i]) difference.push_back(i);
cnt[s[i] - 'a']++;
maxCnt = max(maxCnt, cnt[s[i] - 'a']);
}
if (difference.size() == 1 || difference.size() > 2) return false;
if (difference.size() == 0) {
if (maxCnt >= 2) return true;
else return false;
}
// difference.size() == 2
if (s[difference[0]] == goal[difference[1]] && s[difference[1]] == goal[difference[0]]) return true;
else return false;
}
};