当前位置:网站首页>884. Uncommon words in two sentences
884. Uncommon words in two sentences
2022-07-05 05:42:00 【A big pigeon】
topic : Enter two sentences , Return to the list of uncommon words in two sentences .
“ Uncommon words ” It means to appear only once in a sentence , And another sentence did not appear .
Explain : Directly according to the meaning of the topic , Please appear only once in a sentence , And another word that doesn't appear . use Counter() Count times .
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
tokens1 = s1.split()
tokens2 = s2.split()
ans = []
cnt1 = Counter(tokens1)
cnt2 = Counter(tokens2)
#print(cnt1,cnt2)
for k, v in cnt1.items():
if v == 1 and k not in cnt2:
ans.append(k)
for k, v in cnt2.items():
if v == 1 and k not in cnt1:
ans.append(k)
return ans
To simplify the , Appear once in a sentence , Another sentence does not appear , That's the same thing as being in s1+s2 There is one occurrence in total .
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
s = s1+" "+s2
tokens = s.split()
ans = []
cnt = Counter(tokens)
for k, v in cnt.items():
if v == 1 :
ans.append(k)
return ans
边栏推荐
- Sword finger offer 06 Print linked list from beginning to end
- Sword finger offer 05 Replace spaces
- PC寄存器
- Talking about JVM (frequent interview)
- API related to TCP connection
- The number of enclaves
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- Simple knapsack, queue and stack with deque
- Acwing 4301. Truncated sequence
- Gbase database helps the development of digital finance in the Bay Area
猜你喜欢
随机推荐
软件测试 -- 0 序
Smart construction site "hydropower energy consumption online monitoring system"
Reader writer model
Over fitting and regularization
Talking about JVM (frequent interview)
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
API related to TCP connection
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Web APIs DOM node
Simply sort out the types of sockets
卷积神经网络——卷积层
Add level control and logger level control of Solon logging plug-in
Introduction to memory layout of FVP and Juno platforms
How can the Solon framework easily obtain the response time of each request?
【实战技能】非技术背景经理的技术管理
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
F - Two Exam(AtCoder Beginner Contest 238)
CF1634 F. Fibonacci Additions
Convolution neural network -- convolution layer
In this indifferent world, light crying