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.

Idea 1. HashMap count the occurences, find the occurence == 1, 读题啊,一开始还写了2个map

Time complexity: O(M + N), M = A.length(), N = B.length()

Space complexity: O(M + N)

 class Solution {
public String[] uncommonFromSentences(String A, String B) {
Map<String, Integer> count = new HashMap<>();
for(String str: A.split("\\s")) {
count.put(str, count.getOrDefault(str, 0) + 1);
} for(String str: B.split("\\s")) {
count.put(str, count.getOrDefault(str, 0) + 1);
} List<String> result = new ArrayList<>();
for(String str: count.keySet()) {
if(count.get(str) == 1) {
result.add(str);
}
} return result.stream().toArray(String[]::new);
}
}

Uncommon Words from Two Sentences LT884的更多相关文章

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

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

  2. [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 ...

  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&Python] Problem 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 ...

  5. Uncommon Words from Two Sentences

    https://leetcode.com/problems/uncommon-words-from-two-sentences We are given two sentences A and B.  ...

  6. [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 ...

  7. C#LeetCode刷题之#884-两句话中的不常见单词(Uncommon Words from Two Sentences)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3816 访问. 给定两个句子 A 和 B . (句子是一串由空格分 ...

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

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

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

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

随机推荐

  1. Flask 框架

    装饰器知识回顾 http://www.cnblogs.com/0bug/p/7978595.html 普通装饰器格式: def wrapper(func): def inner(*args, **kw ...

  2. kafuka资料学习

    http://blog.csdn.net/hmsiwtv/article/details/46960053

  3. 报错:NoSuchMethodError: kafka.javaapi.PartitionMetadata.leader()Lkafka/cluster/Broker;

    报错现象: 在pom文件添加: <dependency> <groupId>org.apache.kafka</groupId> <artifactId> ...

  4. redis总结问题

    简单回顾了redis,在这过程中 首先得了解redis是什么,redis的运用场景,redis支持哪些数据格式,redis如何操作数据,redis如何实现高可用 redis是什么: Redis 是一个 ...

  5. 6.3.4 新的_Bool类型

    如果把其他非零数值赋给_Bool类型的变量,该变量会被设置为1.这反映了C把所有的非零值都视为真. input_is_good = (scanf("%ld", &num) ...

  6. Ajax传递json数据简介和一个需要注意的小问题

    Ajax传递json数据 Ajax操作与json数据格式在实际中的运用十分广泛,本文为大家介绍一个两者相结合的小案例: 项目结构 我们新建一个Django项目,在里面创建一个名为app01的应用: p ...

  7. Java选择排序,插入排序,快速排序

      public class Test { public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5 }; 选择排序(a); ...

  8. MRO,C3算法

    1了解python2和python3类的区别 python2在2.4之前使用的是经典类, 2.4之后, 使用的是新式类 class Foo: pass class Foo(object): pass ...

  9. spring 入门demo

    相关资源 官网地址:http://projects.spring.io/spring-boot/ 创建maven项目 勾选箭头处,创建一个简单的项目  填写groupId和artifactId,点击确 ...

  10. 【函数】raise 函数(小窗help)

    在Python中,要想引发异常,最简单的形式就是输入关键字raise,后跟要引发的异常的名称. 异常名称标识出具体的类: Python异常处理是那些类的对象. 执行raise语句时,Python会创建 ...