当前位置:网站首页>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
边栏推荐
- 2017 USP Try-outs C. Coprimes
- 过拟合与正则化
- Add level control and logger level control of Solon logging plug-in
- On-off and on-off of quality system construction
- kubeadm系列-02-kubelet的配置和启动
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- Software test -- 0 sequence
- 个人开发的渗透测试工具Satania v1.2更新
- Codeforces Round #716 (Div. 2) D. Cut and Stick
- Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
猜你喜欢

Hang wait lock vs spin lock (where both are used)

Codeforces round 712 (Div. 2) d. 3-coloring (construction)

F - Two Exam(AtCoder Beginner Contest 238)

Solution to the palindrome string (Luogu p5041 haoi2009)

Sword finger offer 05 Replace spaces

【Jailhouse 文章】Jailhouse Hypervisor

挂起等待锁 vs 自旋锁(两者的使用场合)

Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade

Solution to game 10 of the personal field

Sword finger offer 06 Print linked list from beginning to end
随机推荐
Kubedm series-00-overview
Sword finger offer 06 Print linked list from beginning to end
F - Two Exam(AtCoder Beginner Contest 238)
How can the Solon framework easily obtain the response time of each request?
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Zzulioj 1673: b: clever characters???
shared_ Repeated release heap object of PTR hidden danger
SSH password free login settings and use scripts to SSH login and execute instructions
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
Pointnet++ learning
【Jailhouse 文章】Jailhouse Hypervisor
软件测试 -- 0 序
kubeadm系列-02-kubelet的配置和启动
On-off and on-off of quality system construction
智慧工地“水电能耗在线监测系统”
[practical skills] technical management of managers with non-technical background
AtCoder Grand Contest 013 E - Placing Squares
Cluster script of data warehouse project
Acwing 4301. Truncated sequence
【云原生】微服务之Feign自定义配置的记录