作者: 负雪明烛
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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)

    题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...

  4. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  6. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  7. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  8. 【Leetcode_easy】884. Uncommon Words from Two Sentences

    problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. Linux服务器I/O性能分析-3

    一.通过脚本分析IO的读/写数量.最大延迟.延迟的分布情况.块大小及数量 #!/bin/sh # # File Name : count_io.sh # Time : 2020-07-29-11:24 ...

  2. 22-reverseString-Leetcode

    思路:so easy class Solution { public: string reverseString(string s) { int n = s.size(); for(int i=0;i ...

  3. 巩固javaweb的第二十八天

    巩固内容: 设置页面的编码方式 实现代码: 每个 JSP 页面都需要设置编码方式,设置 JSP 页面的编码方式可以是下面两种方式 之一. 方式一: <%@ page contentType=&q ...

  4. A Child's History of England.10

    In the next reign, which was the reign of Edward, surnamed The Elder, who was chosen in council to s ...

  5. Django url中可以使用类视图.as_view()进行映射的原因

    说明:在练习天天生鲜项目时,对利用类视图去与正则匹配到的url做映射有点疑惑,经过查看他人博客以及自我分析算是整明白了,所以记录一下 参考:https://www.zmrenwu.com/post/5 ...

  6. 商业爬虫学习笔记day4

    一.获取登录后页面信息的两种方法 1.第一种方法: 人为把有效cookies加到请求头中,代码如下 import urllib.request # 确定url url = "https:// ...

  7. Oracle—merge into语法

    oracle的merge into语法,在这种情况下: 基于某些字段,存在就更新,不存在就插入: 不需要先去判断一下记录是否存在,直接使用merge into merge into 语法: MERGE ...

  8. Linux系统时钟与硬件时钟

    linux系统有两个时钟:一个是由主板电池驱动的硬件时钟(Real Time Clock),也叫做RTC或者叫CMOS时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行的系统是不用这个时间的: ...

  9. Linux学习 - 帮助命令

    一.获取帮助信息man(manual) 1 功能 获得命令或配置文件的帮助信息 2 语法 man  [1.5]  [命令或配置文件] 1 命令的帮助 (可用 whatis 代替) 5 配置文件的帮助 ...

  10. Selenium之Canvas画布操作

    现在有一个场景是需要进入到 Canvas画布中 进行单击操作,现在使用过如下方法 canvas = driver.find_element_by_xpath("//canvas[@id='# ...