最常出现的字符串 Most Common Word
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的更多相关文章
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- 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_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
- [LeetCode] 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(词频统计)
题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- [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 ...
- LeetCode – Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
随机推荐
- eclipse maven Errors while generating javadoc on java8
With JDK 8, we are unable to get Javadoc unless your tool meets the standards of doclint. Some of it ...
- 20145208 蔡野 《网络对抗》Exp6 信息搜集与漏洞扫描
20145208 蔡野 <网络对抗>Exp6 信息搜集与漏洞扫描 本实践的目标是掌握信息搜集的最基础技能.具体有(1)各种搜索技巧的应用(2)DNS IP注册信息的查询 (3)基本的扫描技 ...
- linux --- 3 vim 网络 用户 权限 软连接 压缩 定时任务 yum源
一.vi 和vim vi 是老式的字处理器,不过功能已经很齐全了,但是还是有可以进步的地方. vim 则可以说是程序开发者的一项很好用的工具 ①命令模式 移动光标 w(e) 移动光标到下一个单词 b ...
- 启动Activiti项目报错:org.activiti.engine.ActivitiObjectNotFoundException: no deployed process definition found with id '22501'
背景 启动activiti项目时,出现错误org.activiti.engine.ActivitiObjectNotFoundException: no deployed process defini ...
- Python3 tkinter基础 Canvas create_line 画实线与虚线
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 全排列+字符串查找|扑克排序|2014年蓝桥杯A组题解析第六题-fishers
标题:扑克序列 A A 2 2 3 3 4 4, 一共4对扑克牌.请你把它们排成一行. 要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌. 请填写出所有符合要求的排 ...
- POJ 3903 Stock Exchange(LIS || 线段树)题解
题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...
- 【2.0新特性】Spring Boot 2.0新特性
以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...
- 论操作系统的IO
论事件驱动与异步IO - 简书 https://www.jianshu.com/p/814c7e7c4647
- 深度学习课程笔记(十四)深度强化学习 --- Proximal Policy Optimization (PPO)
深度学习课程笔记(十四)深度强化学习 --- Proximal Policy Optimization (PPO) 2018-07-17 16:54:51 Reference: https://b ...