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,把只出现过一次 ...
随机推荐
- Java继承(下)
Object类 在www.oracle中找到java 中的java.lang在中找到object类中找到可以看到在java语言中的定义 如何修改object中的equals类及测试 在object中类 ...
- STS启动时卡在loading加载 dashboard.ui
如果你在用STS 3.4或3.5,启动时可能会卡在 解决方法:打开STS安装目录下的plugins目录,删除文件 org.springsource.ide.eclipse.dashboard.ui_3 ...
- Python微信
""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Dat ...
- [SQL]sql server中如何直接查询存储过程EXEC返回的结果集?
Declare @T Table (iDay VARCHAR(),iNum DECIMAL(,),yuxiang DECIMAL(,)) Insert @T --EXEC [dbo].[BSP0101 ...
- NodeJS client code websocket
var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...
- Spring 中 Bean 的生命周期
所谓 Bean 的生命周期,就是一个 Bean 从创建到销毁,所经历的各种方法调用.大致包含下面几个方法(不是全部) Bean 的实例化,调用了构造方法. 使用 setter 方法填充属性. 一旦依赖 ...
- dubbo使用简介
---------------------------------------------------------------------------------------------------- ...
- Android 开发 框架系列 glide-transformations 图片处理基本使用
首先简单的介绍一下Gilde作用范围.Gilde功能十分强大,它可以实现图片处理.图片本地加载.图片网络加载.位图加载.图片内存缓存.图片磁盘缓存.Gif图片加载.使用简单轻松,轻松的后是它强大的心, ...
- Mybatis-spring 动态代理
1.UserMapper.java package com.cn.mapper; import java.util.List; import com.cn.pojo.User; public inte ...
- 转载:Opencv调整运行窗口图片的大小
本文来自:http://blog.csdn.net/cumtml/article/details/52807961 Opencv在运算时显示图片问题 总结在opencv中,图片显示的问题.简要解决图片 ...