2018-10-26 00:32:05

问题描述:

问题求解:

方法一、Trie

最长出现的字符串,最容易想到的解法就是Trie树了,于是首先使用Trie树进行了实现,代码量有点大,当然了是可以A掉的,只是对于这种Easy的题,理论上是不该超过50行代码的。

public class MostCommonWord {
class TrieNode {
public TrieNode[] next = new TrieNode[26];
public int cnt = 0;
public String word = null;
} public String mostCommonWord(String paragraph, String[] banned) {
int[] maxCnt = new int[1];
String[] res = new String[1];
TrieNode root = buildTrie(paragraph, banned);
helper(root, maxCnt, res);
return res[0];
} private void helper(TrieNode root, int[] maxCnt, String[] res) {
if (root.cnt > maxCnt[0]) {
maxCnt[0] = root.cnt;
res[0] = root.word;
}
for (int i = 0; i < 26; i++) {
if (root.next[i] != null) helper(root.next[i], maxCnt, res);
}
} private TrieNode buildTrie(String s, String[] banned) {
Set<Character> set = new HashSet<>();
Set<String> b = new HashSet<>();
for (String i : banned) b.add(i);
set.add(' ');
set.add('!');
set.add('?');
set.add('\'');
set.add(',');
set.add(';');
set.add('.');
TrieNode root = new TrieNode();
String lowS = s.toLowerCase() + ' ';
char[] chs= lowS.toCharArray();
for (int i = 0; i < chs.length; i++) {
while (i < chs.length && set.contains(chs[i])) i++;
TrieNode cur = root;
for (int j = i; j < chs.length; j++) {
if (set.contains(chs[j])) {
cur.word = lowS.substring(i, j);
if (!b.contains(cur.word)) cur.cnt++;
i = j;
break;
}
if (cur.next[chs[j] - 'a'] == null) cur.next[chs[j] - 'a'] = new TrieNode();
cur = cur.next[chs[j] - 'a'];
}
}
return root;
} public static void main(String[] args) {
System.out.println('\'');
}
}

方法二、split

作为一条Easy必然是有简单解,但是还是有点tricky的,这里使用了正则的replaceAll函数来将其他字符转成” “,之后再split并统计即可。

    public String mostCommonWord(String paragraph, String[] banned) {
String[] strs = paragraph.replaceAll("[!?',;.]", " ").toLowerCase().split(" ");
Map<String, Integer> map = new HashMap<>();
Set<String> set = new HashSet<>();
for (String i : banned) set.add(i);
set.add("");
for (String s : strs) {
if (!set.contains(s)) {
int cnt = map.getOrDefault(s, 0);
map.put(s, ++cnt);
}
}
int maxCnt = 0;
String res = "";
for (String s : map.keySet()) {
if (map.get(s) > maxCnt) {
maxCnt = map.get(s);
res = s;
}
}
return res;
}

最常出现的字符串 Most Common Word的更多相关文章

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

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

  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_easy】819. Most Common Word

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

  4. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  5. leetcode-819-Most Common Word(词频统计)

    题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

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

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

  7. 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators

    backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...

  8. [Swift]LeetCode819. 最常见的单词 | Most Common Word

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  9. LeetCode – Most Common Word

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

随机推荐

  1. python之面向对象的高级进阶

    一 .isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object ...

  2. python简说(五)操作文件

    f = open('users.txt',encoding='utf-8') #读文件的时候,必须存在在才可以读 文件对象,或者文件句柄res = f.read()print(res)f.close( ...

  3. Eclipse中已安装的插件如何卸载

    最近在Eclipse中安装了一个插件,导致Eclipse使用的时候有些问题,就找了资料,原来Eclipse中的插件也是可以卸载的. 方法是点击菜单“Help”,“Install New Softwar ...

  4. JS事件覆盖问题和触发问题

    昨天遇到一个面试题,主要就是事件覆盖问题和触发问题 (不是打广告,无视文本内容) 总之这样的话,会输出三次“做自己的网站”. 为什么不是两次,而是输出三次呢? 1.首先onclick=function ...

  5. 对于ListView的一些用法(一)

    ScrollView:只能用于控件比较少的界面,如果数据有上千上万条,那么使用ScrollView就不好了,因为ScrollView就把所有的控件进行初始化,这是非常消耗性能的操作,所以android ...

  6. topcoder srm 370 div1

    problem1 link 枚举每一种大于等于$n$的计算其概率即可. problem2 link 首先二分答案,然后计算.令$f[i][j]$表示移动完前$i$最后一个在位置$j$的最小代价. pr ...

  7. CodeForces 459C Pashmak and Buses(构造)题解

    题意:n个人,k辆车,要求d天内任意两人都不能一直在同一辆车,能做到给出构造,不能输出-1 思路:我们把某一个人这d天的车号看成一个d位的数字,比如 1 1 2 3代表第一天1号车.第二天1号车.第三 ...

  8. CodeChef - MRO Method Resolution Order(打表)

    题意:有一种关系叫继承,那么继承父类的同时也会继承他的一个函数f,能继承任意多个父类或不继承,但不能继承自己的子类.现在规定一个列表,这个列表必须以1~N的顺序排列,并且父类不会排在子类后面,1含有一 ...

  9. (转) Supercharging Style Transfer

      Supercharging Style Transfer Wednesday, October 26, 2016 Posted by Vincent Dumoulin*, Jonathon Shl ...

  10. gulp结合webpack开启多页面模式,配置如下

    首先老规矩哈.全局包安装先 cnpm install webpack -g cnpm install gulp -g cnpm install babel -g //转换Es6 上面的整合在一起安装可 ...