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,把只出现过一次 ...
随机推荐
- WCF基础之设计和实现服务协定
本来前面还有一个章节“WCF概述”,这章都是些文字概述,就不“复制”了,直接从第二章开始. 当然学习WCF还是要些基础的.https://msdn.microsoft.com/zh-cn/hh1482 ...
- Flask:web表单
客户端发送的所有通过POST发出的请求信息都可以通过request.form获取.但是如果我们要生成表单的HTML代码和验证提交的表单数据那么就需要采用另外的方法.Flask-WTF扩展可以把处理we ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- (转)三层和mvc
先说下两者出现的目的:三层是一种为了Project间解除耦合所提出来的简单的分层方式但MVC其实并不是基于Project的分层方式,而是一种解除展示模板与主要访问控制依赖的设计模式(其实全部都是基于U ...
- Redis shell
Redis shell 命令 参数 功能 redis-cli -r 将一个命令执行多次 -i 每隔几秒执行一次 -x 和|一起接收前面地输出,并执行命令 -c -a --scan/--patt ...
- Why use async requests instead of using a larger threadpool?(转载)
问: During the Techdays here in the Netherlands Steve Sanderson gave a presentation about C#5, ASP.NE ...
- PAT 甲级 1028. List Sorting (25) 【结构体排序】
题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...
- eclips 创建 maven项目
Maven安装完成后我们就可以在Eclipse中新建自己的Maven项目了.我们可以在Eclipse中选择new一个project,在出现的对话框中选择Maven目录下的Maven Project. ...
- 解决使用mybatis做批量操作时发生的异常:Parameter '__frch_item_0' not found. Available parameters are [list] 记录
本文主要描述 使用mybatis进行批量更新.批量插入 过程中遇到的异常及总结: 首先贴出使用批量操作报的异常信息: java.lang.RuntimeException: org.mybatis.s ...
- 程序连接Oracle数据库出现未找到提供程序.该程序可能未正确安装错误提示
好不容易使用plsql可以成功连上数据库了,应用程序连接数据库却出现了问题 其实解决这个问题也简单: 1. 查看oracle安装目录下的BIN目录,E:\app\Administrator\prod ...