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核心技术(四)-----Docker、数据访问、自定义starter
			Docker 1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使 用这个镜 ... 
- 转:步步LINUX C--进程间通信(二)信号
			源地址:http://blog.csdn.net/jmy5945hh/article/details/7529651 linux间进程通信的方法在前一篇文章中已有详细介绍.http://blog.cs ... 
- 结构体的sort排序
			结构体用sort快排的方法 struct node{ int k,s; }p[]; bool cmp1(node x,node y){ return x.s>y.s; //定义降序排序(从大到小 ... 
- 初探.NET CORE WEB API(RESTful风格)
			前面有4篇系列博客 (一)Asp.net web api中的坑-[找不到与请求 URI匹配的 HTTP 资源] (二)Asp.net web api中的坑-[http get请求中的参数] (三)As ... 
- HTML5:使用Canvas和Input range控件放大缩小图片,剪裁,并上传图片
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ... 
- JasperReport报表设计4
			在JRXML模板(或JRXML文件)中的JasperReport 都是标准的 XML文件,以.JRXML扩展.所有JRXML文件包含标签<jasperReport>,作为根元素.这反过来又 ... 
- Sql Server实现自动增长
			在学习中遇到这个问题 数据库里有编号字段 BH00001 BH00002 BH00003 BH00004 如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号. --得 ... 
- 同一浏览器中同一JavaWeb程序不共享session方法
			版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/b2084005/article/details/302227351.要求 在使用struts1开发J ... 
- 一些linux"基本操作"的教程汇总
			linux有些操作不用经常忘掉,如果忘掉还要到处找教程,所以想干脆汇总一些写的比较好的教程,免得下次再去找(完全个人向) 1. guake terminal http://blog.csdn.net/ ... 
- 威胁快报|ProtonMiner挖矿蠕虫扩大攻击面,加速传播
			背景 近日,阿里云安全监测到一种挖矿蠕虫,正在互联网上加速传播.阿里云安全根据它使用ProtonMail邮箱地址作为矿池用户名的行为,将其命名为ProtonMiner.据分析,这种蠕虫与TrendMi ... 
