[LeetCode]819. 最常见的单词
题目
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中,而且答案唯一。
禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
示例:
输入:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
输出: "ball"
解释:
"hit" 出现了3次,但它是一个禁用的单词。
"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。
注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"),
"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。
说明:
1 <= 段落长度 <= 1000.
1 <= 禁用单词个数 <= 100.
1 <= 禁用单词长度 <= 10.
答案是唯一的, 且都是小写字母 (即使在 paragraph 里是大写的,即使是一些特定的名词,答案都是小写的。)
paragraph 只包含字母、空格和下列标点符号!?',;.
paragraph 里单词之间都由空格隔开。
不存在没有连字符或者带有连字符的单词。
单词里只包含字母,不会出现省略号或者其他标点符号。
代码
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
vector<string> result;
//将大写全部改成小写
for(int i=0;i<paragraph.length();i++)
{
if(paragraph[i]>='A'&& paragraph[i]<='Z')
paragraph[i]=tolower(paragraph[i]);
}
//删除标点字符
for (auto begin=paragraph.begin();begin!=paragraph.end();begin++)
{
if (ispunct(*begin))
{
paragraph.erase(begin);
}
if (begin == paragraph.end())
break;
}
//将单词全部存储到一个vecotr中
int begin,end;
for(begin=0,end=0;end!=paragraph.length();end++)
{
if(paragraph[end]==' ')
{
string temp=paragraph.substr(begin,end-begin);
result.push_back(temp);
begin=end+1;
}
}
//最后一个单词加入
result.push_back(paragraph.substr(begin, end - begin));
//用map存储单词出现的次数
map<string,int> reMap;
for(auto i:result)
{
reMap[i]++;
}
//用map存储禁止的单词
map<string,int> banMap;
for(auto i:banned)
{
banMap[i]++;
}
string str;
int time=0;
//判断出现次数最多并且没有出现在禁止表中的单词
for(auto i:reMap)
{
if(i.second>time&&banMap[i.first]==0)
{
time=i.second;
str=i.first;
}
}
return str;
}
};
[LeetCode]819. 最常见的单词的更多相关文章
- Java实现 LeetCode 819 最常见的单词(暴力)
819. 最常见的单词 给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词. 题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁 ...
- 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 ...
- LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)
这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- C#LeetCode刷题之#819-最常见的单词(Most Common Word)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3969 访问. 给定一个段落 (paragraph) 和一个禁用单 ...
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [Swift]LeetCode819. 最常见的单词 | Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
随机推荐
- 学习ASP.NET Core Blazor编程系列八——数据校验
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- ESP32 IDF 获取天气信息
一.注册天气获取账号 我使用的知心天气,没有获取天气账号的小伙伴可以去注册一下,知心天气官网:https://www.seniverse.com/ 取得天气获取的API后,可以直接在浏览器中访问测试一 ...
- Codeforces Round #809 (Div. 2)C.Qpwoeirut And The City
题目大意: 当一栋楼比旁边两栋楼都高的时候,这栋楼为cool,对除了1和n以外的所有楼可以增加任意层,问在满足使最多的楼cool的前提下的花费最小. 当n为奇数的情况下: cool的楼实际上是固定的, ...
- DNS 解析 prefeath
本文将详细介绍DNS预解析prefetch的主要内容 概述 DNS(Domain Name System, 域名系统),是域名和IP地址相互映射的一个分布式数据库.DNS 查询就是将域名转换成 IP ...
- 【Virt.Contest】CF1215(div.2)
第二次打虚拟赛. CF 传送门 T1:Yellow Cards 黄色卡片 中规中矩的 \(T1\). 首先可以算出一个人也不罚下时发出的最多黄牌数: \(sum=a1*(k1-1)+a2*(k2-1) ...
- js和jquery页面初始化加载函数的方法及顺序
运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. <html> <head> <title>首页</title> <scri ...
- Go语言核心36讲51
你好,我是郝林,今天我们继续分享程序性能分析基础的内容. 在上一篇文章中,我们围绕着"怎样让程序对CPU概要信息进行采样"这一问题进行了探讨,今天,我们再来一起看看它的拓展问题. ...
- Go语言核心36讲37
你好,我是郝林,今天我们继续来分享并发安全字典sync.Map的内容. 我们在上一篇文章中谈到了,由于并发安全字典提供的方法涉及的键和值的类型都是interface{},所以我们在调用这些方法的时候, ...
- win 10 玩红警/黑边,不能全屏,闪退
win 10玩红警黑边问题 1.下载ddraw.dll,放在游戏目录 下载链接:ddraw.dll 如果提示 选择保留就行了 2.Win 键+S键,搜索注册表,打开这个 进去按这个路径 计算机\ ...
- IOS AND Android 配置Fiddler环境
下载:http://rj.baidu.com/soft/detail/10963.html?ald 运行Fiddler点击Tools: 选择设置选项: 1. 选择HTTPS新选项卡. 2. ...