博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]819. Most Common Word 出现频率最高的non-banned单词
阅读量:5043 次
发布时间:2019-06-12

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

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.
  • 0 <= 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.

 

题意

求出现频率最高的non-banned单词

 

思路

HashMap

 

代码

class Solution {      public String mostCommonWord(String paragraph, String[] banned) {        String[] words = paragraph.replaceAll("\\pP" , " ").toLowerCase().split("\\s+");        Map
map = new HashMap<>(); for(String word : words){ if(map.containsKey(word)){ map.put(word, map.get(word) + 1); }else{ map.put(word, 1); } } for(String bword : banned) { if(map.containsKey(bword)){ map.remove(bword); } } String res = ""; for(String word : map.keySet()) if(res == "" || map.get(word) > map.get(res)) res = word; return res; }}

 

转载于:https://www.cnblogs.com/liuliu5151/p/11124121.html

你可能感兴趣的文章
Magento CE使用Redis的配置过程
查看>>
poi操作oracle数据库导出excel文件
查看>>
(转)Intent的基本使用方法总结
查看>>
Mac 下的Chrome 按什么快捷键调出页面调试工具
查看>>
Windows Phone开发(24):启动器与选择器之发送短信
查看>>
JS截取字符串常用方法
查看>>
Google非官方的Text To Speech和Speech Recognition的API
查看>>
stdext - A C++ STL Extensions Libary
查看>>
Django 内建 中间件组件
查看>>
bootstrap-Table服务端分页,获取到的数据怎么再页面的表格里显示
查看>>
进程间通信系列 之 socket套接字及其实例
查看>>
天气预报插件
查看>>
Unity 游戏框架搭建 (十三) 无需继承的单例的模板
查看>>
模块与包
查看>>
mysql忘记root密码
查看>>
apache服务器中设置目录不可访问
查看>>
嵌入式Linux驱动学习之路(十)字符设备驱动-my_led
查看>>
【NOIP模拟】密码
查看>>
java容器---------手工实现Linkedlist 链表
查看>>
three.js 性能优化的几种方法
查看>>