[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 ...
随机推荐
- 一次 Redis 事务使用不当引发的生产事故
这是悟空的第 170 篇原创文章 官网:http://www.passjava.cn 你好,我是悟空. 本文主要内容如下: 一.前言 最近项目的生产环境遇到一个奇怪的问题: 现象:每天早上客服人员在后 ...
- Dapr实现.Net Grpc服务之间的发布和订阅,并采用WebApi类似的事件订阅方式
大家好,我是失业在家,正在找工作的博主Jerry,找工作之余,总结和整理以前的项目经验,动手写了个洋葱架构(整洁架构)示例解决方案 OnionArch.其目的是为了更好的实现基于DDD(领域驱动分析) ...
- 基于FPGA的AES加解密IP
Programmable AES Encryption/ Decryption IP 可编程AES加解密IP 可编程AES加解密IP提供了加解密算法功能,兼容美国国家标准与技术研究院(NIST)发布的 ...
- xss学习笔记(萌新版)
xss简介 xss攻击者构造恶意信息然后在用户的浏览器上执行,主要分为反射性xss,这种主要是某个页面存在有漏洞的参数,然后填上恶意参数把整个链接发给用户或者管理员,他们点击了带有恶意参数的链接就会执 ...
- 可编程渲染管线(Scriptable Render Pipeline, SRP)
原文链接 可编程渲染管线处理数据的流程可分为以下3大阶段 1. 应用阶段 这个阶段大概会由CPU处理4件事情.首先会对模型数据进行可见性判断.模型数据由顶点位置.法线方向.顶点颜色.纹理坐标等构成.然 ...
- Day03.1:初学者安装IDEA后需要知道的小技巧
初学者安装IDEA后需要知道的小技巧 1.输入psvm直接生成 main方法 2.输入sout可以直接生成输出语句 3.代码放大设置 4.注释颜色更改 5.代码字体大小通过Ctrl+鼠标滑轮控制的设置 ...
- Codeforces Round #832 (Div. 2) A~C题解
目录 A B C A 思路:这个题的话我们把负数和整数分别求出来,比较绝对值的大小,用较大的那个减去较小的那个就可以了. #include <cstring> #include <i ...
- docker使用代理(in home)
docker 使用 http http_proxy https://docs.docker.com/config/daemon/systemd/ # 代理 和 国内 镜像源 不要 同时使用,... # ...
- pytest文档82 - 用例收集钩子 pytest_collect_file 的使用
前言 pytest 提供了一个收集用例的钩子,在用例收集阶段,默认会查找test_*.py 文件或者 *_test.py文件. 如果我们想运行一个非python的文件,比如用yaml 文件写用例,那么 ...
- Codeforces Round #834 (Div. 3) A-G
比赛链接 A 题目 知识点:模拟. 确定开头字母,然后循环比较即可. 时间复杂度 \(O(n)\) 空间复杂度 \(O(n)\) 题解 #include <bits/stdc++.h> # ...