Count Common Words With One Occurrence LeetCode solution in Java

1
class Solution {
2
public int countWords(String[] words1, String[] words2) {
3
HashMap<String, Integer> map1 = new HashMap<>();
4
HashMap<String, Integer> map2 = new HashMap<>();
5

6
for (String word : words1) map1.put(word, map1.getOrDefault(word, 0) + 1);
7
for (String word : words2) map2.put(word, map2.getOrDefault(word, 0) + 1);
8

9
int count = 0;
10
for (String word : words1) if (map1.get(word) == 1 && map2.getOrDefault(word, 0) == 1) count++;
11
return count;
12
}
13
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0