Uncommon Words from Two Sentences
https://leetcode.com/problems/uncommon-words-from-two-sentences
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.
解题思路:
简单但是蛋疼的一道题目。读题目后发现,这里的uncommon,其实就是在A和B里面加起来只出现一次的。
先用map统计次数,然后拿出只出现一次的词,最后形成array。
class Solution {
public String[] uncommonFromSentences(String A, String B) {
Map<String, Integer> map = new HashMap<String, Integer>();
String[] a1 = A.split(" ");
String[] b1 = B.split(" ");
for (String str : a1) {
map.put(str, map.getOrDefault(str, 0) + 1);
}
for (String str : b1) {
map.put(str, map.getOrDefault(str, 0) + 1);
} List<String> list = new ArrayList<String>();
for (Map.Entry<String, Integer> entry : map.entrySet())
{
if (entry.getValue() == 1) {
list.add(entry.getKey());
}
}
String[] res = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}
有人写的更简洁,逻辑是一样的
https://leetcode.com/problems/uncommon-words-from-two-sentences/discuss/158967/C%2B%2BJavaPython-Easy-Solution-with-Explanation
Uncommon Words from Two Sentences的更多相关文章
- 【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 ...
- 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 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 ...
- [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,把只出现过一次 ...
随机推荐
- 多线程快速解压FastZipArchive介绍
本文转载至 http://blog.csdn.net/xunyn/article/details/12975937 多线程解压iosfast 在iOS项目中用到解压缩,用的是ZipArchive ...
- String代码示例
package lianxi; public class lianxi0112 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...
- 补充ajax分页的代码
1.主页代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- MySQL 5.7 等高版本关于JDBC驱动的几个问题
https://blog.csdn.net/dj673344908/article/details/85223313 mysql 5.7 用8.0版本的驱动可以,5.1版本也可以,5.5.5.6.5. ...
- 更精炼更专注的RTMPClient客户端EasyRTMPClient,满足直播、转发、分析等各种需求
现状 EasyRTMPClient,熟悉的朋友就会联想到EasyRTSPClient项目(https://github.com/EasyDSS/EasyRTSPClient),EasyRTSPClie ...
- go colly proxy 代理ip 动态 ip
package main import ( "fmt" "github.com/gocolly/colly" "github.com/gocolly/ ...
- python数据分析之:数据清理,转换,合并,重塑(一)
DataFrame合并: merge运算是将一个或多个键将行链接起来.来看下面的这个例子: In [5]: df1=DataFrame({'key':['b','b','a','c','a','a', ...
- centos7 执行一个数据库脚本创建项目中的数据库
[root@localhost ~]# su postgres 切换用户 bash-4.2$ psql could not change directory to "/root": ...
- matlab使用usb和gige 网口相机
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- 【python】使用python写windows服务
背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...