-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path884.UncommonWordsfromTwoSentences.py
40 lines (32 loc) · 1.17 KB
/
884.UncommonWordsfromTwoSentences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
We are given two sentences A and B. (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:
Input: A = "this apple is sweet", B = "this apple is sour"
Output: ["sweet","sour"]
Example:
Input: A = "apple apple", B = "banana"
Output: ["banana"]
Note:
1. 0 <= A.length <= 200
2. 0 <= B.length <= 200
3. A and B both contain only spaces and lowercase
letters.
'''
#Difficulty: Easy
#53 / 53 test cases passed.
#Runtime: 20 ms
#Memory Usage: 14.1 MB
#Runtime: 20 ms, faster than 98.73% of Python3 online submissions for Uncommon Words from Two Sentences.
#Memory Usage: 14.1 MB, less than 91.89% of Python3 online submissions for Uncommon Words from Two Sentences.
class Solution:
def uncommonFromSentences(self, A: str, B: str) -> List[str]:
B = f'{A} {B}'.split(' ')
A = set(B)
return [a for a in A if B.count(a) == 1]