题目


给定一个段落 (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. 最常见的单词的更多相关文章

  1. Java实现 LeetCode 819 最常见的单词(暴力)

    819. 最常见的单词 给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词. 题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁 ...

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

  3. LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)

    这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...

  4. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  5. C#LeetCode刷题之#819-最常见的单词(Most Common Word)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3969 访问. 给定一个段落 (paragraph) 和一个禁用单 ...

  6. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  7. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  8. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  9. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  10. [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 ...

随机推荐

  1. 齐博x2自建流媒体RTMP直播服务器

    这里只讲解大家最容易配置的Windows版,测试环境是2008版服务器及WIN7下载下面的软件,解压在任何目录都可,然后双击"启动.bat"即可http://down.php168 ...

  2. 10.异步mysql

    python中操作mysql连接.操作.断开都是网络IO #安装支持异步aiomysql的模块 pip3 install aiomysql async def execute(): # 网络IO操作, ...

  3. vulnhub靶场之CORROSION: 2

    准备: 攻击机:虚拟机kali.本机win10. 靶机:CORROSION: 2,网段地址我这里设置的桥接,所以与本机电脑在同一网段,下载地址:https://download.vulnhub.com ...

  4. Codeforces Round #805 (Div. 3)E.Split Into Two Sets

    题目链接:https://codeforces.ml/contest/1702/problem/E 题目大意: 每张牌上面有两个数字,现在有n张牌(n为偶数),问能否将这n张牌分成两堆,使得每堆牌中的 ...

  5. 【jmeter】将“察看结果树”中的数据保存到本地

    操作说明: 1. "察看结果树"页面,[配置]导出项: 2. "察看结果树"页面,[文件名]选框输入导出文件及路径:  3. 点击jmeter[启动]按钮,响应 ...

  6. Go语言核心36讲09

    从本篇文章开始,我们正式进入了模块2的学习.在这之前,我们已经聊了很多的Go语言和编程方面的基础知识,相信你已经对Go语言的开发环境配置.常用源码文件写法,以及程序实体(尤其是变量)及其相关的各种概念 ...

  7. Android网络请求(3) 网络请求框架OkHttp

    Android网络请求(3) 网络请求框架OkHttp 本节我们来讲解OkHtpp网络请求框架 什么是网络请求框架 在我的理解中,网络请求框架是为了方便我们更加便捷规范的进行网络请求所建的类,我们通过 ...

  8. python进阶(29)单例模式

    初识单例模式 单例模式含义 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在.许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整 ...

  9. ES文件传输助手1.0.0

    软件下载地址 1.软件功能 与 ES文件浏览器 的快传功能 直接传输文件 支持接受文件点击预览 可以多台电脑使用该软件,从而实现电脑与电脑局域网互传文件 单个文件夹上传会递归上传该文件夹下所有文件夹与 ...

  10. ubuntu1804搭建FTP服务器的方法

    搭建FTP服务器 FTP的工作原理: FTP:File Transfer Protocol ,文件传输协议.属于NAS存储的一种协议,基于CS结构. ftp采用的是双端口模式,分为命令端口和数据端口, ...