编程语言
首页 > 编程语言> > Leetcode 884. 两句话中的不常见单词 (Python Count和split的使用)

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

作者:互联网

可以将两个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

 

标签:Count,884,Python,s2,occ,split,str,ans
来源: https://blog.csdn.net/wwxy1995/article/details/122754061