Skip to content

Commit befa1d3

Browse files
authored
Create check-whether-two-strings-are-almost-equivalent.cpp
1 parent 3314bae commit befa1d3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool checkAlmostEquivalent(string word1, string word2) {
7+
static const int K = 3;
8+
9+
unordered_map<char, int> cnt1;
10+
for (const auto& c : word1) {
11+
++cnt1[c];
12+
}
13+
unordered_map<char, int> cnt2;
14+
for (const auto& c : word2) {
15+
++cnt2[c];
16+
}
17+
for (const auto& [k, v] : cnt1) {
18+
if (abs(cnt2[k] - v) > K) {
19+
return false;
20+
}
21+
}
22+
for (const auto& [k, v] : cnt2) {
23+
if (abs(cnt1[k] - v) > K) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
};

0 commit comments

Comments
 (0)