问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. C#-WebForm-点击网页中的按钮后跳转到其他页面是怎么实现的?
  2. yii2 登录验证
  3. 夺命雷公狗-----React---27--小案例之react经典案例todos(清除已完成)
  4. dedecms /include/filter.inc.php Local Variable Overriding
  5. 基于Maven构建开发第一个Storm项目
  6. CAS 实现单点登录 .NET MVC
  7. 论文第4章:iOS绘图平台的实现
  8. JAVA的网络编程基础概念
  9. yum 安装 php5.6 和 mysql5.6
  10. PicklingError: Can&#39;t pickle &lt;type &#39;generator&#39;&gt;: it&#39;s not found as __builtin_
  11. JAVA中用于处理字符串的“三兄弟”
  12. 【struts2】ActionContext与ServletActionContext
  13. 201521123098 《Java程序设计》第10周学习总结
  14. win10下运行cmd闪退时检查方法
  15. Python3学习笔记17-类与实例
  16. 【转】myeclipse 自定义视图Customize Perspective 没有反应
  17. C语言指针的高级操作
  18. Python之旅:流程控制
  19. php curl 发送get和post请求示例
  20. tcp与http的区别

热门文章

  1. DEX文件解析--5、dex方法原型解析
  2. Ethical Hacking - GAINING ACCESS(22)
  3. 题解 CF786B 【Legacy】
  4. 抓取Android崩溃日志
  5. ffmpeg获取视频封面图片
  6. java 心跳机制
  7. circle踢人(约瑟夫环) c++
  8. Fortify Audit Workbench 笔记 Race Condition: Singleton Member Field 竞争条件:单例的成员字段
  9. PHP get_html_translation_table() 函数
  10. loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd