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.

  

class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
if(paragraph == null || paragraph.length() == 0){
return "";
} String[] strs = paragraph.split(" ");
int max = 0;
String res = "";
Map<String, Integer> map = new HashMap<>();
for(String str : strs){
char[] chars = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
if(c != '!' && c != '?' && c != ',' && c != ';' && c != '.'){
sb.append(Character.toLowerCase(c));
}
}
if(!map.containsKey(sb.toString())){
boolean isBanned = false;
for(String ban : banned){
if(ban.equals(sb.toString())){
isBanned = true;
break;
}
}
if(!isBanned){
if(max == 0){
max = 1;
res = sb.toString();
}
map.put(sb.toString(), 1);
}
}
else{
if(map.get(sb.toString())+1 > max){
max = map.get(sb.toString())+1;
res = sb.toString();
}
map.put(sb.toString(), map.get(sb.toString())+1);
}
}
return res;
}
}

  

LeetCode – Most Common Word的更多相关文章

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

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

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

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

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

  4. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

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

  6. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

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

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

  9. 【Leetcode_easy】819. Most Common Word

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

随机推荐

  1. 红黑树与AVL

     红黑树和avl树都属于自平衡二叉树: 两者查找.插入.删除的时间复杂度相同: 包含n个内部结点的红黑树的高度是o(logn); TreeMap是一个红黑树的实现,能保证插入的值保证排序       ...

  2. day06字典类型

    基本使用: 1.用途:用来存多个(不同种类的)值 2定义方式:在{}内用逗号分隔开多个key:value的元素,其中value可以是任意数据类型,而key的功能通常是用来描述value的,所以key通 ...

  3. Uva 11520 - Fill the Square 贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  4. 安装Adobe Acrobat XI Pro

    从网上下载Adobe Acrobat XI Pro这款软件,下载后将其解压到我们的电脑上,然后找到setup.exe双击安装它,安装时选择“使用试用版本或订阅” 2 选择“自定义”   自定义安装组件 ...

  5. U启动安装原版Win7系统教程

    1.制作u启动u盘启动盘2.下载原版win7系统镜像并存入u盘启动盘3.硬盘模式更改为ahci模式 第一步: 将准备好的u启动u盘启动盘插在电脑usb接口上,然后重启电脑,在出现开机画面时通过u盘启动 ...

  6. Docker容器使用jenkins部署web项目--总结(二)

    (1)需要安装Docker容器,在Docker容器内安装jenkins,gogs,tomcat.   新建maven项目,添加findbugs plugin. 使用docker启动jenkins,go ...

  7. hustoj 管理员和后台设置

    之前用过hustoj 的livecd版本,觉得有一些小问题,所以从头到尾搭建.主要包含的过程包括: 安装ubuntu系统 搭建hustoj 管理员和后台资源建设 本文介绍如何在搭建好hustoj的基础 ...

  8. 用mysql存储过程代替递归查询

    查询此表某个id=4028ab535e370cd7015e37835f52014b(公司1)下的所有数据 正常情况下,我们采用递归算法查询,如下 public void findCorpcompany ...

  9. Spring学习六(事物管理)

    参考链接 http://www.mamicode.com/info-detail-1248286.html http://www.cnblogs.com/wangdaqian/archive/2017 ...

  10. python day 06 作业