给定一个段落 (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. Jeecms之查询实现

    现有一需求如下:     按时间段查询及留言状态(已回复,未回复,已审批)来查询留言.     当时的想法是这样子的,首先要把查询的条件通过页面传递到后台.于是在后台管理中找看有没有类似的功能,费了半 ...

  2. PHP数组循环遍历的四种方式

     1.使用for循环遍历数组     conut($arr);用于统计数组元素的个数.     for循环只能用于遍历,纯索引数组!!!!     如果存在关联数组,count统计时会统计两种数组的总 ...

  3. SpringBoot Jar应用Linux后台部署执行

    nohup java -jar shop-h5.jar > log_h5.file 2>&1 &

  4. 2.快速创建springboot项目 连pom文件里面的配置都不用配了

    无论是创建项目 还是module 模块 选择这个 .然后在后面的选择中选择自己要的功能 就可以把相关的依赖都加进去 省去了依赖 其后的写法跟第一篇一样 在这个项目下面有一个配置文件 ====>a ...

  5. CentOS 6 忘记root密码的修改方法

    1.Linux的root密码修改不像Windows的密码修改找回,Windows的登录密码忘记需要介入工具进行解决.CentOS6和CentOS7的密码方法也是不一样的,具体如下: 2.centos ...

  6. beanstalkd 消息队列发邮件

    放入消息 /** * 获取beanstalk实例 * * @staticvar resource|bool $beanstalk * @return resource */ function get_ ...

  7. Oracle基础知识:DECODE、NVL

    select 1 from PMADW.GET_WX_DATAPUSH_NEW_CHECK A INNER JOIN PMADW.V_EXCEPTION_QTY_MAIN B on DECODE( A ...

  8. 使用Jedis操作Redis-使用Java语言在客户端操作---hash类型

        我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.            所以该类型非常适合于存储值对象的信息.如Username.P ...

  9. Linux 中查询 CPU 的核数的方法

    以一台 Linux 服务器为例.这台 Linux 包括两颗 Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz CPU, 单颗 CPU 包括 10 个 cpu core ...

  10. axios简单的二次封装

    import axios from 'axios' import { Message} from 'element-ui' import store from '../store' //vuex im ...