-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuddy Strings.cpp
More file actions
30 lines (28 loc) · 796 Bytes
/
Buddy Strings.cpp
File metadata and controls
30 lines (28 loc) · 796 Bytes
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
class Solution {
public:
bool buddyStrings(string A, string B) {
if(A.length() != B.length())
return false;
int i, n = A.length();
bool flag = false;
unordered_set<char> s;
vector<int> miss;
for(i = 0; i < n; i++)
{
if(A[i] != B[i])
miss.push_back(i);
else
{
if(s.find(A[i]) != s.end())
flag = true;
else
s.insert(A[i]);
}
}
if(miss.size() != 2 && miss.size() != 0)
return false;
if(miss.size() == 0)
return (flag) ? true : false;
return (A[miss[0]] == B[miss[1]] && A[miss[1]] == B[miss[0]]) ? true : false;
}
};