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 ...
随机推荐
- 用svg实现不规则形状
像这种弧形,用纯html和css很难写,但是用svg就简单多了. 可以用作图工具画出一个弧形,然后导成svg格式.在页面中,下面的白块就是div+svg构成 mixin svgCard(...cont ...
- int数组的下标为n
int 类型的数组 先定义int n = 5: 定义int a[n] = {0}; 编译怎么不通过啊?n必须为常量如果想实现你说的,应该用宏#define n 5int a[n]={0};这样就可以了 ...
- Highcharts 基本条形图;Highcharts 堆叠条形图;Highcharts 反向条形图
Highcharts 基本条形图 配置 chart 配置 设置 chart 的 type 属性 为 bar ,chart.type 描述了图表类型.默认值为 "line". var ...
- JavaScript---事件监听
JavaScript的事件监听是通过addEventListener()来实现的 它算是事件绑定的第二种方式. 他的特别之处在于这种绑定事件的方法不会被同名事件覆盖. 看具体的demo <!DO ...
- echarts在vue中使用的感悟
echarts在vue中使用的感悟 echarts作为图表展示的强大存在,每当使用后台系统,或多或少都会使用到,但是作为菜鸟的我,则是一路采坑,各种头大,比比皆是,为了避免下次再犯同样的错误,特意记录 ...
- laravel中上传图片之后图片的处理
$file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...
- laravel5.5中查询构造器的使用
//查询构造器新增数据: public function query1() { /* $bool=DB::table('student')->insert( ['name'=>'小李',' ...
- Java复习9网路编程
Java 复习9网路编程 20131008 前言: Java语言在网络通信上面的开发要远远领先于其他编程语言,这是Java开发中最重要的应用,可以基于协议的编程,如Socket,URLConnecti ...
- C++复习7.虚表的概念
C++ 类的虚表 20130929 关键技术:封装.继承.组合.虚函数.抽象基类.动态绑定.多态性等等 1.首先整理一下在阿里巴巴面试遇到的函数虚表的问题. 在C++中的Class中的函数式存储在Cl ...
- 十七、dbms_tts(检查表空间集合是否是自包含)
1.概述 作用:用于检查表空间集合是否是自包含的,并在执行了检查之后,将违反自包含规则的信息写入到临时表TRANSPORT_SET_VIOLATIONS中. 2.包的组成 1).transport_s ...