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. 火车时刻表WebApp

    关键词 :Ajax 跨域访问 php 同源策略 JQueryMobile 前言 在面试的过程中,兄弟连的徐老师提出要求我用JQuery Mobile(前端框架)来实现一个具有“火车时刻表”功能的Web ...

  2. 如何使用AsyncTask

    1 如何使用handler,安卓规定只能再UI线程里面刷新UI,但是不能再UI线程里面执行耗时操作.所以我们要把耗时操作放在子线程里,然后把要刷新UI的操作传递到handler里面,然后在由Handl ...

  3. Flutter学习指南:UI布局和控件

    Flutter学习指南:UI布局和控件 - IT程序猿  https://www.itcodemonkey.com/article/11041.html

  4. 比赛总结——atcoder beginner contest 109

    第一次AK的ABC 虽然题非常简单 但是值得纪念一下 T1 一道很水的题 不存在做法 纯粹乱跑 但是我把Yes打成YES了,哭唧唧 #include <cstdio> #include & ...

  5. 题解——Codeforces Round #508 (Div. 2) T1 (模拟)

    依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...

  6. (转) How a Kalman filter works, in pictures

    How a Kalman filter works, in pictures I have to tell you about the Kalman filter, because what it d ...

  7. (转) Dissecting Reinforcement Learning-Part.2

    Dissecting Reinforcement Learning-Part.2 Jan 15, 2017 • Massimiliano Patacchiola 原文链接:https://mpatac ...

  8. ZooKeeper分布式过程协同技术详解1——ZooKeeper的概念和基础

    简介 分布式系统和应用,不仅能提供更强的计算能力,还能为我们提供更好的容灾性和扩展性. ZooKeeper是Google的Chubby项目的开源实现,它曾经作为Hadoop的子项目,在大数据领域得到广 ...

  9. Git误操作 git reset强制回滚 恢复commit方法

    参考: 找回Git中丢失的Commit Git误操作 git reset强制回滚 恢复commit方法 使用Git时,常有误操作,在Commit之后又执行了git reset --hard HEAD强 ...

  10. jquery选择器扩展之样式选择器

    https://github.com/wendux/style-selector-jQuery-plugin http://blog.csdn.net/duwen90/article/details/ ...