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 <= 200
0 <= B.length <= 200
A
andB
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的更多相关文章
- 【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,把只出现过一次 ...
随机推荐
- 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...
- solr6.4.1搜索引擎(3)增量同步mysql数据库
尚未实现首次同步mysql数据库的,请参考我的另一篇文章http://www.cnblogs.com/zhuwenjoyce/p/6512378.html(solr6.4.1搜索引擎同步mysql数据 ...
- nginx的启动,停止和重启
启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...
- 应用程序与驱动程序通信 DeviceIoControl
之前写过一篇关于通过DeviceIoControl函数来使应用程序与驱动程序通信的博客,这次再通过这个完整的代码来简要疏通总结一下. 这种通信方式,就是驱动程序和应用程序自定义一种IO控制码,然后调用 ...
- 模块3 re + 正则表达式
1. 正则表达式 匹配字符串 元字符 . 除了换行 \w 数字, 字母, 下划线 \d 数字 \s 空白符 \n \t \b 单词的边界 \W 非xxx \D \S [] 字符组 [^xxx] 非xx ...
- You Only Look Once: Unified, Real-Time Object Detection
论文下载:http://arxiv.org/abs/1506.02640 代码下载:https://github.com/pjreddie/darknet Abstract 作者提出一种新的目标检测 ...
- getColor问题
getResource.getColor(R.color.color_name) 过时了,可以使用新加入的方法 ContextCompat.getColor(context, R.color.colo ...
- Linux:sudo,没有找到有效的 sudoers 资源。
首先,这是因为用户的权限不够导致的. 使用 ls -l /etc/passwd 查看所有用户及权限.只有可读权限(r),说明用户的权限不够. 因此,我们可以用以下方法修改用户权限: 1. su roo ...
- Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)
Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...
- 原生js实现Base64编码解码
注:ie10+ var str = window.btoa("liusong"); console.log(str); var s = window.atob("bGl1 ...