给定一个段落 (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 只包含字母、空格和下列标点符号!?',;.
  • 不存在没有连字符或者带有连字符的单词。
  • 单词里只包含字母,不会出现省略号或者其他标点符号。
  1. class Solution {
  2. public:
  3. string mostCommonWord(string paragraph, vector<string>& banned) {
  4. map<string, int> check;
  5. int len = banned.size();
  6. for(int i = 0; i < len; i++)
  7. check[banned[i]] = 1;
  8. len = paragraph.size();
  9. map<string, int> save;
  10. string temp = "";
  11. int MAX = 0;
  12. string str = "";
  13. for(int i = 0; i < len; i++)
  14. {
  15. if(paragraph[i] >= 'a' && paragraph[i] <= 'z' || paragraph[i] >= 'A' && paragraph[i] <= 'Z')
  16. {
  17. if(paragraph[i] >= 'A' && paragraph[i] <= 'Z')
  18. temp += paragraph[i] + 'a' - 'A';
  19. else
  20. temp += paragraph[i];
  21. continue;
  22. }
  23. else
  24. {
  25. if(temp == "")
  26. continue;
  27. if(check[temp] == 1)
  28. {
  29. temp = "";
  30. continue;
  31. }
  32. save[temp]++;
  33. if(save[temp] > MAX)
  34. {
  35. MAX = save[temp];
  36. str = temp;
  37. }
  38. temp = "";
  39. }
  40. }
  41. if(temp != "")
  42. {
  43. if(check[temp] == 1)
  44. {
  45. return str;
  46. }
  47. save[temp]++;
  48. if(save[temp] > MAX)
  49. {
  50. MAX = save[temp];
  51. str = temp;
  52. }
  53. }
  54. return str;
  55. }
  56. };

Leetcode819.Most Common Word最常见的单词的更多相关文章

  1. 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 ...

  2. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  3. [LeetCode] Shortest Completing Word 最短完整的单词

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  4. leetcode Most Common Word——就是在考察自己实现split

    819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...

  5. leetcode-819-Most Common Word(词频统计)

    题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

  6. Aspose.Word 的常见使用(2018-12-26 更新版)

    Aspose.Word 的常见使用 起因 因项目需要,而且使用html转Word的时候,样式不兼容问题,于是只能使用Aspose.Word通过代码生成.下面是通过DocumentBuilder来设计W ...

  7. 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

    [058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 左神算法书籍《程序员代码面试指南》——1_10最大值减去最小值小于或等于num的子数组数量

    [题目]给定数组arr和整数num,共返回有多少个子数组满足如下情况:max(arr[i.j]) - min(arr[i.j]) <= num max(arfi.j])表示子数组ar[ij]中的 ...

  2. 关于mybatis-config.xml文件的基础解释

    今天是我第一天落户博客园,想一想从mybatis框架开始写起吧.mybatis框架与Hibernate框架相比来说,专注于SQL语句,对SQL语句的要求比较高吧. 我觉得,对于mybatis框架来说, ...

  3. hdu3853之概率dp入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xingyeyongheng/article/details/25205693 LOOPS Time ...

  4. kuangbin带我飞QAQ 最短路

    1. poj 1502 Mathches Game 裸最短路 #include <iostream> #include <string.h> #include <cstd ...

  5. 二进制日志过期时间设置expire_logs_days

    # expire_logs_days参数只支持整数,且范围是[0,99] show variables like 'expire_logs_days';set global expire_logs_d ...

  6. goland设置go build的工作目录

  7. js 表格合并

    1.合并 function autoRowSpan(tbid, row, col) { var tb = document.getElementById(tbid); var lastValue = ...

  8. 移动端canvas刮刮乐

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta cont ...

  9. cookie记录

    登录页面引用: <script src="/jquery.cookie.js"></script> 登录页面jq: var telphone = $('[n ...

  10. message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)

    原因是 文件名或文件夹名中不能出现以下字符:\   /   :   *   ?  "  <  >   | 但是后台读取到的附件的文件路径就是这样的 网上大佬说了,这样处理 rep ...