题目描述:

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 mostCommonWord(string paragraph, vector<string>& banned)

说明:

1、这道题目给定一个字符串,里面是一个句子,包含了字母(大小写都有)和空格、符号,还给了一个禁用词的vector(小写),要求我们对字符串里面的单词做词频分析,找到出现次数最多的单词,返回这个单词。

2、明白题意,这道题很容易做,也是一道工程类题目。

首先,对字符串中的字符逐个判断,如果是字母,转化为小写形式,记录位置为 i ,继续处理下一个,直到元素不是字母,记录位置 j ,把 i 到 j -1的子字符串放在vector中。

然后,对vector中的单词逐个判断,如果不是禁用词,那么累加次数。这里要使用set.count()来判断是不是禁用词,和map的数据结构来存储单词和对应的出现次数。

最后,遍历一遍map,不断更新出现的最大次数,顺便记录对应的元素,最终返回元素就可以了。

代码如下:

    string mostCommonWord(string paragraph, vector<string>& banned)
{
int s1=paragraph.size(),i=0,j;
vector<string>words;
while(i<s1)
{
if(isalpha(paragraph[i]))
{
j=i+1;
paragraph[i]=tolower(paragraph[i]);//转化为小写字母
while(isalpha(paragraph[j]))//j不断前进
{
paragraph[j]=tolower(paragraph[j]);
j++;
}
words.push_back(paragraph.substr(i,j-i));//提取子字符串,插入到vector中
i=(j+1);//更新i的值
}
else
i++;
}
set<string>banwords(banned.begin(),banned.end());//把禁用词vector转化为set,快速判断
map<string,int>wordnum;//定义一个map来存储单词和出现次数
for(auto word:words)//记录每个单词的出现次数
{
if(banwords.count(word)==0)
wordnum[word]++;
}
int max1=0;
string res;
for(map<string,int>::iterator iter=wordnum.begin();iter!=wordnum.end();iter++)
{
if(iter->second>max1)//不断更新max1和对应的单词
{
max1=iter->second;
res=iter->first;
}
}
return res;
}

上述代码实测7ms,因为服务器接收到的cpp submissions有限,所以没有打败的百分比。

leetcode-819-Most Common Word(词频统计)的更多相关文章

  1. 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 ...

  2. LeetCode 819. Most Common Word

    原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...

  3. 【Leetcode_easy】819. Most Common Word

    problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...

  4. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

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

    [抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

  6. 819. Most Common Word

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

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

    819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...

  8. 【原创】大数据基础之词频统计Word Count

    对文件进行词频统计,是一个大数据领域的hello word级别的应用,来看下实现有多简单: 1 Linux单机处理 egrep -o "\b[[:alpha:]]+\b" test ...

  9. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  10. 作业3-个人项目<词频统计>

    上了一天的课,现在终于可以静下来更新我的博客了.       越来越发现,写博客是一种享受.来看看这次小林老师的“作战任务”.                词频统计 单词: 包含有4个或4个以上的字 ...

随机推荐

  1. leetcode BFS解题思路

    Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...

  2. ECS 游戏架构 理解

    转载自:http://blog.csdn.net/i_dovelemon/article/details/25798677 理解 组件-实体-系统 (ECS \CES)游戏编程模型 - 博客频道   ...

  3. [模板]单源最短路径(Dijkstra)

    如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 主要还是再打一遍最短路,这种算法我用的不多... #include<bits/stdc++.h> using namesp ...

  4. cenots7单机安装Kubernetes

    关于什么是Kubernetes请看另一篇内容:http://www.cnblogs.com/boshen-hzb/p/6482734.html 一.环境搭建 master安装的组件有: docker ...

  5. aspnetcore的那些actionresult们

    比MVC5多了n个actionresult,傻傻分不清,整理了下,妈妈再也不用担心了 https://docs.asp.net/projects/api/en/latest/autoapi/Micro ...

  6. javascript总结22: javascript的对象--面向对象编程

    1 对象:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数. 对象与程序的关系: 程序 = 基于对象操作的算法 + 以对象为最小单位的数据结构 此外: 面向对象的本质就是让对象有多 ...

  7. KindEditor3.x整合教程-Xproer.WordPaster

    版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...

  8. 基于Xcode5的本地化

    一.程序名国际化   1.首先添加应用对多语言支持的国际化文件   点击工程根目录,然后选择PROJECT下的项目,然后选择Info选项卡,在底部可以看到Localizations,点击“+”号,可以 ...

  9. Ubuntu使用tzselect修改时区

    1.命令行运行 sudo tzselect 2.选择洲区(这里选择亚洲Asia) waichung@desktop:~$ sudo tzselect [sudo] password for waich ...

  10. 编写高质量代码改善C#程序的157个建议——建议136:优先使用后缀表示已有类型的新版本

    建议136:优先使用后缀表示已有类型的新版本 加后缀在某些情况下是很奇怪的形式,我们都不愿意看到OrderProcessor2这样的类型.但是,有的时候仍旧有必要这样做.最典型的是FCL中关于数字证书 ...