Skip to content

Commit d5dfea0

Browse files
committed
Time: 3 ms (36.18%), Space: 42.3 MB (11%) - LeetHub
1 parent 6456cec commit d5dfea0

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+
class Solution {
2+
public boolean checkAlmostEquivalent(String word1, String word2) {
3+
Map<Character, Integer> map1 = new HashMap<>();
4+
Map<Character, Integer> map2 = new HashMap<>();
5+
for (int i = 0; i < word1.length(); i++) {
6+
map1.put(word1.charAt(i), map1.getOrDefault(word1.charAt(i), 0) + 1);
7+
}
8+
for (int i = 0; i < word2.length(); i++) {
9+
if (map1.containsKey(word2.charAt(i))) {
10+
map1.put(word2.charAt(i), map1.get(word2.charAt(i)) - 1);
11+
} else {
12+
map2.put(word2.charAt(i), map2.getOrDefault(word2.charAt(i), 0) + 1);
13+
}
14+
}
15+
for (Character ch: map1.keySet()) {
16+
Integer count = map1.get(ch);
17+
if (count < -3 || count > 3) {
18+
return false;
19+
}
20+
}
21+
for (Character ch: map2.keySet()) {
22+
Integer count = map2.get(ch);
23+
if (count > 3) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}

0 commit comments

Comments
 (0)