forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
62 lines (50 loc) · 1.47 KB
/
main2.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
/// Source : https://leetcode.com/problems/buddy-strings/description/
/// Author : liuyubobobo
/// Time : 2018-06-25
#include <iostream>
using namespace std;
/// Scan and Compare
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool buddyStrings(string A, string B) {
if(A.size() != B.size())
return false;
if(A == B)
return atLeastTwoSame(A);
int first = -1, second = -1;
for(int i = 0 ; i < A.size(); i++)
if(A[i] != B[i]){
if(first == -1)
first = i;
else if(second == -1)
second = i;
else
return false;
}
return A[first] == B[second] && A[second] == B[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;
}