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.

这道题给了我们两个字符串,表示两个句子,每个句子中都有若干个单词,用空格隔开,现在让我们找出两个句子中唯一的单词。那么只要把每个单词都提取出来,然后统计其在两个句子中出现的个数,若最终若某个单词的统计数为1,则其一定是符合题意的。所以我们可以先将两个字符串拼接起来,中间用一个空格符隔开,这样提取单词就更方便一些。在 Java 中,可以使用 split() 函数来快速分隔单词,但是在 C++ 中就没这么好命,只能使用字符串流 istringstream,并用一个 while 循环来一个一个提取。当建立好了单词和其出现次数的映射之后,再遍历一遍 HashMap,将映射值为1的单词存入结果 res 即可,参见代码如下:

class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
vector<string> res;
unordered_map<string, int> wordCnt;
istringstream iss(A + " " + B);
while (iss >> A) ++wordCnt[A];
for (auto a : wordCnt) {
if (a.second == 1) res.push_back(a.first);
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/884

参考资料:

https://leetcode.com/problems/uncommon-words-from-two-sentences/

https://leetcode.com/problems/uncommon-words-from-two-sentences/discuss/158967/C%2B%2BJavaPython-Easy-Solution-with-Explanation

https://leetcode.com/problems/uncommon-words-from-two-sentences/discuss/158981/Java-3-liner-and-5-liner-using-HashMap-and-HashSets-respectively.

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 884. Uncommon Words from Two Sentences 两个句子中不相同的单词的更多相关文章

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

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

  2. Leetcode884.Uncommon Words from Two Sentences两句话中的不常见单词

    给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...

  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_easy】884. Uncommon Words from Two Sentences

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

  5. 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)

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

  6. [Swift]LeetCode884. 两句话中的不常见单词 | 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 ...

  7. leetcode 884. 两句话中的不常见单词 (python)

    给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...

  8. leetcode-解题记录 884. 两句话中的不常见单词

    题目 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不 ...

  9. 领扣(LeetCode)两句话中的不常见单词 个人题解

    给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...

随机推荐

  1. Debug 路漫漫-12:Python: ValueError: 'userid' is both an index level and a column label, which is ambiguous.

    啊,又遇到难题了 == 想要对两个 dataframe 做自然连接 merge,连接的key 为 “userid”,但是报错:ValueError: 'userid' is both an index ...

  2. 十、自定义ThreadPoolExecutor线程池

    自定义ThreadPoolExecutor线程池 自定义线程池需要遵循的规则 [1]线程池大小的设置 1.计算密集型: 顾名思义就是应用需要非常多的CPU计算资源,在多核CPU时代,我们要让每一个CP ...

  3. jQuery 源码解析(三) pushStack方法 详解

    该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...

  4. Redis缓存穿透,缓存击穿,缓存雪崩,热点Key

    导读 使用Redis难免会遇到Redis缓存穿透,缓存击穿,缓存雪崩,热点Key的问题.有些同学可能只是会用Redis来存取,基本都是用项目里封装的工具类来操作.但是作为开发,我们使用Redis时可能 ...

  5. fatal error compiling: tools.jar not found

    在Eclipse中使用Maven提供的Install(打包)命令插件的时候报错[Fatal error compiling: tools.jar not found]. 报错的原因 报错的原因从错误信 ...

  6. ORACLE 求和(多列)

    SELECT SUM(列名),SUM(列名),SUM(列名),SUM(列名) FROM 表名

  7. Python【day 14-2】递归遍历文件夹

    #需求 遍历文件夹中所有的子文件夹及子文件--用递归实现 '''''' ''' 伪代码 1.遍历根目录--listdir for 得到第一级子文件夹(不包含子文件夹的子文件)和文件 2.判断是文件还是 ...

  8. PowerShell美化

    转载自Powershell 美化 --oh-my-posh,作者Zvonimir. PowerShell默认的主题太丑了,用过OhMyZsh之后是无法忍受这种丑陋的,幸好PowerShell有对应的O ...

  9. 修改host指定域名指向ip,Windows脚本与Linux脚本

    修改host指定域名指向ip,Windows脚本与Linux脚本 一,Windows系统修改hosts文件 Windows系统下hosts文件位置:C:\Windows\System32\driver ...

  10. Linux CentOS 防止SSH暴力破解

    一. 问题的发现 昨晚苦逼加班完后,今早上班继续干活时,SSH连接服务器发现异常的提示,仔细看了一下吓一小跳,昨晚9点钟到现在,一夜之间被人尝试连接200+,慌~~~ 1. 速度查一下log [roo ...