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 ...
随机推荐
- LeetCode第[91]题(Java):Decode Ways(解码方法数)
题目:解码方法数 难度:Medium 题目内容: A message containing letters from A-Z is being encoded to numbers using the ...
- 基于事件的 JavaScript 编程:异步与同
JavaScript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程.然而,对于初学者来说,书写异步代码可能会比较困难.而在这篇文章里,我将会 ...
- 如何在 Ubuntu 中安装 QGit 客户端
QGit是一款由Marco Costalba用Qt和C++写的开源的图形界面 Git 客户端.它是一款可以在图形界面环境下更好地提供浏览版本历史.查看提交记录和文件补丁的客户端.它利用git命令行来执 ...
- CentOS 7添加应用快捷方式到桌面
以eclipse为例,编辑下面文件,复制到桌面即可. vi client.desktop [Desktop Entry]Encoding=UTF-8Name=eclipseExec=/home/clo ...
- Linux SSH隧道技术(端口转发,socket代理)
动态转发(SOCKS5代理): 命令格式:ssh -D <local port> <SSH Server> ssh -fnND 0.0.0.0:20058 172.16.50. ...
- day27 CRM delete& action& 嵌入CRM
课程目录:deleteactionpop up window嵌入crm项目 权限(未讲)学员交作业发邮件 代码路径:https://github.com/liyongsan/git_class/tre ...
- 【Error】 : make 不是内部或外部命令,也不是可运行的程序
之前有段源码需要编译,一开始选择在Windows上编译,由于没有安装VS,只能采取Make + Gcc 的方式,虽然后来还是在ubuntu上编译的,但是遇到的问题还是要记录下来. 虽然我也把make的 ...
- IRC BOT原来是利用IRC下发C&C命令——在xx云环境遇到了,恶意软件开的是6666端口
Backdoor/IRC.RpcBot 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Backdoor/IRC.RpcBot是一些批处理文件.脚本文件和执行文件的集合,也是 ...
- 快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23033 Accepted: 10612 Descri ...
- Docker的安装及操作
1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get update 安装包允许apt通过HTTPS使用仓库 sudo apt-get install \ a ...