Solution to LeetCode 884 Uncommon Words from Two Sentences.
LeetCode 884
Uncommon Words from Two Sentences (Medium). [link]
Time complexity O(|S1|+|S2|). Space complexity O(|S1|+|S2|).
class Solution(object):
def uncommonFromSentences(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: List[str]
"""
frequency = Counter(s1.split()) + Counter(s2.split())
ans = []
for word, count in frequency.items():
if count == 1:
ans.append(word)
return ans