9/20 每日一題(找出生僻字)
A sentence is a string of single-space separated words where 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.
Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.
Example 1:
Input: s1 = "this apple is sweet", s2 = "this apple is sour" Output: ["sweet","sour"]
Example 2:
Input: s1 = "apple apple", s2 = "banana" Output: ["banana"]
Constraints:
1 <= s1.length, s2.length <= 200s1ands2consist of lowercase English letters and spaces.s1ands2do not have leading or trailing spaces.- All the words in
s1ands2are separated by a single space.
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
s_list=s1.split()
s_list.extend(s2.split())
print(s_list)
s_dict=Counter(s_list)
print(s_dict)
res=[]
for k,v in s_dict.items():
if v==1:
res.append(k)
print(res)
return res
from collections import Counter
class Solution:
def getList(self, s: str) -> (List[str], List[str]):
res = Counter(s)
tem1 = set()
tem2 = set()
for i in res:
if res[i] == 1:
tem1.add(i)
else:
tem2.add(i)
return (tem1, tem2)
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
if not s1: return s2
if not s2: return s1
l1, r1 = self.getList(s1.split(" "))
l2, r2 = self.getList(s2.split(" "))
return (l1^l2) - r1 - r2 ----------------------------------------
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
l1=s1.split()
l2=s2.split()
l3=[]
for i in range(0,len(l1)):
if l1[i] not in l2:
if l1.count(l1[i])==1:
l3.append(l1[i])
for i in range(0,len(l2)):
if l2[i] not in l1:
if l2.count(l2[i])==1:
l3.append(l2[i])
return l3
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁