0
点赞
收藏
分享

微信扫一扫

Leetcode 884. 两句话中的不常见单词 (Python Count和split的使用)

笙烛 2022-01-31 阅读 59

可以将两个cnt合并到一起,然后直接看出现次数为1的单词即可。

这里使用了Python的技巧,代码特别简单。

class Solution:
    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
        freq = Counter(s1.split()) + Counter(s2.split())
        
        ans = list()
        for word, occ in freq.items():
            if occ == 1:
                ans.append(word)
        
        return ans

 

举报

相关推荐

0 条评论