C#LeetCode刷题之#819-最常见的单词(Most Common Word)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3969 访问。
给定一个段落 (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 只包含字母、空格和下列标点符号!?',;.
- 不存在没有连字符或者带有连字符的单词。
- 单词里只包含字母,不会出现省略号或者其他标点符号。
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." banned = ["hit"]
Output: "ball"
Explanation: "hit" occurs 3 times, but it is a banned word."ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive,that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.
Note:
- 1 <= paragraph.length <= 1000.
- 1 <= banned.length <= 100.
- 1 <= banned[i].length <= 10.
- The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
- paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
- There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3969 访问。
public class Program {
public static void Main(string[] args) {
var paragraph = "Bob. hIt, baLl";
var banned = new string[] { "bob", "hit" };
var res = MostCommonWord(paragraph, banned);
Console.WriteLine(res);
Console.ReadKey();
}
private static string MostCommonWord(string paragraph, string[] banned) {
//转小写后,过滤非字符
//也可按题目给定的 !? ',;. 为非字符进行判定
var sb = new StringBuilder(paragraph.ToLower());
for(var i = 0; i < sb.Length; i++) {
if(!(sb[i] >= 'a' && sb[i] <= 'z') && !(sb[i] >= 'A' && sb[i] <= 'Z')) {
sb[i] = ' ';
}
}
//用字典统计次数
var dic = new Dictionary<string, int>();
var split = sb.ToString().Split(' '/*, StringSplitOptions.RemoveEmptyEntries*/);
foreach(var word in split) {
//过滤空值和ban列表中存在的值
if(word.Trim() == "") continue;
if(!banned.Contains(word)) {
if(dic.ContainsKey(word)) {
dic[word]++;
} else {
dic[word] = 1;
}
}
}
//输出最大值
return dic.OrderByDescending(d => d.Value).ToList()[0].Key;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3969 访问。
ball
分析:
显而易见,以上算法的时间复杂度为: 。
C#LeetCode刷题之#819-最常见的单词(Most Common Word)的更多相关文章
- #leetcode刷题之路30-串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置.注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考 ...
- C#LeetCode刷题之#14-最长公共前缀(Longest Common Prefix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3921 访问. 编写一个函数来查找字符串数组中的最长公共前缀. 如 ...
- [Swift]LeetCode819. 最常见的单词 | Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- LeetCode刷题预备知识(二)
Python四大数据结构的属性及方法 在LeetCode刷题预备知识一中我们掌握了常见的内置函数,和四大数据结构的基本概念: 但只掌握这些还远远不够,我们还需了解四大数据结构的属性及方法才能更高效快速 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
随机推荐
- 009.Nginx缓存及配置
一 浏览器缓存 1.1 缓存概述 缓存对于Web至关重要,尤其对于大型高负载Web站点.Nginx缓存可作为性能优化的一个重要手段,可以极大减轻后端服务器的负载.通常对于静态资源,即较少经常更新的资源 ...
- OSCP Learning Notes - Scanning(2)
Scanning with Metasploite: 1. Start the Metasploite using msfconsole 2. search modules 3.Choose one ...
- Python数据分析之全球人口数据
这篇文章用pandas对全球的人口数据做个简单分析.我收集全球各国1960-2019年人口数据,包含男女和不同年龄段,共6个文件. pop_total.csv: 各国每年总人口 pop_female. ...
- C++语法小记---多重继承
多重继承 工程中不建议使用多继承,因为多继承带来的问题比带来的便利多,已被放弃 问题一:多重继承的对象,向上获取指针时,有不同的地址 ----无法解决 问题二:菱形继承问题,导致成员冗余 ----虚继 ...
- elementUI form表单验证不通过的原因
<el-form :model="form" :rules="rules"> <el-form-item prop="input&q ...
- 切换npm源的几种方法
我们在使用官方提供的npm源安装各种依赖包的时候,下载速度会很慢,通常需要更换npm源. 我们可以在终端中输入命令 npm config list 来查看 npm 源地址,默认地址为 metrics- ...
- 001.Nginx简介
一 Nginx概述 1.1 Nginx简介 Nginx是一个高性能的HTTP和反向代理web服务器,Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在B ...
- web自动化 -- HTMLreport(一)测试报告自定义测试用例名,重写ddt
一.需求痛点 1.HTMLreport测试报告的用例名不明确 2.希望可以自定义HTMLreport测试报告的用例名 3.痛点截图 二.解决办法 1.原因分析 HTMLreport测试报告中的用例名是 ...
- ajax快速入门
一.ajax简单入门 1.Ajax的实现步骤 // 1.创建ajax对象var xhr = new XMLHttpRequest();// 2.高数ajax请求地址及请求方式//第一个参数就是请求方式 ...
- Tomcat内部结构、工作原理、工作模式和运行模式
TOMCAT的内部结构 Tomcat是一个基于组件的服务器,它的构成组件都是可配置的,其中最外层的是Catalina servlet容器,其他组件按照一定的格式要求配置在这个顶层容器中.Tomcat的 ...