2085.统计出现过一次的公共字符串

原题

一、双map

class Solution:
    def countWords(self, words1: List[str], words2: List[str]) -> int:
        map1, map2 = {}, {}
        cnt = 0
        for word in words1:
            map1[word] = map1.get(word, 0) + 1
        for word in words2:
            map2[word] = map2.get(word, 0) + 1
        for word in  set(words1) & set(words2):
            if map1[word] == 1 and map2[word] == 1:
                cnt += 1
        return cnt  

最后更新于