Check Whether Two Strings are Almost Equivalent LeetCode solution in Java

1
class Solution {
2
public boolean checkAlmostEquivalent(String word1, String word2) {
3
Map<Character, Integer> map = new HashMap();
4
for (int i = 0; i < word1.length(); i++) {
5
map.put(word1.charAt(i), map.getOrDefault(word1.charAt(i), 0) + 1);
6
map.put(word2.charAt(i), map.getOrDefault(word2.charAt(i), 0) - 1);
7
}
8
for (int i : map.values()) { // get value set
9
if (i > 3 || i < -3) {
10
return false;
11
}
12
}
13
return true;
14
}
15
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0