[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 ...
随机推荐
- Laravel-Easy-Admin 快速搭建数据后台 web管理后台
基于PHP + Laravel + element-admin-ui 搭建的快速数据后台,只在解决系列后台增删改查等日常操作.快速搭建,在生成业务的同时可以花更多的时间关注技术本身,提高程序员自身进阶 ...
- ElasticSearch之Windows中环境安装
ElasticSearch 说明 本章,我们主要以在 Windows 中对ElasticSearch 安装进行介绍! 1. 下载 ElasticSearch 这里我们下载的版本为7.17.4为例进行介 ...
- RDD(弹性分布式数据集)及常用算子
RDD(弹性分布式数据集)及常用算子 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是 Spark 中最基本的数据 处理模型.代码中是一个抽象类,它代表一个 ...
- mybatis-增删改查和配置
加入log4j日志功能 加入依赖 <!-- log4j日志 --> <dependency> <groupId>log4j</groupId> < ...
- 设置CMD命令的初始目录
本文提供两种方式,第一种通过打开桌面上的快捷方式实现,第二种可以通过 CMD 命令直接进入. 快捷方式 "开始 ---> 程序 ---> 附件",右键单击 " ...
- Java计算文件或文件夹大小
导入此类后,直接调用FilesUtil.returnSizi(String path),即可获取文件或者文件夹大小. 代码: 1 /** 2 * 路人甲操作文件的工具类 3 * returnSizi( ...
- ubuntu上升级cmake到3.16版本
本来cmake的旧版本是2.8.12.2,现在更新到3.16.0版本. 需要文件:cmake 3.16.0压缩包,在附件. 1. 查看cmake版本:cmake --version 2. 解压cm ...
- django启动报错:DisallowedHost at /
学习django第一天,第一次启动服务就报错,报错内容如下: DisallowedHost at / Invalid HTTP_HOST header: '192.168.116.22:8000'. ...
- 图文详解在VMware Workstation 16 PRO虚拟机上安装Ubuntu 22.04.5 linux系统
一.下载Ubuntu linux系统镜像 机构 下载地址 官网地址 https://cn.ubuntu.com/download 南京大学 https://mirrors.nju.edu.cn/ubu ...
- .NET周报【11月第4期 2022-11-30】
国内文章 .NET 7 的 AOT 到底能不能扛反编译? https://www.cnblogs.com/huangxincheng/p/16917197.html 在B站,公众号上发了一篇 AOT ...