给定一个段落 (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 只包含字母、空格和下列标点符号!?',;.
  • 不存在没有连字符或者带有连字符的单词。
  • 单词里只包含字母,不会出现省略号或者其他标点符号。
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
map<string, int> check;
int len = banned.size();
for(int i = 0; i < len; i++)
check[banned[i]] = 1;
len = paragraph.size();
map<string, int> save;
string temp = "";
int MAX = 0;
string str = "";
for(int i = 0; i < len; i++)
{
if(paragraph[i] >= 'a' && paragraph[i] <= 'z' || paragraph[i] >= 'A' && paragraph[i] <= 'Z')
{
if(paragraph[i] >= 'A' && paragraph[i] <= 'Z')
temp += paragraph[i] + 'a' - 'A';
else
temp += paragraph[i];
continue;
}
else
{
if(temp == "")
continue;
if(check[temp] == 1)
{
temp = "";
continue;
}
save[temp]++;
if(save[temp] > MAX)
{
MAX = save[temp];
str = temp;
}
temp = "";
}
}
if(temp != "")
{
if(check[temp] == 1)
{
return str;
}
save[temp]++;
if(save[temp] > MAX)
{
MAX = save[temp];
str = temp;
}
}
return str;
}
};

Leetcode819.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] 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] Shortest Completing Word 最短完整的单词

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

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

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

  5. leetcode-819-Most Common Word(词频统计)

    题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

  6. Aspose.Word 的常见使用(2018-12-26 更新版)

    Aspose.Word 的常见使用 起因 因项目需要,而且使用html转Word的时候,样式不兼容问题,于是只能使用Aspose.Word通过代码生成.下面是通过DocumentBuilder来设计W ...

  7. 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

    [058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...

  8. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

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

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

随机推荐

  1. SQLServer-Version:SQLServer版本对应内部数据库版本号配置表

    ylbtech-SQLServer-Version:SQLServer版本对应内部数据库版本号配置表 1.返回顶部 1. 1.1 查询SQLServer对应的内部数据库版本号select DATABA ...

  2. 封装MySQL C API 基本操作

    根据我的以前的文章 http://blog.csdn.net/skyhuangdan/article/details/21099929 链接数据库成功后进行封装. 我封装类使用的是VS2005下的wi ...

  3. Redis 核心

    一.Redis单机多实例原理 每个实例对应不同的配置文件,配置文件对应不同的端口.数据库文件位置.日志位置. 二.Redis单实例多数据库 每个Redis实例都有16个数据库,下标从0-15,当 se ...

  4. 2019-2-19-win10-uwp-客户端如何发送类到-asp-dotnet-core-作为参数

    title author date CreateTime categories win10 uwp 客户端如何发送类到 asp dotnet core 作为参数 lindexi 2019-2-19 9 ...

  5. Jupyter notebook使用matplotlib不出图解决办法

    1.在jupyter notebook使用plot的时候没有显示图像2.在命令行知道需要使用ipython --pylab进入ipython环境才能做出图像,jupyter notebook该怎么设置 ...

  6. 【转载】TSN简介及相关资源

    原文:https://blog.csdn.net/u012692537/article/details/86188392 一.简介 1.1 什么是TSN TSN(Time Sensitive Netw ...

  7. PHP学习1.5-预定义超全局数组变量

    1.PHP 预定义的超全局变量数组 特性: a.特殊的数组,操作方式没有区别 b.不用声明,php脚本中默认存在,因为在php中不用定义,所以在自定义变量是应避免和预定的全局变量同名 c.在全局范围内 ...

  8. Linq 之 Where操作

    适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形式.关系条件形式 ...

  9. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  10. grpc安装

    整个过程就是: 1.客户端 发送 数据(以字节流的方式) 2.服务端接收,并解析. 根据 约定 知道要知道执行什么.然后把结果返回客户端 RPC就是 把 1.上述过程封装下,使其操作更加优化 2.使用 ...