-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc1790.cpp
More file actions
55 lines (47 loc) · 1.37 KB
/
lc1790.cpp
File metadata and controls
55 lines (47 loc) · 1.37 KB
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
#include <string>
#include <unordered_map>
#include <vector>
#include <iostream>
class Solution {
public:
bool areAlmostEqual(std::string s1, std::string s2) {
std::unordered_map<char,int> s1_map, s2_map;
int diff = 0;
for (int i = 0; i < s1.size(); i++) {
s1_map[s1[i]]++;
s2_map[s2[i]]++;
if (s1[i] != s2[i]) {
diff++; // Count the number of different characters
if (diff > 2)
return false;
}
}
// Check if the two strings have the same characters
for(int i = 0; i < s1.size(); i++) {
if (s2_map[s1[i]] != s1_map[s1[i]]) {
return false;
}
}
return true;
}
};
int main() {
Solution s;
std::vector<std::pair<std::string, std::string>> testCases = {
{"bank", "kanb"},
{"attack", "defend"},
{"kelb", "kelb"},
{"abcd", "dcba"},
{"abcd", "abdc"},
{"aabbcc", "ccbbaa"},
{"abc", "acb"},
{"abc", "abc"},
{"a", "a"},
{"", ""}
};
for (const auto& testCase : testCases) {
std::cout << "s1: " << testCase.first << ", s2: " << testCase.second << " -> "
<< (s.areAlmostEqual(testCase.first, testCase.second) ? "true" : "false") << std::endl;
}
return 0;
}