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. solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)

    編譯方法參見官網 - https://github.com/rime/home/wiki/RimeWithIBus 安装依赖:列出几个可能用到的命令 #安裝cmake gcc等开发工具 sudo eo ...

  2. 网络通信协议六之IP地址和MAC地址特征分析

    逻辑地址和物理地址 >>逻辑地址:工作在网络层,也叫IP地址,①具有全局唯一性②用软件实现③32位 10.1.0.6 -——>00001010.00000001.00000000.0 ...

  3. mysql distinct 后面没有括号

    当distinct应用到多个字段的时候,其应用的范围是其后面的所有字段,而不只是紧挨着它的一个字段,而且distinct只能放到所有字段的前面 如下语句是错误的: SELECT country, di ...

  4. ajax 200 4 parseerror 的错误

    这个问题也碰到几次: 最后在网上还是找到了点线索:1.一可能是data:中的json 不规范2.js语句不规范3.我碰到的是dataType: 'json',data:是数组,最后把json改为tex ...

  5. 一窥Spring Cloud Eureka

    在Spring Cloud中Eureka负责服务发现功能.服务发现需要解决如何找到服务提供者在网络中位置的问题. 服务端 在Spring Tool Suite的文件菜单中,点击新建Spring Sta ...

  6. 泡泡一分钟:Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition

    Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition Peng Yin, Lingyun Xu, Z ...

  7. 依赖: nginx-common (= 1.14.0-0ubuntu1) 但是它将不会被安装

    .apt --fix-broken install .sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件. .sudo apt-get ...

  8. React组件的State

    React组件的State 1.正确定义State React把组件看成一个状态机.通过与用户的交互,实现不同状态,然后渲染UI,让用户界面和数据保持一致.组件的任何UI改变,都可以从State的变化 ...

  9. python面向对象:类方法

    类的方法包括以下几种: 构造方法 :__init__(self,) 析构方法 :__del__(self) 类方法@classmethod.实例方法.静态方法@staticmethod 一.构造方法 ...

  10. JavaScript学习笔记--语法二

    条件判断与C语言一样 两种循环.for 循环和 while 循环,JavaScript不区分整数和浮点数,统一用Number表示,所以不是 int i var x = 0; var i; for (i ...