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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM