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;
    }
};

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

时间: 2024-11-09 03:27:29

819. Most Common Word的相关文章

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

(Easy) Most Common Word - LeetCode

Description: 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 bann

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

UVA 10100 Longest Match

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1041 LCS类型的题,不过并不是找common character,而是common word.就先把string处理成a list of word,然后再用LCS算法求common word. 代码如下: 1 #include <iostr

mysql 全文搜索的FULLTEXT

FULLTEXT索引 创建FULLTEXT索引语法 创建table的时候创建fullText索引 CREATE TABLE table_name( column1 data_type, column2 data_type, column3 data_type, - PRIMARY_KEY(key_column), FULLTEXT key key_name (column1,column2,..) ); 在已存在的table上面创建fullText索引 ALTER TABLE table_nam

小白日记5:kali渗透测试之被动信息收集(四)--theHarvester,metagoofil,meltag,个人专属密码字典--CUPP

1.theHarvester theHarvester是一个社会工程学工具,它通过搜索引擎.PGP服务器以及SHODAN数据库收集用户的email,子域名,主机,雇员名,开放端口和banner信息. 注:一般需要FQ #可使用proxychains指定代理 [email protected]:~# theharvester -h ******************************************************************* * * * | |_| |__

漫谈 Clustering (2): k-medoids

上一次我们了解了一个最基本的 clustering 办法 k-means ,这次要说的 k-medoids 算法,其实从名字上就可以看出来,和 k-means 肯定是非常相似的.事实也确实如此,k-medoids 可以算是 k-means 的一个变种. k-medoids 和 k-means 不一样的地方在于中心点的选取,在 k-means 中,我们将中心点取为当前 cluster 中所有数据点的平均值: Rough Collie 并且我们已经证明在固定了各个数据点的 assignment 的情

Zipf’s Law

Let f(w) be the frequency of a word w in free text. Suppose that all the words of a text are ranked according to their frequency, with the most frequent word first. Zipf’s Law states that the frequency of a word type is inversely proportional to its

关于MySQL的FULLTEXT实现全文检索的注意事项

对于英文,MySQL的FULLTEXT属性来实现全文检索是相当方便且高效的,但是在使用过程中需要注意一些事项. 首先对我们对需要进行检索的字段添加FULLTEXT属性(假设已经建表): SQL: alter table table_name add fulltext index(filed_1,filed_2); 接下来查询数据: SQL:  SELECT * FROM table_name WHERE MATCH (filed_1,filed_2) AGAINST ('keyword'); 此