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. Loj #2568. 「APIO2016」烟花表演

    Loj #2568. 「APIO2016」烟花表演 题目描述 烟花表演是最引人注目的节日活动之一.在表演中,所有的烟花必须同时爆炸.为了确保安全,烟花被安置在远离开关的位置上,通过一些导火索与开关相连 ...

  2. mybatis使用collection查询集合属性规则

    接上篇mybatis使用associaton进行分步查询 相关的类还是上篇中的类. 查询部门的时候将部门对应的所有员工信息也查询出来 DepartmentMapper.xml <!--嵌套结果集 ...

  3. Autoware 培训笔记 No. 2——基于点云的定位

    1. 前言 构建出地图后,应该测试点云地图定位效果,这里用到ndt的scan_matching方法,这是一种scan-to-map方法.这里用的是我们自己采集的数据进行仿真. 本章内容有和No. 1重 ...

  4. .net core EF Core 视图的应用

    由之前的一篇文章<.net core Entity Framework 与 EF Core>我们都已经知道 EF Core 增加了许多特性,并且性能上也有了很大的提升. 但是EF Core ...

  5. 4、Ext.NET 1.7 官方示例笔记 - 树

    <%@ Page Language="C#" %> <%@ Import Namespace="System.Collections.Generic&q ...

  6. java socket通信:聊天器(1)

    目的:实现多个客户之间的通信 首先,这个聊天器的框架是这样的: 对于服务器端:建立socket,连接到服务器,并且开始监听. import java.io.*; import java.util.Ar ...

  7. CSS animation属性

    定义和用法 animation属性是下列属性的一个缩写属性: animation-name animation-duration animation-timing-function animation ...

  8. 【转载】Visual Studio2017如何打包发布Winform窗体程序

    在用C#语言编写好Winform窗体程序后,最后一步的操作是将设计好的Winform程序代码进行打包以及发布成安装包.在Visual Studio2017开发工具中,打包发布WinForm程序是比较简 ...

  9. python高级语法1

    1.解压序列赋值给多个变量 p = (4, 5, 6) x, y , z = p # x = 4, y = 5, z = 6 若可迭代对象超过变量个数,会抛出ValueError 用处:可以解压赋值任 ...

  10. 记录几个 Android x86 系统的官网

    首先是官网:https://www.android-x86.org/ 国内: 凤凰OS(Android 7.1):http://www.phoenixos.com/download_x86 技德Rem ...