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 ...
随机推荐
- shlve 模块
shlve 模块 也用于序列化 它与pickle 不同之处在于 不需要惯性文件模式什么的 直接把它当成一个字典来看待 它可以直接对数据进行修改 而不用覆盖原来的数据 而pickle 你想要修改只能 ...
- 验证码 kaptcha 参数详解
Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and optio ...
- dapper 简单多表查询
public List<Book> GetBookList() { List<Book> bList = null; try { using (var t = new SqlC ...
- windows 访问局域网共享文件
直接在浏览器或资源管理器输入路径就OK file://10.16.73.129/FinTech/soft
- 创建自己的docker基础镜像
1.下载镜像 centos7 docker pull centos: 2.创建容器加载镜像 docker run -i -t --name centos7 centos: docker run 参数详 ...
- 编程实现Linux系统的od功能
选做题目以及分析 题目:编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 分析:我觉得这道题目中的参数应当是-tx1而不是-tx,使用了-tx后结 ...
- day 32 子进程的开启 及其用法
开启两种子进程的两种方式# # # 1 传统方式# from multiprocessing import Process# import time# def task(name):# print ( ...
- 4-log4j2之切分日志文件
一.添加maven依赖 <dependencies> <dependency> <groupId>org.apache.logging.log4j</grou ...
- 常见无线DOS攻击
记录下自己最近一段时间对无线渗透学习的笔记. 无线DOS就是无线拒绝服务攻击.主要包括以下几种攻击类型:Auth Dos攻击.Deauth Flood攻击.Disassociate攻击及RF干扰攻击等 ...
- BT觀念分享和常見問題彙整
一. TCP/IP基本觀念 1. IP : 每台在TCP/IP網路上的電腦必須具備的一個代表號或一個地址.IP又分為private IP(192.168.x.x /10.x.x.x /172.16.x ...