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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
閱讀 2431·2021-09-22 15:41
閱讀 1448·2021-08-19 10:54
閱讀 1755·2019-08-23 15:11
閱讀 3402·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