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:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A and B both contain only spaces and lowercase letters.

思路将所有words, 计数, == 1, append进入ans

Code

class Solution:
def uncommonWord(self, A, B):
words = A.split() +B.split()
ans, d = [], collections.Counter(words)
for k in d.keys():
if d[k] == 1:
ans.append(k)
return ans

[LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table的更多相关文章

  1. [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  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 解题报告

    题目要求 We are given two sentences A and B.  (A sentence is a string of space separated words.  Each wo ...

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

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

  5. [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  6. [LeetCode] 1. Two Sum_Easy tag: Hash Table

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  8. [LeetCode] 697. Degree of an Array_Easy tag: Hash Table

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

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

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

随机推荐

  1. KMP 算法详解

    之前模模糊糊的理解了KMP,结果由于并不是完全弄清楚而导致自己在一道题目上疯狂的T,似乎是next函数写的有问题,于是痛心疾首的回来写一篇报告,警示自己 对KMP来说,匹配串的next数组是重中之重, ...

  2. 异常详细信息: System.ArgumentException: 不支持关键字: “metadata”。

    ASP.NET MVC中报的一个错误... 异常详细信息: System.ArgumentException: 不支持关键字: “metadata”. 其实这个是我修改web.config文件造成的, ...

  3. [No0000107]C#中 Excel列字母与数字的转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. [No0000F9]C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其他函数一样,重载运 ...

  5. 【每日一题】 UVA - 11809 Floating-Point Numbers 阅读题+取对数处理爆double

    https://cn.vjudge.net/problem/UVA-11809 题意:很长orz 题解:算一下输入范围,发现用double是读不进来的,在这里wa了半天,(double 1e300  ...

  6. C和C指针小记(十)-函数

    1.函数的定义 函数的定义就是函数体的实现. 语法: 类型 函数名(形式参数) 代码块 函数返回类型和函数名分开写是代码风格的问题,现代语言如swift返回值在函数名和参数表的后面,这样使得某些工程工 ...

  7. day13: 迭代器和生成器

    1,思考所有可以被for循环的:list,tuple,set,dict,range,enumerate,f,str,差不多了,为何这些数据类型可以被for循环呢? 2,一个标准的装饰器函数 from ...

  8. https://pypi.org/project/py-mysql2pgsql/

    https://packages.ubuntu.com/trusty/postgresql-server-dev-9.3 所以使用下面的命令即可安装python-dev: yum install py ...

  9. 终端:Xcode模拟器上安装.app方法

    有的时候,我们可能需要将别人的Xcode运行之后的程序包(xxx.app)安装在自己的模拟器上,如下我将介绍如何通过终端来安装. 实现 获取自己Xcode生成的xxx.app steps 1:在工程d ...

  10. 完美解决failed to open stream: HTTP request failed!(file_get_contents引起的)

    当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP reque ...