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. jexus手动跨域设置

    AP.NET MVC默认跨域方法如下: <system.webServer> <validation validateIntegratedModeConfiguration=&quo ...

  2. Django:视图views(二)

    把request对象和response对象原理流程写一下 request对象 服务器端接收到http协议的请求,会根据报文信息构建HttpRequest对象 通过第一个参数,把该对象传递给视图函数 R ...

  3. hyperledger-fabric/qemu/kvm/virtual-manager -------vagrant-virtual-box

    天我也遇到了这个问题,原因是你的 vagrant 版本跟你的 virtualbox 版本不匹配,解决的方法是,更换 virtualbox 的版本.我的 vagrant 版本是 1.8.4 ,virtu ...

  4. 转:web.xml 配置中classpath: 与classpath*:的区别

    原文链接:web.xml 配置中classpath: 与classpath*:的区别 引用自:http://blog.csdn.net/wxwzy738/article/details/1698393 ...

  5. MyEclipse中JDK运行环境和编译环境的设置

    一.设置myEclipse中新项目使用的JDK 1.运行环境   [Window]->[Preferences]->[Java]->[Installed JREs] 步骤:Add-- ...

  6. 如何解决selenium打开chrome提示chromedriver.exe已停止工作

    场景:启动Chrome,打开URL,提示“disconnected: unable to connect to renderer” 解决方法:chromedriver与chrome的对应关系表, 需要 ...

  7. 【Python基础】安装python第三方库

    pip命令行安装(推荐) 打开cmd命令行 安装需要的第三方库如:pip install numpy 在安装python的相关模块和库时,我们一般使用“pip install  模块名”或者“pyth ...

  8. Word Embedding理解

    一直以来感觉好多地方都吧Word Embedding和word2vec混起来一起说,所以导致对这俩的区别不是很清楚. 其实简单说来就是word embedding包含了word2vec,word2ve ...

  9. 转: js实现全角半角检测的方法

    //全角半角校验 function issbccase(strTmp) { for (var i=0; i<strTmp.length; i++) { if (strTmp.charCodeAt ...

  10. IO和NIO

    一.创建IO System.out.println( "*************欢迎进入文件操作系统*************" ); System.out.println( & ...