884. Uncommon Words from Two Sentences (easy)
We are given two sentences
AandB. (A sentence is a string of space separated words. Each word consists only of lowercase letters.)A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Return a list of all uncommon words.
You may return the list in any order.
Example 1:
Input: A = \"this apple is sweet\", B = \"this apple is sour\" Output: [\"sweet\",\"sour\"]Example 2:
Input: A = \"apple apple\", B = \"banana\" Output: [\"banana\"]
class Solution:
def uncommonFromSentences(self, A, B):
\"\"\"
:type A: str
:type B: str
:rtype: List[str]
\"\"\"
uncommon = []
AList = A.split()
BList = B.split()
Acount = collections.Counter(AList)
Bcount = collections.Counter(BList)
Aset = {key for key,count in Acount.items() if count == 1}
Bset = {key for key,count in Bcount.items() if count == 1}
ABset = set(AList) & set(BList)
return list({x for x in Aset^Bset if x not in ABset})
Runtime: 36 ms, faster than 98.82% of Python3
继续阅读与本文标签相同的文章
下一篇 :
Google 或近期恢复对华为的 GMS 认证
-
如何成为一名优秀的初级开发者?
2026-05-18栏目: 教程
-
展望2025多媒体技术与应用趋势
2026-05-18栏目: 教程
-
“拼下限”的网络直播
2026-05-18栏目: 教程
-
阿里云第六代云服务器特性、实例类型、及可选区域相关介绍
2026-05-18栏目: 教程
-
刚刚,3位外籍院士签约落地中关村!创下一个国内之最
2026-05-18栏目: 教程
