Leetcode819.Most Common Word最常见的单词
给定一个段落 (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最常见的单词的更多相关文章
- 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] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- [LeetCode] Shortest Completing Word 最短完整的单词
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- leetcode-819-Most Common Word(词频统计)
题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- Aspose.Word 的常见使用(2018-12-26 更新版)
Aspose.Word 的常见使用 起因 因项目需要,而且使用html转Word的时候,样式不兼容问题,于是只能使用Aspose.Word通过代码生成.下面是通过DocumentBuilder来设计W ...
- 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...
- [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 ...
- [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 ...
随机推荐
- springboot核心技术(一)-----入门、配置
Hello World 1.创建一个maven工程:(jar) 2.导入spring boot相关的依赖 <parent> <groupId>org.springframewo ...
- jeecms框架单点登录功能的实现
单点登录的功能实现主要原理: 1: 在点击登录按钮的时候使用reponse.addCookie()方法向浏览器发送cookie: 2: 在前段拦截器中的request.getCookie()在接收到设 ...
- PAT甲级——A1017 Queueing at Bank
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- 封装原生JavaScript的ajax
function obj2str(data) { data = data || {}; // 如果没有传参, 为了添加随机因子,必须自己创建一个对象 data.t = new Date().getTi ...
- Eureka自我保护机制、健康检查的作用、actuator模块监控
在上一篇文章微服务入门之服务的注册以及服务之间的调用中,我们基本实现了服务之间的调用,今天我们来了解一下Eureka自我保护机制以及健康检查. Eureka自我保护机制 接着以上篇文章建立的三个工程为 ...
- npm install时出现error
今天启动vue前端时,发现依赖没了.于是乎cmd->npm install->更新了部分依赖后,出现error信息,提示更新依赖失败.很奇怪,原来这个项目都是好的,为啥突然更新下来依赖了呢 ...
- TZOJ 4651 zry和他的的灯泡们(lca)
描述 zry有一个收集灯泡的习惯,他把灯泡的阴极都共地,阳极连成一颗树,这样的话,他只要在任意一个灯泡的阳极加上合适的电压,所有灯泡都能亮起来.不幸的是,有一对灯泡之间的阳极连线断掉了,这样的话,这颗 ...
- 02Redis入门指南笔记(基本数据类型)
一:热身 获得符合规则的健名列表:keys pattern pattern支持glob风格的通配符,具体规则如下表: Redis命令不区分大小写.keys命令需要遍历Redis中的所有健,当键的数量 ...
- Django项目:CRM(客户关系管理系统)--29--21PerfectCRM实现King_admin查看页面美化
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- iview中table的render()函数
Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...