[LeetCode] 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.html

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:post

  • 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 !?',;.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

 

這道題給了咱們一個字符串,是一個句子,裏面有不少單詞,而且還有標點符號,而後又給了咱們一個相似黑名單功能的一個字符串數組,讓咱們在返回句子中出現的頻率最高的一個單詞。要求很是簡單明瞭,那麼思路也就簡單粗暴一些吧。由於咱們返回的單詞不能是黑名單中的,因此咱們對於每個統計的單詞確定都須要去黑名單中檢查,爲了提升效率,咱們能夠把黑名單中全部的單詞放到一個HashSet中,這樣就能夠常數級時間內查詢了。而後要作的就是處理一下字符串數組,由於字符串中可能有標點符號,因此咱們先過濾一遍字符串,這裏咱們使用了系統自帶的兩個函數isalpha()和tolower()函數,其實本身寫也能夠,就放到一個子函數處理一下也不難,這裏就偷懶了,遍歷每一個字符,若是不是字母,就換成空格符號,若是是大寫字母,就換成小寫的。而後咱們又使用一個C++中的讀取字符串流的類,Java中能夠直接調用split函數,叼的飛起。但誰讓博主執拗的寫C++呢,也無所謂啦,習慣就好,這裏咱們也是按照空格拆分,將每一個單詞讀出來,這裏要使用一個mx變量,統計當前最大的頻率,還須要一個HashMap來創建單詞和其出現頻率之間的映射。而後咱們看讀取出的單詞,若是不在黑名單中內,而且映射值加1後大於mx的話,咱們更新mx,而且更新結果res便可,參見代碼以下:url

 

class Solution {
public:
    string mostCommonWord(string paragraph, vector<string>& banned) {
        unordered_set<string> ban(banned.begin(), banned.end());
        unordered_map<string, int> strCnt;
        int mx = 0;
        for (auto &c : paragraph) c = isalpha(c) ? tolower(c) : ' ';
        istringstream iss(paragraph);
        string t = "", res = "";
        while (iss >> t) {
            if (!ban.count(t) && ++strCnt[t] > mx) {
                mx = strCnt[t];
                res = t;
            }
        }
        return res;
    }
};

 

參考資料:spa

https://leetcode.com/problems/most-common-word/code

https://leetcode.com/problems/most-common-word/discuss/124286/Clean-6ms-C%2B%2B-solutionhtm

https://leetcode.com/problems/most-common-word/discuss/123854/C%2B%2BJavaPython-Easy-Solution-with-Explanationblog

 

LeetCode All in One 題目講解彙總(持續更新中...)leetcode

相關文章
相關標籤/搜索