[抄题]:

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. Hibernate中 一 二级缓存及查询缓存(2)

    缓存:缓存是什么,解决什么问题?  位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为缓存Cache.缓存目的:让数据更接近于应用程序,协调速度不匹配,使访问速度更 ...

  2. 剑指offer-第五章优化时间和空间效率(最小的k个数)

    题目:输入n个数,输出最小的k个数. 时间复杂度为O(n) 思路1:我们想的到的最直接的思路就是对这个N个数进行排序,然后就可以找到最小的k个了,同样可以用快排partition.但是只要找到前K个最 ...

  3. Django的CSRF机制

    原文链接:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 必须有的是: 1.每次初始化一个项目时,都能看到django.mi ...

  4. FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办?

    FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办? 有小伙伴提问 FastAdmin 在 CRUD 时出现 ...

  5. iframe添加点击事件

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 支付宝RSA签名

    1.参考网上相关文章,开放php中的openssl,但使用网上例子调用openssl_pkey_new,一直报100013错误.后改用用支付宝提供的SDKdemo程序 发现使用提供的privkye,可 ...

  7. photoshop画矩形款

    1.屏幕截图 2.在photoshop新建图形 3.用矩形选框 4.右键打开描边,宽度设成5个像素

  8. liunx基础(5)

    第十三单元 硬盘分区.格式化及文件系统的管理二 1. 文件系统的挂载与卸载(详见linux系统管理P406)1) 掌握挂载的定义:挂载指将一个设备(通常是存储设备)挂接到一个已存在的目录上.2) 掌握 ...

  9. linux 下安装php扩展

    linux下安装php扩展 步骤: 1.在扩展解压包目录执行 phpize 2.执行 ./configure --with-php-config=/usr/local/php/bin/php-conf ...

  10. FPGA中RAM使用探索

    FPGA中RAM的使用探索.以4bitX4为例,数据位宽为4为,深度为4. 第一种方式,直接调用4bitX4的RAM.编写控制逻辑对齐进行读写. quartus ii 下的编译,资源消耗情况. 85C ...