819. Most Common Word

Posted jtechroad

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了819. Most Common Word相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    string mostCommonWord(string paragraph, vector<string>& banned) {
        unordered_set<string> s(banned.begin(), banned.end());
        unordered_map<string, int> m;
        int idx = 0;
        
        while (true) {
            string t = getLowerWord(paragraph, idx);
            if (t.length() == 0)    break;
            if (s.find(t) == s.end())
                m[t]++;
        }
        
        string res;
        int curmax = 0;
        for (const auto & it : m) {
            if (it.second > curmax) {
                curmax = it.second;
                res = it.first;
            }
        }
        return res;
    }
    string getLowerWord(const string &p, int &idx) {
        while (idx < p.length() && !isalpha(p[idx]))
            idx++;
        string res;
        while (idx < p.length() && isalpha(p[idx])) {
            res.push_back(tolower(p[idx]));
            idx++;
        }
        return res;
    }
};

 

以上是关于819. Most Common Word的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-819-Most Common Word(词频统计)

819. Most Common Word

[LeetCode] 819. Most Common Word

819. Most Common Word 统计高频词(暂未被禁止)

leetcode Most Common Word——就是在考察自己实现split

*LeetCode--Most Common Word