zoukankan      html  css  js  c++  java
  • 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 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.
    

    Note:

    • 1 <= paragraph.length <= 1000.
    • 1 <= banned.length <= 100.
    • 1 <= banned[i].length <= 10.
    • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
    • paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
    • Different words in paragraph are always separated by a space.
    • There are no hyphens or hyphenated words.
    • Words only consist of letters, never apostrophes or other punctuation symbols.

    题目标签:String

      题目给了我们一段paragraph,还有 banned words,让我们找出 在 paragraph 里出现最多的 non-banned word。

      主要思想是,开始要把paragraph 都变成小写,然后利用 regex split 成 string array;

      接着把banned words 放入 HashSet;

      把 non-banned words 放入 HashMap,在这一过程中,可以保留 出现次数最多的 word,不需要在多一个loop 来找出 max。

      !s.equals("") 的作用是因为 regex 会造成 “” 的情况,所以要跳过。

      具体请看code。

      

    Java Solution:

    Runtime beats  (no enough accepted submissions)% 

    完成日期:05/06/2018

    关键词:HashSet; HashMap; Regex

    关键点:用 Regex 来filter out string

     1 class Solution 
     2 {
     3     public String mostCommonWord(String paragraph, String[] banned) 
     4     {
     5         HashSet<String> bannedSet = new HashSet<>();
     6         HashMap<String, Integer> wordsMap = new HashMap<>();
     7         String mcw = "";
     8         int mcwCount = -1;
     9         
    10         // filter spaces and punctuation
    11         String[] wordsArr = paragraph.toLowerCase().split(" |!|\?|'|,|;|\."); 
    12         
    13         // put banned words into hash set
    14         for(String s: banned)
    15             bannedSet.add(s);
    16     
    17         // add and count non-banned words into wordsMap
    18         for(String s: wordsArr)
    19         {
    20             if(!bannedSet.contains(s) && !s.equals(""))
    21             {
    22                 wordsMap.put(s, wordsMap.getOrDefault(s, 0) + 1);
    23             
    24                 int count = wordsMap.get(s);
    25                 // keep tracking the most common word
    26                 if(count > mcwCount)
    27                 {
    28                     mcw = s;
    29                     mcwCount = count;
    30                 }
    31             }
    32             
    33         }
    34                 
    35         return mcw;
    36         
    37     }
    38 }

    参考资料:n/a

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    [转]WordPress 主题教程 #2:模板文件和模板
    [转]经验分享:微信小程序外包接单常见问题及流程
    [转]为什么软件开发,人多,事少,还会工作量大?
    [转]Reporting Service部署之访问权限
    [转]SQL Server 2008 如何配置报表管理器
    [转]Reporting Services 中的身份验证类型
    [转]Microsoft SQL SERVER 2008 R2 REPORT SERVICE 匿名登录
    [转]EasyUI 日期格式
    chartjs
    [转]分布式中Redis实现Session终结篇
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/9000771.html
Copyright © 2011-2022 走看看