1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public int countWords(String[] words1, String[] words2) { Map<String, Integer> countMap1 = new HashMap<>(), countMap2 = new HashMap<>();
for (String word : words1) { countMap1.put(word, countMap1.getOrDefault(word, 0) + 1); } for (String word : words2) { countMap2.put(word, countMap2.getOrDefault(word, 0) + 1); }
int count = 0; for (Map.Entry<String, Integer> entry : countMap1.entrySet()) { if (entry.getValue() == 1 && countMap2.getOrDefault(entry.getKey(), 0) == 1) { count++; } }
return count; } }
|
References
2085. Count Common Words With One Occurrence