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% ...
随机推荐
- vue :没有全局变量的计数器
created: created () { let num = null this.mFun(num) }, methods: methods:{ mFun(m){ if (m === null) { ...
- 终于搞懂Spring中Scope为Request和Session的Bean了
之前只是很模糊的知道其意思,在request scope中,每个request创建一个新的bean,在session scope中,同一session中的bean都是一样的 但是不知道怎么用代码去验证 ...
- STL源码剖析:序
STL源码包含哪些内容 容器:STL的核心 适配器:容器都是在一种最底层的基础容器上使用适配器实现 空间配置器:提供内存的管理 迭代器:由于遍历容器中的数据 算法:由于操作容器中的数据,如排序,拷贝, ...
- 使用ImpromptuInterface反射库方便的创建自定义DfaGraphWriter
在本文中,我为创建的自定义的DfaGraphWriter实现奠定了基础.DfaGraphWriter是公开的,因此您可以如上一篇文章中所示在应用程序中使用它,但它使用的所有类均已标记为internal ...
- 05 ES6模块化规范基础详解
ES6模块规范 1.1 ES6规范说明 历史上,JavaScript 一直没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来.其他语言都有这项功能,比如 Ru ...
- 最简单的VScode Python 开发环境配置以及中文化
前置条件 Python 3.X(2020年了,建议使用Python3.X版本) 一.下载VSCode VSCode官方下载链接 由于安装过程是中文界面,此处略过. 二.VSCode中文化 不需要配置什 ...
- JVM系列之:对象的锁状态和同步
目录 简介 java对象头 java中锁状态的变化 偏向锁biased locking 轻量级锁thin lock 重量级锁 三种锁状态的不同 简介 锁和同步是java多线程编程中非常常见的使用场景. ...
- 仓库储存,存在添加减少,不存在插入ON DUPLICATE KEY UPDATE
原文链接:https://blog.csdn.net/qq_42269354/article/details/100589640 首先声明:ON DUPLICATE KEY UPDATE 为MySQL ...
- SpringBoot2 整合MinIO中间件,实现文件便捷管理
本文源码:GitHub·点这里 || GitEE·点这里 一.MinIO简介 1.基础描述 MinIO是一个开源的对象存储服务.适合于存储大容量非结构化的数据,例如图片.视频.日志文件.备份数据和容器 ...
- List接口(动态数组)
List接口(动态数组) List集合类中元素有序且可重复 ArrayList(重要) 作为List接口的主要实现类 线程不安全的,效率高 底层使用Object[] elementData数组存储 A ...