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 (最常见的单词)的更多相关文章

  1. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  2. LeetCode 819. Most Common Word

    原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...

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

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

  4. 【Leetcode_easy】819. Most Common Word

    problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...

  5. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

  6. 819. Most Common Word 统计高频词(暂未被禁止)

    [抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

  7. 819. Most Common Word

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)

    这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...

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

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

随机推荐

  1. 使用morphia实现对mongodb的聚合查询

    morphia是谷歌的一个针对mongodb的数据化持久框架: 关于mongodb的介绍不在这里展示,直接进入主题:采用morphia实现对mongodb的聚合查询 这里获取所有学生的分数总和 spr ...

  2. Magento 多站点多域名安装教程(可以设置手机模版哟,亲 \(^o^)/)

    这篇文章是安装magento子域名的教程,请先进行安装之前,确认以下几点: 1.请先确认子域名是否已经指向你的服务器 2.可以编辑.htaccess文件 3. 熟悉Cpanel操作 我们的目标是建立一 ...

  3. [oracle]表空间情况查看、占用、扩容、使用情况、空间维护等操作

    --查询表空间使用情况SELECT Upper(F.TABLESPACE_NAME)         "表空间名",       D.TOT_GROOTTE_MB          ...

  4. 使用CocoaPods,文档中出现引用头文件找不到的问题。

    现在很多人都会使用CocoaPods来管理自己使用的第三方开源代码,这两天我的工程中碰到了这样一个问题,当我使用CocoaPods来进行三方源码的引入,但是在实际的工程当中引入出现了这样一个问题,就是 ...

  5. R函数详解

    字符串连接函数paste 1.字符串连接:paste(..., sep = " ", collapse = NULL)sep表示分隔符,默认为空格.collapse表示如果不指定值 ...

  6. 使用vs2010打开vs2015的项目

    本来在单位项目一直使用vs2010写,五一放假拿回家 ,用vs2015捣鼓了一下 当然向下兼容打开毫无问题,结果回来悲催了,用vs2010打不开了 ,打不开. 记得以前有个转换向导,可是这次没看见,一 ...

  7. CAD绘制多行文字

    在CAD设计时,需要绘制多行文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawMText 绘制一个多行文字.详细说明如下: 参数 说明 DOUBLE dP ...

  8. ES 提案的各状态

    JavaScrpit,亦即 ECMAScript,新功能的演进是由一个叫 TC39 这么个组织在统筹协调和推进的. 一般新特性会由社区先提案,被采纳后开始进入下一流程.一个提案到最终落地到成为标准,需 ...

  9. luogu P3899 [湖南集训]谈笑风生 线段树合并

    Code: #include<bits/stdc++.h> #define maxn 300002 #define ll long long using namespace std; vo ...

  10. POJ P2096 Collecting Bugs

    思路 分类讨论,不妨先设$DP[i][j]$表示已经发现$i$种子系统中有$n$种$bug$无非只有四种情况 发现的$bug$在旧的系统旧的分类,概率$p1$是$(i/s)*(j/n)$. 发现的$b ...