当前位置:网站首页>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
边栏推荐
- Introduction et expérience de wazuh open source host Security Solution
- Software test -- 0 sequence
- Hang wait lock vs spin lock (where both are used)
- Scope of inline symbol
- Time complexity and space complexity
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- Control Unit 控制部件
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- 【实战技能】非技术背景经理的技术管理
- Sword finger offer 58 - ii Rotate string left
猜你喜欢
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
剑指 Offer 05. 替换空格
A new micro ORM open source framework
sync. Interpretation of mutex source code
Wazuh開源主機安全解决方案的簡介與使用體驗
剑指 Offer 06.从头到尾打印链表
Implement an iterative stack
CF1634 F. Fibonacci Additions
【Jailhouse 文章】Jailhouse Hypervisor
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
随机推荐
利用HashMap实现简单缓存
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
【实战技能】如何做好技术培训?
Sword finger offer 06 Print linked list from beginning to end
个人开发的渗透测试工具Satania v1.2更新
[jailhouse article] look mum, no VM exits
Web APIs DOM node
Typical use cases for knapsacks, queues, and stacks
Wazuh開源主機安全解决方案的簡介與使用體驗
Sword finger offer 04 Search in two-dimensional array
Little known skills of Task Manager
F - Two Exam(AtCoder Beginner Contest 238)
Acwing 4300. Two operations
CF1634 F. Fibonacci Additions
【云原生】微服务之Feign自定义配置的记录
Sword finger offer 05 Replace spaces
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
sync.Mutex源码解读
全排列的代码 (递归写法)