Problem
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 !?",;. There are no hyphens or hyphenated words. Words only consist of letters, never apostrophes or other punctuation symbols.Solution
class Solution { public String mostCommonWord(String paragraph, String[] banned) { String[] strs = paragraph.toLowerCase().replaceAll("[!?",;.]+", " ").split(" "); Setset = new HashSet<>(Arrays.asList(banned)); Map map = new HashMap<>(); for (String str: strs) { if (!set.contains(str)) { map.put(str, map.getOrDefault(str, 0)+1); } } int max = 0; String res = ""; for (Map.Entry entry: map.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); res = entry.getKey(); } } return res; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71946.html
摘要:读取文件内容,正则去除非中英文字符,正则去除非中英文字符筛选出所有英文单词筛选出所有英文单词筛选出所有的中文筛选出所有的中文如果参数为空,则按照从高频到低频依次全部打印打印频率最高的五个字符反序输出打印大于等于小于指定值的转化成取得 import collections import re #读取tips.txt文件内容,type(mytips)=str with open(ti...
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...
Problem Given a set of words (without duplicates), find all word squares you can build from them. A sequence of words forms a valid word square if the kth row and column read the exact same string, wh...
摘要:一题目描述空格分隔,逐个反转二题目描述三题目描述当然也可以用的做,不过用双指针更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 题目描述 Given a string, you need to reverse the order of chara...
阅读 2430·2021-09-22 15:41
阅读 1448·2021-08-19 10:54
阅读 1755·2019-08-23 15:11
阅读 3401·2019-08-23 10:23
阅读 1428·2019-08-22 16:28
阅读 799·2019-08-22 15:11
阅读 739·2019-08-22 14:53
阅读 710·2019-08-22 13:49