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 ...
随机推荐
- mybatis 关联查询
1.关联的两个实体类 外部类 parent public class Parent{ private String parentId; private String parentName; priva ...
- 三维重建PCL:点云单侧面正射投影
终于把点云单侧面投影正射投影的代码写完了,为一个阶段,主要使用平面插值方法,且只以XOY平面作为的正射投影面.有些凑合的地方,待改进. 方法思路:使用Mesh模型,对每一个表面进行表面重建.借助Ope ...
- Hive DDL&DML
1.删除分区 ALTER TABLE table_name DROP IF EXISTS PARTITION(dt=') 如果是外部表,记得rm对应文件 2.添加分区 ALTER TABLE tabl ...
- POJ_2239_Selecting Courses
题意:一周上7天课,每天12节课,学校最多开设300节不同的课,每周每种课可以只有一个上课时间或者多个上课时间(上课内容一样),问一周最多可以选多少节课. 分析:二分图最大匹配,将一周84个时间点和可 ...
- HDU_1207_汉诺塔2
汉诺塔II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Flask框架 之request对象
一.request对象属性 属性 说明 类型 data 记录请求的数据,并转换为字符串 * form 记录请求中的表单数据 MultiDict args 记录请求中的查询参数 MultiDict co ...
- CAD如何动态绘制带面积周长的圆?
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- 梦想CAD控件网页版文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现js代码如下: function CreateText(){ //返回控件的数据库对象 var database =mxOcx. ...
- PHP爬数据 QueryList
QueryList官方文档:https://www.querylist.cc/docs/guide/v3 因为php版本使用5.6,所以使用QueryList v3版本,php7可以使用 v4版本 v ...
- 17Web应用乱码问题
Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...