forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
67 lines (53 loc) · 1.63 KB
/
main.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// Source : https://leetcode.com/problems/buddy-strings/description/
/// Author : liuyubobobo
/// Time : 2018-06-23
#include <iostream>
#include <unordered_map>
using namespace std;
/// Scan and Compare
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class Solution {
public:
bool buddyStrings(string A, string B) {
if(A.size() != B.size())
return false;
unordered_map<char, char> diff;
int diff_num = 0;
for(int i = 0 ; i < A.size() ; i ++)
if(A[i] != B[i]){
diff[A[i]] = B[i];
diff_num ++;
}
if(diff_num == 0)
return atLeastTwoSame(A);
if(diff_num != 2)
return false;
unordered_map<char, char>::iterator iter1 = diff.begin();
unordered_map<char, char>::iterator iter2 = diff.begin();
iter2 ++;
return iter1->first == iter2->second && iter1->second == iter2->first;
}
private:
bool atLeastTwoSame(const string& s){
int freq[26];
memset(freq, 0, sizeof(freq));
for(char c: s){
freq[c - 'a'] ++;
if(freq[c - 'a'] >= 2)
return true;
}
return false;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
print_bool(Solution().buddyStrings("ab", "ba")); // true
print_bool(Solution().buddyStrings("ab", "ab")); // false
print_bool(Solution().buddyStrings("ab", "ba")); // true
print_bool(Solution().buddyStrings("aaaaaaabc", "aaaaaaacb")); // true
print_bool(Solution().buddyStrings("", "aa")); // false
return 0;
}