Cool说丨819

简介: 819. 最常见的单词

819. 最常见的单词

给定一个段落 (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 只包含字母、空格和下列标点符号!?',;.
  • 不存在没有连字符或者带有连字符的单词。
  • 单词里只包含字母,不会出现省略号或者其他标点符号。

第一版,有的没考虑到

输入:"a, a, a, a, b,b,b,c, c" ["a"]

输出:"bbbc"

预期:"b"

stringmostCommonWord(stringparagraph, vector<string>&banned) {

   stringword;

   intlen=paragraph.size(),max=1;

   unordered_map<string, int>res;

   for (inti=0; i<len; ++i) {

       if (paragraph[i] >='A'&&paragraph[i] <='Z') word+=paragraph[i] +'a'-'A';

       elseif (paragraph[i] >='a'&&paragraph[i] <='z') word+=paragraph[i];

       elseif (paragraph[i] ==' ') {//空格

           if (find(banned.begin(), banned.end(), word) ==banned.end()) {

               res[word] +=1;

           }

           word="";

       }

   }

   for (auto&a : res) {

       if (max<=a.second) {

           max=a.second;

           word=a.first;

       }

   }

   returnword;

   }

第二版,其实不难,也可以再优化一点

执行用时 :8 ms, 在所有 cpp 提交中击败了78.04%的用户

内存消耗 :8.6 MB, 在所有 cpp 提交中击败了97.33%的用户

   stringmostCommonWord(stringparagraph, vector<string>&banned) {

   stringword;

   intlen=paragraph.size(),max=1;

   unordered_map<string, int>res;

   for (auto&p:paragraph) {

       if (p>='A'&&p<='Z') word+=p+'a'-'A';

       elseif (p>='a'&&p<='z') word+=p;

       elseif(word!="") // && paragraph[i]==' '|| paragraph[i] == '!' || paragraph[i] == '?' || paragraph[i] ==  '\''|| paragraph[i] == ',' || paragraph[i] == ':' || paragraph[i] == '.'

       {

           if (find(banned.begin(), banned.end(), word) ==banned.end()) {

               res[word] +=1;

           }

           word="";

       }

   

   }

   for (auto&a : res) {

       //cout << a.first << " " << a.second << endl;

       if (max<=a.second) {

           max=a.second;

           word=a.first;

       }

   }

   returnword;

   }

第三版,这个反而更快,有点不可思议。。。

执行用时 :4 ms, 在所有 cpp 提交中击败了98.99%的用户

内存消耗 :8.6 MB, 在所有 cpp 提交中击败了100.00%的用户

   stringmostCommonWord(stringparagraph, vector<string>&banned) {

   stringword;

   intlen=paragraph.size(),max=1;

   unordered_map<string, int>res;

   for (autop:paragraph) {

       if (p>='A'&&p<='Z') word+=p+'a'-'A';

       elseif (p>='a'&&p<='z') word+=p;

       elseif(word!="")

       {

           if (find(banned.begin(), banned.end(), word) ==banned.end()) {

               res[word] +=1;

           }

           word="";

       }

   

   }

   for (autoa : res) {

       if (max<=a.second) {

           max=a.second;

           word=a.first;

       }

   }

   returnword;

   }


目录
相关文章
|
存储 关系型数据库 Java
数据COOL谈第3期
本文整理自阿里巴巴大淘宝技术部双12队长朱成(锡泽),阿里巴巴业务平台双11队长徐培德(裴度),阿里巴巴数据库双11队长陈锦赋(智盛),InfoQ主编王一鹏,在数据COOL谈第3期的分享。
|
C# C++
Cool说丨717与674
717. 1比特与2比特字符 674. 最长连续递增序列
42 0
|
自然语言处理 C# C++
Cool说丨970与720
970. 强整数 720. 词典中最长的单词
66 0
|
人工智能 BI C#
Cool说丨884与1207
[884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) [1207. 独一无二的出现次数](https://leetcode-cn.com/problems/unique-number-of-occurrences/)
56 0
|
C# C++ 索引
Cool说丨力扣387
387. 字符串中的第一个唯一字符
48 0
|
人工智能 BI C#
Cool说丨力扣718
[718. 最长重复子数组](https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/)
48 0
|
人工智能 BI C#
Cool说丨力扣697与888
697. 数组的度 888. 公平的糖果交换
53 0
|
算法 C# C++
Cool说丨力扣414与581
414. 第三大的数 581. 最短无序连续子数组
48 0
|
C# C++
Cool说丨力扣605
605. 种花问题
59 0
|
C# C++ 索引
Cool说丨力扣599
[599. 两个列表的最小索引总和](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/)
63 0