LeetCode – 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.
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的更多相关文章
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- [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 (最常见的单词)
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- [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 ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- [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 ...
- 【Leetcode_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
随机推荐
- Java的File.separator
一.File类 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还有 ...
- bzoj1010
题解: 斜率优化dp f[i]=f[j]+(i-j+sum[i]-sum[j]-L)^2 然后斜率优化 代码: #include<bits/stdc++.h> typedef long l ...
- RabbitMQ 设置消息的优先级
在RabbitMQ中,可以设置消息的优先级,也就相当于在队列中置顶某条消息,让某个消息优先得到处理的功能. 既然是设置消息的优先级,那么就是针对生产者,也就是消息发布端. 设置消息的优先级一共有2个步 ...
- Linux查看当前使用的网卡 以及 查看某进程使用的网络带宽情况 以及 端口占用的情况
一:Linux查看当前使用的网卡 ifconfig命令可以查看当前linux 系统有多少个网卡. [app@p2-app2 ~]$ ifconfig br-2e5b046a02d5: ...
- 愛拼才會贏--IPA--闽南语
闽南语经典曲目.
- Cracking The Coding Interview 4.6
//原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a b ...
- 打开和写入word文档
一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...
- `define、parameter、localparam三者的区别(转)
`define: 可以跨模块的定义,写在模块名称上面,在整个设计工程都有效.一旦‘define指令被编译,其在整个编译过程中都有效.例如,通过另一个文件中的`define指令,定义的常量可以被其他文件 ...
- django-restful风格
每一种求情都代表一种资源,它主要强调http应该一资源为中心,并且规范了url的风格, url:统一资源标志符,某一规定下能把资源独一无二的标示出来,好比每个人都有身份证号码. 它有四中对资源操作的请 ...
- Python 私有
class Person: __qie = "潘潘" # 类变量 def __init__(self, name, mimi): self.name = name self.__m ...