Count Common Words With One Occurrence LeetCode solution in Java
1class Solution {2public int countWords(String[] words1, String[] words2) {3HashMap<String, Integer> map1 = new HashMap<>();4HashMap<String, Integer> map2 = new HashMap<>();56for (String word : words1) map1.put(word, map1.getOrDefault(word, 0) + 1);7for (String word : words2) map2.put(word, map2.getOrDefault(word, 0) + 1);89int count = 0;10for (String word : words1) if (map1.get(word) == 1 && map2.getOrDefault(word, 0) == 1) count++;11return count;12}13}