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/

  • 相关阅读:
    【Android命令行】apktool参数详解
    【Android】ANR+OOM+FC
    如何创建低成本沙箱环境?推荐你使用API仿真!
    关于代码覆盖率,你不可不知的两大陷阱!
    如何借助自动创建单元测试来提高单元测试的投资回报率(ROI)?
    [实用指南]如何使您的旧代码库(遗留代码)符合MISRA C 2012编码规范?
    超实用的10个技巧!让您无论使用哪种静态分析工具都能轻松更新现有的静态分析实现
    主数据管理(MDM)的6大层级简述,你不可不知的数据治理参考!
    【收藏】关于元数据(Metadata)和元数据管理,这是我的见过最全的解读!
    机器学习 | 基于机器学习的供应链管理之销售库存优化分析(实操分享)
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/9000771.html
Copyright © 2011-2022 走看看