819. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 是反斜杠不是正斜杠
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
去标点、去空格都用正则表达式
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- Arrays.asList()返回的是List,而且是一个定长的List。把string数组转成普通数组,才能存到hashset中。
public static void main(String[] args){
2 int[] a1 = new int[]{1,2,3};
3 String[] a2 = new String[]{"a","b","c"};
4
5 System.out.println(Arrays.asList(a1));
6 System.out.println(Arrays.asList(a2));
7 }
打印结果如下:
[[I@dc8569]
[a, b, c]
删除标点、划分空格:反斜杠
String[] words = p.replaceAll("\\pP" , "").toLowerCase().split("\\s+");
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
//ini
Set<String> set = new HashSet<>(Arrays.asList(banned));
Map<String, Integer> map = new HashMap<>();
String res = "";
int max = Integer.MIN_VALUE;
//store in HM
String[] words = paragraph.replaceAll("\\pP", "").toLowerCase().split("\\s+");
for (String w : words) {
if (!set.contains(w)) {
map.put(w, map.getOrDefault(w, 0) + 1);
if (map.get(w) > max) {
res = w;
max = map.get(w);
}
}
}
//return
return res;
}
}
819. Most Common Word 统计高频词(暂未被禁止)的更多相关文章
- 【Leetcode_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
- 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】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- LeetCode 819. Most Common Word
原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...
- 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 ...
- 基于统计的无词典的高频词抽取(二)——根据LCP数组计算词频
接着上文[基于统计的无词典的高频词抽取(一)——后缀数组字典序排序],本文主要讲解高频子串抽取部分. 如果看过上一篇文章的朋友都知道,我们通过 快排 或 基数排序算出了存储后缀数组字典序的PAT数组, ...
- [LeetCode] Top K Frequent Words 前K个高频词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- 运用jieba库 寻找高频词
一.准备 1.首先 先用cmd 安装 jieba库,输入 pip install jieba 2.其次 本次要用到wordcloud库和 matplotlib库,也在cmd输入pip install ...
随机推荐
- E: Could not get lock /var/lib/dpkg/lock解决
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决 通过终端安装或卸载程序sudo apt-get install/autoremove xxx时出 ...
- java 线程基础学习
今天趁空闲时间看了点线程方面的知识 首先看的是volatile关键字,按照我之前书上看到的一点知识,自己的理解是,volatile关键字会阻止编译优化,因为cpu每次读取数据是并不是从高速缓存中读取, ...
- HihoCoder 1068 RMQ-ST算法+BIT
以前都是用的BIT或者线段树(前者多一些). 对于ST(Sparse Table),在求倍增or公共祖先(LCA)时见过,说明还有其他用处,所以还是学习一下. 首先是预处理,用动态规划(DP)解决. ...
- 如何加快MyEclipse的启动速度
学习java开发的朋友对Myeclipse应该不陌生,MyEclipse企业级工作平台(MyEclipseEnterprise Workbench ,简称MyEclipse)是对EclipseIDE的 ...
- bzoj 2734 集合选数
Written with StackEdit. Description <集合论与图论>这门课程有一道作业题,要求同学们求出\(\{1, 2, 3, 4, 5\}\)的所有满足以 下条件的 ...
- AGC006 C Rabbit Exercise——思路(置换)
题目:https://agc006.contest.atcoder.jp/tasks/agc006_c 选了 i 位置后 x[ i ] = x[ i-1 ] + x[ i+1 ] - x[ i ] . ...
- 16.Selenium+Python关于句柄的小Demo
前言:有些链接点击之后,会重新打开一个窗口,对于这种情况,就要切换窗口了,获得窗口的唯一标识是用句柄(handle) 代码如下所示: from selenium import webdriver dr ...
- laravel前后端分离的用户登陆 退出 中间件的接口与session的使用
在项目开发的过程中,需要有用户的登陆 退出 还有校验用户是否登陆的中间件; 基本思路: 登陆: 前端请求接口的参数校验 用户名 密码规则的校验 用户名密码是否正确的校验; 如果上面的校验都通过的了,把 ...
- 平台调用之如何利用VS2013 C#调试C++DLL库
对于托管代码调用非托管DLL文件,已经是非常普遍的事情,下面写一下如何通过托管代码(C#)像调试托管代码一样调试DLL中的代码. 注意:(1)[dll工程和调用dll的exe工程需要在同一个解决方案中 ...
- java从键盘输入一组数据,输出其最大值,平均值,最小值没法输出
总结::需要耐心,加思考.做事不思考,那就是白做徒劳!!!!! package com.aini; import java.util.Scanner; //操...为什么数组的大小比较我硬是搞不懂,比 ...