博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Most Common Word 最常见的词
阅读量:5789 次
发布时间:2019-06-18

本文共 2014 字,大约阅读时间需要 6 分钟。

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.

Example: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 !?',;.
  • Different words in paragraph are always separated by a space.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

这是一道字符串处理的题目,给我们一个字符串段落,以及一个被禁止的单词,段落中我们要处理!?',;.这些标点符号。我们可以算出各个单词出现的次数,然后将被禁止的单词排除在外,就可以算出出现次数最多的单词的哪一个。

解法一:

class Solution {public:    string mostCommonWord(string paragraph, vector
& banned) { map
M; map
::iterator it; M.clear(); int i,j,n=paragraph.size(),m=banned.size(); string s; for(i=0;i
='A'&&paragraph[i]<='Z')paragraph[i]+='a'-'A'; for(i=0;i
'z');j++); if(j==n)break; for(;j
='a'&&paragraph[j]<='z';j++)s+=paragraph[j]; M[s]++; } for(i=0;i
second>i) { s=it->first; i=it->second; } return s; }};

转载地址:http://kwqyx.baihongyu.com/

你可能感兴趣的文章
LeetCode36.有效的数独 JavaScript
查看>>
Scrapy基本用法
查看>>
PAT A1030 动态规划
查看>>
自制一个 elasticsearch-spring-boot-starter
查看>>
【人物志】美团前端通道主席洪磊:一位产品出身、爱焊电路板的工程师
查看>>
一份关于数据科学家应该具备的技能清单
查看>>
机器学习实战_一个完整的程序(一)
查看>>
Web框架的常用架构模式(JavaScript语言)
查看>>
如何用UPA优化性能?先读懂这份报告!
查看>>
这些Java面试题必须会-----鲁迅
查看>>
Linux 常用命令
查看>>
CSS盒模型
查看>>
ng2路由延时加载模块
查看>>
使用GitHub的十个最佳实践
查看>>
脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
查看>>
慎用!BLEU评价NLP文本输出质量存在严重问题
查看>>
JAVA的优势就是劣势啊!
查看>>
ELK实战之logstash部署及基本语法
查看>>
帧中继环境下ospf的使用(点到点模式)
查看>>
BeanShell变量和方法的作用域
查看>>