【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/uncommon-words-from-two-sentences/description/
题目描述
We are given two sentences A
and B
. (A sentence is a string of space separated words. Each word consists only of lowercase letters.)
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Return a list of all uncommon words.
You may return the list in any order.
Example 1:
Input: A = "this apple is sweet", B = "this apple is sour"
Output: ["sweet","sour"]
Example 2:
Input: A = "apple apple", B = "banana"
Output: ["banana"]
Note:
- 0 <= A.length <= 200
- 0 <= B.length <= 200
- A and B both contain only spaces and lowercase letters.
题目大意
如果一个词在一句话中只出现了一次,在另外一句话中没出现,那么这个词是不同的词。找出两句话中所有不同的词。
解题方法
字典统计
统计一下两句话单词的set,找出两个set的不同词,然后再判断这个词是否只出现了1次,如果只出现了1次,即为题目所求。
注意,要先找不同词,然后再判断是否出现1次。如这个测试用例:
Input:
"s z z z s"
"s z ejt"
Output:
["ejt","s","z"]
Expected:
["ejt"]
不可以先去重组成set,然后再求。。也就是说,只要一个词在一句话中出现的次数超过了1次,那么一定会被排除掉。
另外注意,这其实是求下面这个图的A+B部分。在python3中的Counter.keys是个set,可以直接做交并补操作。
代码如下:
class Solution:
def uncommonFromSentences(self, A, B):
"""
:type A: str
:type B: str
:rtype: List[str]
"""
count_A = collections.Counter(A.split(' '))
count_B = collections.Counter(B.split(' '))
words = list((count_A.keys() | count_B.keys()) - (count_A.keys() & count_B.keys()))
ans = []
for word in words:
if count_A[word] == 1 or count_B[word] == 1:
ans.append(word)
return ans
日期
2018 年 8 月 16 日 —— 一个月不写题,竟然啥都不会了。。加油!
2018 年 11 月 8 日 —— 项目进展缓慢
【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)的更多相关文章
- LeetCode 884 Uncommon Words from Two Sentences 解题报告
题目要求 We are given two sentences A and B. (A sentence is a string of space separated words. Each wo ...
- [LeetCode] 884. Uncommon Words from Two Sentences 两个句子中不相同的单词
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)
题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【Leetcode_easy】884. Uncommon Words from Two Sentences
problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
随机推荐
- python—模拟生成双色球号和大乐透号
下边这个脚本,比较适合初级学习基本python语法用.但是,不精炼建议可参考https://www.cnblogs.com/Formulate0303/p/14031748.html的写法. 大乐透玩 ...
- CentOS6源码安装zabbix服务器
1.下载安装包并解压 2.预环境搭建 3.创建zabbix用户,编译安装zabbix 4.配置mysql 5.配置zabbix-server 6.配置apache和php 7.添加开机自启动 1 yu ...
- 利用unordered_map维护关联数据
在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...
- Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...
- 【Java 8】Stream中的Pipeline理解
基于下面一段代码: public static void main(String[] args) { List<String> list = Arrays.asList("123 ...
- 莫烦python教程学习笔记——使用波士顿数据集、生成用于回归的数据集
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- Java常用类,这一次帮你总结好!
常用类 常用类概述: 内部类 Object类 包装类 数学类 时间类 字符串 String Builder和StringBuffer DecimalFormat 一.内部类 概念:在一个类内部再定义一 ...
- 剖析虚幻渲染体系(13)- RHI补充篇:现代图形API之奥义与指南
目录 13.1 本篇概述 13.1.1 本篇内容 13.1.2 概念总览 13.1.3 现代图形API特点 13.2 设备上下文 13.2.1 启动流程 13.2.2 Device 13.2.3 Sw ...
- 2、Spring的IOC标签介绍以及实例
一.Spring_ioc配置文件bean标签介绍 1. bean标签 名称:bean 类型:标签 归属:beans标签 作用:定义spring中的资源,受此标签定义的资源将受到spring控制 格式: ...
- Kerberos认证
http://www.cnblogs.com/artech/archive/2011/01/24/kerberos.html 最近一段时间都在折腾安全(Security)方面的东西,比如Windows ...