[抄题]:

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]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 是反斜杠不是正斜杠

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

去标点、去空格都用正则表达式

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 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 统计高频词(暂未被禁止)的更多相关文章

  1. 【Leetcode_easy】819. Most Common Word

    problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...

  2. 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 ...

  3. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

  4. LeetCode 819. Most Common Word

    原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...

  5. 819. Most Common Word

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. leetcode Most Common Word——就是在考察自己实现split

    819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...

  7. 基于统计的无词典的高频词抽取(二)——根据LCP数组计算词频

    接着上文[基于统计的无词典的高频词抽取(一)——后缀数组字典序排序],本文主要讲解高频子串抽取部分. 如果看过上一篇文章的朋友都知道,我们通过 快排 或 基数排序算出了存储后缀数组字典序的PAT数组, ...

  8. [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 ...

  9. 运用jieba库 寻找高频词

    一.准备 1.首先 先用cmd 安装 jieba库,输入 pip install jieba 2.其次 本次要用到wordcloud库和 matplotlib库,也在cmd输入pip install ...

随机推荐

  1. BZOJ - 5427:最长上升子序列 (二分&思维)

    现在给你一个长度为n的整数序列,其中有一些数已经模糊不清了,现在请你任意确定这些整数的值, 使得最长上升子序列最长.(为何最长呢?因为hxy向来对自己的rp很有信心)   Input 第一行一个正整数 ...

  2. python操作excel xlrd和xlwt的使用

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  3. Bootstrap-table学习笔记(一)

    第一次使用Bootstrap-table这个表格插件,记录一下使用过程中遇到的问题. =================== | 引入CSS文件 <link rel="styleshe ...

  4. 接口测试基础——第5篇xlrd模块

    读取Excel表格中的内容,不多说,直接上代码: # coding: utf-8 import xlrd # excel路径 excel_path = r'C:\Users\weiming\Deskt ...

  5. eclipse share project到svn时显示不被信任的证书,暂时接受也不行

    svn: 方法 OPTIONS 失败于 “https://eping.net/svn/testproject”: SSL handshake failed: SSL 错误:在证书中检测到违规的密钥用法 ...

  6. 从python爬虫引发出的gzip,deflate,sdch,br压缩算法分析

    今天在使用python爬虫时遇到一个奇怪的问题,使用的是自带的urllib库,在解析网页时获取到的为b'\x1f\x8b\x08\x00\x00\x00\x00...等十六进制数字,尝试使用chard ...

  7. 【ecmall】解决无法上传店铺logo和banner照片问题 (转)

    问题描述:在ecmall个人用户登录,点击用户中心-店铺设置-更换店标,上传一个jpg图像(图像符合规范)后,不能预览,而且在页面底部点击提交后没有上传成功.banner店铺条幅也是一样的情况,还有下 ...

  8. ASP.NET WebApi通过自定义ControllerSelector来自定义Controller的选择

    在web api中,我们可以通过给Cotroller类添加RoutePrefixAttribute来定义url与Controller之间的映射,但是有时候有一些特殊情况下,我们需要做一些特殊处理来将某 ...

  9. JAX-RS之queryparam、PathParam、DefaultValue、FormParam、Context、RestController等

    这几天做东西接触了JAX-RS的东西,没有系统的从开始就学,只是单纯去复制粘贴的用,主要用到了几个Annotations变量,具体如下: queryparam.PathParam.FormParam. ...

  10. 通过docker构建zabbix监控系统

    下载zabbix的镜像 $ docker pull berngp/docker-zabbix Using default tag: latest latest: Pulling from berngp ...