Uncommon Words from Two Sentences LT884
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:
0 <= A.length <= 2000 <= B.length <= 200AandBboth 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的更多相关文章
- 【Leetcode_easy】884. Uncommon Words from Two Sentences
problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...
- [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 ...
- 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 ...
- [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 ...
- Uncommon Words from Two Sentences
https://leetcode.com/problems/uncommon-words-from-two-sentences We are given two sentences A and B. ...
- [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 ...
- C#LeetCode刷题之#884-两句话中的不常见单词(Uncommon Words from Two Sentences)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3816 访问. 给定两个句子 A 和 B . (句子是一串由空格分 ...
- 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetc ...
- LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)
题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...
随机推荐
- Python循环语句之break与continue的用法
摘自原文章: http://www.jb51.net/article/73383.htm Python break 语句Python break语句,就像在C语言中,打破了最小封闭for或while循 ...
- python之路——14
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.迭代器 1.双下方法:不常直接调用,是通过其他语法触发的 2.可迭代的:可迭代协议——含有__it ...
- weblogic发序列化命令执行漏洞工具分享
weblogic发序列化命令执行漏洞工具分享(链接: https://pan.baidu.com/s/1qE5MFJ32672l-MMl-QL-wQ 密码: d85j) JBOSS_EXP 工具分享( ...
- docker 小结
1.docker修改镜像地址: /etc/docker/daemon.json 2.docker 启动容器: docker run -t -i ubuntu:14.04 /bin/bash 3.查看容 ...
- day39数据库之基本数据类型
数据库之数据类型1.数据存储引擎 一个功能的核心部分,回到mysql 核心功能是存储数据 涉及到存储数据的代码 就称之为存储引擎 根据不同的需求 也有着不同的引擎分类 不 ...
- debug protractor
HTAir:protractor-cucumber-typescript kbladewht$ node --inspect-brk=0.0.0.0:1229 node_modules/protrac ...
- 定位JVM内存溢出问题思路总结
JVM的内存溢出问题,是个常见而有时候有非常难以定位的问题.定位内存溢出问题常见方法有很多,但是其实很多情况下可供你选择的有效手段非常有限.很多方法在一些实际场景下没有实用价值.这里总结下我的一些定位 ...
- py库: arrow (时间)
arrow是个时间日期库,简洁易用.支持python3.6 https://arrow.readthedocs.io/en/latest/ arrow官网api https://github.com/ ...
- leetcode309
使用动态规划,下面的代码可以通过210个测试,最后1个(第211个)会超时.说明思路是正确的,但是其中有一些是无效的计算. class Solution { public: int maxProfit ...
- hadoop分布式集群搭建(2.9.1)
1.环境 操作系统:ubuntu16 jdk:1.8 hadoop:2.9.1 机器:3台,master:192.168.199.88,node1:192.168.199.89,node2:192.1 ...