当前位置:网站首页>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
边栏推荐
- 每日一题-无重复字符的最长子串
- 剑指 Offer 06.从头到尾打印链表
- Use of room database
- Solution to game 10 of the personal field
- Solution to the palindrome string (Luogu p5041 haoi2009)
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Simple knapsack, queue and stack with deque
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- 卷积神经网络简介
- [practical skills] how to do a good job in technical training?
猜你喜欢
[cloud native] record of feign custom configuration of microservices
SAP-修改系统表数据的方法
Wazuh开源主机安全解决方案的简介与使用体验
2017 USP Try-outs C. Coprimes
API related to TCP connection
利用HashMap实现简单缓存
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
【Jailhouse 文章】Look Mum, no VM Exits
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Little known skills of Task Manager
随机推荐
Summary of Haut OJ 2021 freshman week
Csp-j-2020-excellent split multiple solutions
2017 USP Try-outs C. Coprimes
Fried chicken nuggets and fifa22
Add level control and logger level control of Solon logging plug-in
Reflection summary of Haut OJ freshmen on Wednesday
Sword finger offer 04 Search in two-dimensional array
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Over fitting and regularization
26、 File system API (device sharing between applications; directory and file API)
Personal developed penetration testing tool Satania v1.2 update
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
CF1634 F. Fibonacci Additions
Gbase database helps the development of digital finance in the Bay Area
Simply sort out the types of sockets
Control Unit 控制部件
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
【实战技能】非技术背景经理的技术管理
剑指 Offer 09. 用两个栈实现队列
Cluster script of data warehouse project