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

    题解: 吧询问变成前缀形式 然后莫队 代码: #include<bits/stdc++.h> ; using namespace std; ]; ,L=,R=; ,Ans[N]; bool ...

  2. fedora网络设置

    一:网络设置 1.找到要设置的网卡 命令:ip addr 列出所有的网络配置,找到你需要配置的网卡 入图,我这个是ens33 2.找到配置文件 配置文件路径: /etc/sysconfig/netwo ...

  3. 运行java程序的方法-DOS命令和Eclipse方法

    ● 运行java程序的方法(使用DOS命令) 首先进行一个"文件夹选项"的设置: 在D:\Android\java_code目录下新建了一个Hello_World.java文件(不 ...

  4. 深入理解java虚拟机---Class文件(二十)

    无符号数.表 当实现了不同语言的编译器,比如jython,jruby等等,那么就可以利用这些语言编写代码,通过各自的编译器编译成符合jvm规范的字节码文件,就可以利用jvm来执行了. Class文件在 ...

  5. Python 默认值字典

    from collections import defaultdict # 默认值字典 dd = defaultdict(lambda: "胡辣汤") # callable 可调用 ...

  6. Python 面向对象和面向过程对比

    # 大象装冰箱 # 脚本, 此时代码是最简单的. 不需要构思整个程序的概况 print("开门") print("装大象") print("关门&qu ...

  7. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  8. 二进制数值Byte [] 转Base64字符串

    将二进制数据转换成Base64字符串: String base64String = new String(byteArray).replaceAll("\n","&quo ...

  9. 服务器安装wordpress,搭建自己的博客平台

    自己构造网站的话,建立一个简单的网页还可以(比如,yongjieshi.com),对于建立复杂的博客就需要借助第三方的工具,常见的有wordpress,在阿里云上安装wordpress,我主要参考了这 ...

  10. SQLAlchemy中表结构的一对一

    1.先导入相对应的库 from flask import Flask from flask_sqlalchemy import SQLAlchemy import pymysql pymysql.in ...