Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://leetcode.com/problems/uncommon-words-from-two-sentences/
- class Solution:
- def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
- s1_list = s1.split()
- s2_list = s2.split()
- words_dict = {}
- uncommon_list = []
- only_in_s1 = []
- only_in_s2 = []
- for x1 in s1_list:
- if x1 not in s2_list:
- only_in_s1.append(x1)
- else:
- pass
- for x2 in s2_list:
- if x2 not in s1_list:
- only_in_s2.append(x2)
- else:
- pass
- for word in only_in_s1:
- if word in words_dict:
- words_dict[word] +=1
- else:
- words_dict[word] = 1
- for word in only_in_s2:
- if word in words_dict:
- words_dict[word]+=1
- else:
- words_dict[word] = 1
- for uncommon in words_dict:
- if words_dict.get(uncommon) == 1:
- uncommon_list.append(uncommon)
- else:
- pass
- return uncommon_list
Advertisement
Add Comment
Please, Sign In to add comment