[LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example:
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.
Note:
1 <= paragraph.length <= 1000
.1 <= banned.length <= 100
.1 <= banned[i].length <= 10
.- The answer is unique, and written in lowercase (even if its occurrences in
paragraph
may have uppercase symbols, and even if it is a proper noun.) paragraph
only consists of letters, spaces, or the punctuation symbols!?',;.
- There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
这道题给了我们一个字符串,是一个句子,里面有很多单词,并且还有标点符号,然后又给了我们一个类似黑名单功能的一个字符串数组,让我们在返回句子中出现的频率最高的一个单词。要求非常简单明了,那么思路也就简单粗暴一些吧。因为我们返回的单词不能是黑名单中的,所以我们对于每一个统计的单词肯定都需要去黑名单中检查,为了提高效率,我们可以把黑名单中所有的单词放到一个HashSet中,这样就可以常数级时间内查询了。然后要做的就是处理一下字符串数组,因为字符串中可能有标点符号,所以我们先过滤一遍字符串,这里我们使用了系统自带的两个函数isalpha()和tolower()函数,其实自己写也可以,就放到一个子函数处理一下也不难,这里就偷懒了,遍历每个字符,如果不是字母,就换成空格符号,如果是大写字母,就换成小写的。然后我们又使用一个C++中的读取字符串流的类,Java中可以直接调用split函数,叼的飞起。但谁让博主固执的写C++呢,也无所谓啦,习惯就好,这里我们也是按照空格拆分,将每个单词读出来,这里要使用一个mx变量,统计当前最大的频率,还需要一个HashMap来建立单词和其出现频率之间的映射。然后我们看读取出的单词,如果不在黑名单中内,并且映射值加1后大于mx的话,我们更新mx,并且更新结果res即可,参见代码如下:
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
unordered_set<string> ban(banned.begin(), banned.end());
unordered_map<string, int> strCnt;
int mx = ;
for (auto &c : paragraph) c = isalpha(c) ? tolower(c) : ' ';
istringstream iss(paragraph);
string t = "", res = "";
while (iss >> t) {
if (!ban.count(t) && ++strCnt[t] > mx) {
mx = strCnt[t];
res = t;
}
}
return res;
}
};
参考资料:
https://leetcode.com/problems/most-common-word/
https://leetcode.com/problems/most-common-word/discuss/124286/Clean-6ms-C%2B%2B-solution
[LeetCode] Most Common Word 最常见的单词的更多相关文章
- LeetCode 819. Most Common Word (最常见的单词)
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- Leetcode819.Most Common Word最常见的单词
给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写字母表 ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Completing Word 最短完整的单词
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- C#LeetCode刷题之#819-最常见的单词(Most Common Word)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3969 访问. 给定一个段落 (paragraph) 和一个禁用单 ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- DirectX11 With Windows SDK--14 深度测试
前言 当使用加法/减法/乘法颜色混合,或者使用透明混合的时候,在经过深度测试时可能会引发一些问题.例如现在我们需要使用加法混合来绘制一系列对象,而这些对象彼此之间不会相互阻挡.若我们仍使用原来的深度测 ...
- WPF布局的应用
一 写在开头1.1 本文内容本文主要内容是使用WPF来实现几个简单的界面. 二 登录窗口小例子2.1 实现代码XAML代码: <Window x:Class="LoginDialog. ...
- 第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
一. 简介 1. 优先级:Fluent API > data annotations > default conventions. 2. 所有的Fluent API配置都要在 OnMode ...
- [物理学与PDEs]第1章第8节 静电场和静磁场 8.3 静磁场
1. 静磁场: 由稳定电流形成的磁场. 2. 此时, Maxwell 方程组为 $$\beex \bea \Div{\bf D}&=\rho_f,\\ \rot {\bf E}&={\ ...
- APPLE-SA-2019-3-25-6 iCloud for Windows 7.11
APPLE-SA-2019-3-25-6 iCloud for Windows 7.11 iCloud for Windows 7.11 is now available and addresses ...
- es6编程建议和技巧点汇总
大括号 特点:大括号(单独的大括号或者if等后的大括号)内是一个单独的作用域 注意点:在块级作用域内声明的函数,类似var,会被提升到大括号外,应避免在块级作用域内声明函数.如果确实需要,写成函数表达 ...
- CEYE平台的使用
0x01 CEYE 是什么 CEYE是一个用来检测带外(Out-of-Band)流量的监控平台,如DNS查询和HTTP请求.它可以帮助安全研究人员在测试漏洞时收集信息(例如SSRF / XXE / R ...
- Java开发之@PostConstruct和@PreDestroy注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- Lua中的一些库(1)
[数学库] 数学库(math)由一组标准的数学函数构成.这里主要介绍几个常用的函数,其它的大家可以自行百度解决. 三角函数(sin,cos,tan……)所有的三角函数都使用弧度单位,可以用函数deg( ...
- English 动词篇
动词后加to do 和 doing的记忆口诀 一.只能用动名词(ing)作宾语 [口诀] 考虑建议盼原谅,承认推迟没得想. 避免错过继续练,否定完成停欣赏. 禁止想象才冒险,不禁介意弃逃亡. cons ...