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
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
题解:
遇到不是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 ...
随机推荐
- python中如何剔除字符串
问题: 过滤用户输入中前后多余的空白字符 ‘ ++++abc123--- ‘ 过滤某windows下编辑文本中的’\r’: ‘hello world \r\n’ 去掉文本中unicode组 ...
- 分布式锁tair redis zookeeper,安全性
tair分布式锁实现:https://yq.aliyun.com/articles/58928 redis分布式锁:https://www.cnblogs.com/jianwei-dai/p/6137 ...
- NPOI:初次操作(新建Excel)
1. 由于在某些电脑上没有安装office,或者有权限限制,使用COM组件进行读写Excel的话会出现问题, 为此,NPOI是一个很好的选择,NPOI可以在上述环境中满足Office的操作需求,并且功 ...
- JS中,如何判断一个数是不是小数?如果是小数,如何判断它是几位小数??
<script type="text/javascript"> var x = 4.23323;//测试的数字 var y = String(x).in ...
- 【nynu】 妹妹的工资怎么算(二分)
题目链接:http://47.93.252.151/problem.php?id=1148 题目描述 <我的妹妹哪有这么可爱!>中的女主叫做高坂桐乃,高坂家的幺女,外表出众.成绩优秀.运动 ...
- log4cpp安装
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 解决xadmin下设置“use_bootswatch = True”无效的问题
环境:python 2.7django 1.9xadmin采用源代码的方式引入到项目中QQ群交流:697028234 1.安装requests pip install requests 2./xadm ...
- vim 插件 for gbasic
https://github.com/tracyone/vim-gbasic 功能特点 提供正确语法显示,包括关键字,bulidin函数,注释,强调注释,TODO注释 提供正确的折叠; 准确secti ...
- Runtime获取类的属性列表和方法列表
Runtime获取类的属性列表和方法列表 Runtime很强大,他使得OC中没有真正意义上的私有属性和私有方法,我们可以利用OC的运行时拿到一个类的任何方法和任何属性,然后动态的去调用方法,objc_ ...
- U盘做了一个启动盘来安装Ubuntu,装好后,U盘不能进行格式化了,现在说一下网上找的方法
参考网址:http://wenwen.sogou.com/z/q289778573.htm 说是这种情况需要对U盘进行低级格式化,具体方法如下: 你可以尝试使用diskpart命令 ① 以管理员身份运 ...