给定一个段落 (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. 事务一致性理解 事务ACID特性的完全解答

    A  原子性 事务管理者多个小操作,他们同时完成或者同时不完成就是原子性 C 一致性 一致性,是一个很相对的,很主观的概念, 一致性 描述的是 事务 从一个一致的状态变成 另一个一致的状态. 一致性需 ...

  2. java填坑记录

    一.The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the ...

  3. rdf(资源描述框架)

    资源描述框架(Resource Description Framework),一种用于描述Web资源的标记语言.RDF是一个处理元数据的XML(标准通用标记语言的子集)应用,所谓元数据,就是“描述数据 ...

  4. 009-python一些问题整理

    1. Python中的 // 与 / 的区别 " / "  表示浮点数除法,返回浮点结果 >>> 90/30 3.0 " // " 表示整数除 ...

  5. Spring cloud properties与yml配置说明

    encrypt说明 名称 默 认 描述 encrypt.fail-on-error true 标记说,如果存在加密或解密错误,进程将失败. encrypt.key 对称密钥.作为一个更强大的替代方案, ...

  6. 利用Qt自带工具发布程序

    Qt官方开发环境生成的exe发布方式--使用windeployqt 从开始菜单-->Qt 5.4.0-->5.4-->MinGW 4.9 (32-bit)-->Qt 5.4 f ...

  7. Frank Dellaert Slam Speech 20190708

    Georgia Institue of Tecknology 3D Models from Community Databases Spatiotemporal Reconstruction 4D C ...

  8. WPF:数据绑定--PropertyChangeNotification属性更改通知

      PropertyChangeNotification属性更改通知 实现效果:1.拍卖金额自动随属性值变化而通知界面绑定的值变化. 关键词 : INotifyPropertyChanged Obse ...

  9. IO流10 --- 缓冲流(字节型)实现非文本文件的复制 --- 技术搬运工(尚硅谷)

    字节型缓冲流,BufferedOutputStream默认缓冲区大小 8192字节byte,满了自动flush() @Test public void test6(){ File srcFile = ...

  10. hadoop-hive查询ncdc天气数据实例

    使用hive查询ncdc天气数据 在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果. 1. 在hive中创建ncdc表,这个 ...