Count Common Words With One Occurrence

给两个字符串组, 找到其中出现一次的字符串的交集.

class Solution {
    public int countWords(String[] words1, String[] words2) {
        Map<String, Integer> count1 = new HashMap<>();
        Map<String, Integer> count2 = new HashMap<>();
        for(String w : words1)
            count1.put(w, count1.getOrDefault(w, 0) + 1);
        for(String w : words2)
            count2.put(w, count2.getOrDefault(w, 0) + 1);
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();
        for(String w : words1)
            if(count1.get(w) == 1)
                set1.add(w);
        for(String w : words2)
            if(count2.get(w) == 1)
                set2.add(w);
        set1.retainAll(set2);
        return set1.size();
    }
}