leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0859.cpp (676B)
0 class Solution {
1 public:
2 bool buddyStrings(const string &s, const string &goal) {
3 int a = -1, b = -1, dup = 0, count[26] = {0};
4 if (s.size() != goal.size()) return false;
5 for (int i = 0; i < s.size(); i++) {
6 if (count[s[i] & 0xF]) dup = 1;
7 count[s[i] & 0xF] = 1;
8 if (s[i] != goal[i]) {
9 if (a == -1)
10 a = i;
11 else if (b == -1)
12 b = i;
13 else
14 return false;
15 }
16 }
17 if (a == -1) return dup;
18 if (b == -1) return false;
19 return s[a] == goal[b] && s[b] == goal[a];
20 }
21 };