码迷,mamicode.com
首页 > 其他好文 > 详细

819. Most Common Word

时间:2018-11-20 01:30:16      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:order   fir   ==   code   max   rap   int   cond   red   

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

标签:order   fir   ==   code   max   rap   int   cond   red   

原文地址:https://www.cnblogs.com/JTechRoad/p/9986753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!