当前位置:网站首页>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
边栏推荐
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Zzulioj 1673: b: clever characters???
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
- Codeforces Round #716 (Div. 2) D. Cut and Stick
- Introduction et expérience de wazuh open source host Security Solution
- PC register
- Sword finger offer 53 - I. find the number I in the sorted array
- Sword finger offer 05 Replace spaces
- 剑指 Offer 06.从头到尾打印链表
猜你喜欢
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

Sword finger offer 53 - I. find the number I in the sorted array

全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析

Introduction and experience of wazuh open source host security solution

Scope of inline symbol

API related to TCP connection

R语言【数据集的导入导出】

Web APIs DOM node

CF1634E Fair Share

Some common problems in the assessment of network engineers: WLAN, BGP, switch
随机推荐
Sword finger offer 35 Replication of complex linked list
智慧工地“水电能耗在线监测系统”
从Dijkstra的图灵奖演讲论科技创业者特点
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
全排列的代码 (递归写法)
利用HashMap实现简单缓存
2020ccpc Qinhuangdao J - Kingdom's power
[cloud native] record of feign custom configuration of microservices
Full Permutation Code (recursive writing)
kubeadm系列-00-overview
剑指 Offer 05. 替换空格
Add level control and logger level control of Solon logging plug-in
[jailhouse article] jailhouse hypervisor
Typical use cases for knapsacks, queues, and stacks
Add level control and logger level control of Solon logging plug-in
剑指 Offer 09. 用两个栈实现队列
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Pointnet++ learning
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
常见的最优化方法