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. java热加载之springloaded

    https://github.com/spring-projects/spring-loaded/wiki

  2. java不常用但很有用的问题排查工具(持续完善)

    因为用的频率不是很多,老忘掉,每次都要搜下,特记录下备忘. 查看进程的启动jvm选项 [root@iZ23nn1p4mjZ ~]# jinfo -flags 16603Attaching to pro ...

  3. Python之字符编码(一)

    一.了解字符编码的知识储备? 1.计算机基础知识? 计算机中所有的软件文件(包括:操作系统)都存储在硬盘,启动计算机,计算机需要把系统文件都去到内存中. 2.文本编辑器存取文件的原理(nodepad+ ...

  4. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  5. Click()与Submit()

    <input type="button" /> 定义可点击的按钮,但没有任何行为.如果你不写javascript 的话,按下去什么也不会发生. button 类型常用于 ...

  6. error和exception有什么区别?

    Error(错误)表示系统级的错误和程序不必处理的异常,是java运行环境中的内部错误或者硬件问题.比如:内存资源不足等.对于这种错误,程序基本无能为力,除了退出运行外别无选择,它是由Java虚拟机抛 ...

  7. bsgs整理

    目录 bsgs问题 或 poj2417: 概述 代码 exbsgs 鸣谢 \(gzy gzy gzy\) bsgs问题 或 poj2417: 给定质数\(p\),给定\(a\),\(b\),\((a, ...

  8. LOJ121 「离线可过」动态图连通性

    思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...

  9. 深度学习课程笔记(十四)深度强化学习 --- Proximal Policy Optimization (PPO)

    深度学习课程笔记(十四)深度强化学习 ---  Proximal Policy Optimization (PPO) 2018-07-17 16:54:51  Reference: https://b ...

  10. Unity3D学习笔记(三十四):Shader着色器(1)

    一.GPU:图形处理器,Graphics Processing Unit 显卡的处理器就是图形处理器.与CPU类似.   GPU和CPU的区别? 1.CPU主要是为了串行指令设计,GPU则是为了大规模 ...