LeetCode 819. Most Common Word
原题链接在这里:https://leetcode.com/problems/most-common-word/description/
题目:
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
paragraphmay have uppercase symbols, and even if it is a proper noun.) paragraphonly consists of letters, spaces, or the punctuation symbols!?',;.- Different words in
paragraphare always separated by a space. - There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols
题解:
遇到不是letter的char就把当前采集到的词频率加一. 如果高于目前最大频率就更换res.
Note: paragraph 本身末位加个符号. 否则会丢掉最后一个词.
Time Complexity: O(m+n). m = paragraph.length(). n = banned.length.
Space: O(m+n).
AC Java:
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
HashSet<String> hs = new HashSet<String>(Arrays.asList(banned));
paragraph += '.';
String res = "";
int count = 0;
HashMap<String, Integer> hm = new HashMap<String, Integer>();
StringBuilder sb = new StringBuilder();
for(char c : paragraph.toCharArray()){
if(Character.isLetter(c)){
sb.append(Character.toLowerCase(c));
}else if(sb.length() > 0){
String s = sb.toString();
if(!hs.contains(s)){
hm.put(s, hm.getOrDefault(s, 0)+1);
if(hm.get(s) > count){
res = s;
count = hm.get(s);
}
}
sb = new StringBuilder();
}
}
return res;
}
}
LeetCode 819. Most Common Word的更多相关文章
- 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_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 819. Most Common Word 统计高频词(暂未被禁止)
[抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- 819. Most Common Word
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
随机推荐
- CSS元素隐藏的11种方法
{ display: none; /* 不占据空间,无法点击 */ } { visibility: hidden; /* 占据空间,无法点击 */ } { position: absolute; cl ...
- http & https & http2.0
一.http状态码 1xx(信息性状态码,接受的请求正在处理) 2xx(成功状态码,请求正常处理完毕)200 OK204 No Content:请求成功但没有资源返回206 Partial Conte ...
- 开机启动服务(ftp、apache、mysql)
当linux服务器开机时,会将 /etc/rc.d/rc.local 中的指令全部执行一遍, 因此将相应服务的启动指令放到该shell脚本中即可实现开机启动效果; 在 /etc/rc.d/rc.loc ...
- 揭开A*算法的神秘面纱
揭开A*算法的神秘面纱 一.总结 一句话总结:f(n)=g(n)+h(n) 这个算法有点像BFS的优化算法. g(n)为起点到当前方格的距离,这个是已知的. h(n)为当前方格到终点的距离,这个简单点 ...
- Dir命令
注: 此系列为自己之前所搭建网站内容. 其实python的os模块能够很好的完成此任务.改天总结下. 之前在处理气象数据时,十几个文件,文件名比较长,需要自己处理变动的年份找出地址的规律再进行文件的读 ...
- CSS技巧和经验
如何清除图片下方出现几像素的空白间隙 方法1 img { display: block; } 方法2 除了top值,还可以设置为text-top | middle | bottom | text-bo ...
- ftp的匿名用户的搭建
在搭建之前需要server端安装vsftpd用yum装就好,客户端直接装ftp就ok yum装的vsftpd直接就有共享目录,在/var/ftp/pub 目录,看下目录,给他降权,将属主,属组改为ft ...
- bzoj2241
题解: 暴力枚举锤子大小 然后前缀和判断是否可行 代码: #include<bits/stdc++.h> #define N 105 using namespace std; int m, ...
- MySQL Block Nested-Loop Join(BNL)
5.5 版本之前,MySQL本身只支持一种表间关联方式,就是嵌套循环(Nested Loop).如果关联表的数据量很大,则join关联的执行时间会非常长.在5.5以后的版本中,MySQL通过引入BNL ...
- 【javascript基础】 广告嵌套document.write的非iframe方式的延迟加载
用ControlJS优化阿里妈妈广告http://ued.taobao.com/blog/2011/03/controljs-alimama/让document.write的广告无阻塞的加载http: ...