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 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!?',;.
- Different words in
paragraph
are always separated by a space. - There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
题目标签:String
题目给了我们一段paragraph,还有 banned words,让我们找出 在 paragraph 里出现最多的 non-banned word。
主要思想是,开始要把paragraph 都变成小写,然后利用 regex split 成 string array;
接着把banned words 放入 HashSet;
把 non-banned words 放入 HashMap,在这一过程中,可以保留 出现次数最多的 word,不需要在多一个loop 来找出 max。
!s.equals("") 的作用是因为 regex 会造成 “” 的情况,所以要跳过。
具体请看code。
Java Solution:
Runtime beats (no enough accepted submissions)%
完成日期:05/06/2018
关键词:HashSet; HashMap; Regex
关键点:用 Regex 来filter out string
class Solution
{
public String mostCommonWord(String paragraph, String[] banned)
{
HashSet<String> bannedSet = new HashSet<>();
HashMap<String, Integer> wordsMap = new HashMap<>();
String mcw = "";
int mcwCount = -1; // filter spaces and punctuation
String[] wordsArr = paragraph.toLowerCase().split(" |!|\\?|'|,|;|\\."); // put banned words into hash set
for(String s: banned)
bannedSet.add(s); // add and count non-banned words into wordsMap
for(String s: wordsArr)
{
if(!bannedSet.contains(s) && !s.equals(""))
{
wordsMap.put(s, wordsMap.getOrDefault(s, 0) + 1); int count = wordsMap.get(s);
// keep tracking the most common word
if(count > mcwCount)
{
mcw = s;
mcwCount = count;
}
} } return mcw; }
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 819. Most Common Word (最常见的单词)的更多相关文章
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- LeetCode 819. Most Common Word
原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...
- Leetcode819.Most Common Word最常见的单词
给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写字母表 ...
- 【Leetcode_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 819. Most Common Word 统计高频词(暂未被禁止)
[抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- 819. Most Common Word
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)
这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
随机推荐
- Python 语言搭建SELENIUM测试环境,搭建过程记录。
第一步,安装Python: 第二步,安装SetupTools: 第三步,安装Pip: 第四步,安装selenium(for python) 第五步,新建第一个基于Firefox的测试用例 上述 只是步 ...
- vux+vuex+vue+Es6开发微信公众号的坑
初次开发微信公众号遇到很多问题,可能是基础不怎么牢靠,最近几天一直在看vue的东西,现在就来慢慢介绍vux和vue这个骚东西的用法: 细看文档一步步来, npm install vux --save ...
- WinRT ListView间隔变色(二)
上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...
- 【sqli-labs】 对于less34 less36的宽字节注入的一点深入
1.AddSlashes() 首先来观察一下是如何通过构造吃掉转义字符的 先将less 34的网页编码换成gbk 加上一些输出 echo "Before addslashes(): &quo ...
- 梦想CAD控件关于标注的系统变量说明
主要用到函数说明: IMxDrawDimension::SetDimVarDouble 设置标注属性的实数类型变量值,详细说明如下: 参数 说明 [in] LONG iType 该属性的类形值 dVa ...
- 配置servlet出现java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
拷贝一份sqljdbc.jar放到/WEB-INF/lib即可
- vue SSR 部署详解
先用vue cli初始化一个项目吧. 输入命令行开始创建项目: vue create my-vue-ssr 记得不要选PWA,不知为何加了这个玩意儿就报错. 后续选router模式记得选 histor ...
- Trie树 hihocoder 1014
Trie树 hihocoder 1014 传送门 字典树的基本应用 #include<queue> #include<cmath> #include<cstdio> ...
- 【Codeforces 375A】Divisible by Seven
[链接] 我是链接,点我呀:) [题意] 让你把一个包含数字1,6,8,9的数字重新组合,使得组合成的数字能被7整除 [题解] 我们先提取出来1,6,8,9各1个 然后把剩余的len-4个数字除了0之 ...
- HDU 4780 Candy Factory
Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...