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

https://leetcode.com/problems/most-common-word/discuss/123854/C%2B%2BJavaPython-Easy-Solution-with-Explanation

[LeetCode] Most Common Word 最常见的单词的更多相关文章

  1. 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 ...

  2. Leetcode819.Most Common Word最常见的单词

    给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写字母表 ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Shortest Completing Word 最短完整的单词

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  6. [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 ...

  7. leetcode Most Common Word——就是在考察自己实现split

    819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...

  8. C#LeetCode刷题之#819-最常见的单词(Most Common Word)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3969 访问. 给定一个段落 (paragraph) 和一个禁用单 ...

  9. [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

随机推荐

  1. Mysql MGR架构误操作引发的问题处理

    [背景介绍] 故障方描述:一次用户刷权限的时候不小心把数据库用户表记录删掉了,执行之后发现不对后重建用户,杀掉进程后重新MGR启动报错. [报错信息] 2018-06-13T12:47:41.4055 ...

  2. 同步Name到Comment 及 同步 Comment 到Name

    在 PowerDesigner执行命令  Tools->Execute Commands->Edit/Run Scripts 代码一:将Name中的字符COPY至Comment中 Opti ...

  3. 你对安卓触控一体机了解多少?视野还停留在windows一体机上?

    android一体机可以根据用户的不同需求拓展各种不同的硬件外接设备和各种应用软件,环境适应能力又强,所以在诸多领域的应用都非常受欢迎,并且还在不断地开拓新市场.安卓系统触摸一体机占据绝对性优势. 1 ...

  4. 🍓vue & react 一些重要但没必要死记硬背的东西

  5. windows bat 脚本(一)切换当前目录

    一.切换当前目录 现在桌面新建一个文件, 然后打开输入 cmd /k "cd /d D:\file"    如下图点击“另存为”,保存类型选择 “所有文件” 然后会在保存路径下发现 ...

  6. Selective Search for Object Recognition(理解)

    0 - 背景 在目标检测任务中,我们希望输入一副图像,输出目标所在的位置以及目标的类别.最常用的算法是滑动窗口方法,但滑动窗口其实相当于穷举图像中的所有子图像,其效率低且精度也受限.该论文提出一种新的 ...

  7. 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN

    object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.object detection要解决的问题就是物体在哪里,是什么这整个流程的问题.然而,这个问题 ...

  8. vue中怎么实现获取当前点击对象this

    应用场景 在评论列表中,有很多条评论(通过循环出来的评论列表),评论的文字有多跟少,默认展示2行超出显示点击查看更多,,要点击查看更多对当前的这条评论进行全部评论展示! 问题描述 要是在传统的点击事件 ...

  9. shell脚本的小记

    作者:邓聪聪 mysql的脚本执行 #!/bin/sh HOST="127.0.0.1" PORT=" UESRNAME="root" PASSWOR ...

  10. 【原创】大数据基础之CM5(Cloudera Manager)+CDH5离线安装

    CM/CDH 5.16.1 CM官方:https://www.cloudera.com/products/product-components/cloudera-manager.html CDH官方: ...