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,把只出现过一次 ...
随机推荐
- 多版本opencv管理; find_package()的原理解析
近期用cmake编译程序时,报错找不到opencv2.由于我电脑里安装了多个版本的opencv,管理不善,借此机会梳理一下思路. 1. Cmake -- find_package(Opencv REQ ...
- PTA上Java问题自查与提问方法
自查 首先请一定先看这篇文章<PTA中提交Java程序的一些套路>.该文囊括了PTA中提交Java程序的几乎所有常见问题,请仔细阅读可以少踩很多坑 题目测试方法:复制样例输入,然后打开一个 ...
- Android批量验证渠道、版本号(Linux版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号(Linux版本)使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2../VerifyChannelVe ...
- vi 使用小结
复制 1,ny 从哪行到哪行的复制,中间用逗号隔开,然后命令y. 黏贴 是在命令模式下直接按p即可 跳到n行: 命令模式直接输入数字即可 剪切:d命令 删除:x命令 跳到行首行尾:直接home或end ...
- JS数据类型的判断
在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefine ...
- BZOJ 2173 luoguo P4451 [国家集训队]整数的lqp拆分
整数的lqp拆分 [问题描述] lqp在为出题而烦恼,他完全没有头绪,好烦啊… 他首先想到了整数拆分.整数拆分是个很有趣的问题.给你一个正整数N,对于N的一个整数拆分就是满足任意m>0,a1 , ...
- Android 开发 框架系列 glide-transformations 图片处理基本使用
首先简单的介绍一下Gilde作用范围.Gilde功能十分强大,它可以实现图片处理.图片本地加载.图片网络加载.位图加载.图片内存缓存.图片磁盘缓存.Gif图片加载.使用简单轻松,轻松的后是它强大的心, ...
- Shell脚本21-40例
21.统计数字并求和 计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字.例如a.txt内容如下:12aa*lkjskdjalskdflkskdjflkjj 我们脚本 ...
- spring 大会的启示
1.事件驱动的微服务编程 2.无服务架构的编程模型 3.微服务缓存
- Android中五大字符串总结(String、StringBuffer、StringBuilder、Spanna
https://www.aliyun.com/jiaocheng/2861.html?spm=5176.100033.1.35.2ed56b03CbsYFK 摘要:String.StringBuffe ...