
LeetCode Most Common Word 最常见的词
发布日期:2025-04-05 01:11:48
浏览次数:11
分类:精选文章
本文共 3035 字,大约阅读时间需要 10 分钟。
Here is an optimized version of the thought process and solution:
Data Preparation
- Convert the entire paragraph to lowercase to handle case insensitivity.
- Remove all punctuation marks (such as commas, periods, exclamation points, etc.) to isolate words.
- Ensure words are properly separated by spaces to avoid partial words (e.g., "ball," becomes "ball").
Word Frequency Calculation
- Traverse the prepared string, extracting each word by ignoring punctuation and case differences.
- Use a hash map (dictionary) to count occurrences of each word.
- For each character in the paragraph: If it's a letter, add it to the current word being built. If it's not a letter or reaches the end of the string, finalize the word and update its count in the hash map.
Filter Banned Words
- Store banned words in a set for quick lookup.
- Iterate through the hash map to exclude any words that exist in the banned set, keeping only valid words.
Determine Most Frequent Word
- Sort the remaining words by their frequency in descending order.
- Return the first word in this sorted list, as it by definition is unique and has the highest count according to the problem constraints.
Final Solution Code
import java.util.HashMap;import java.util.HashSet;import java.util.Map;public class Solution { public String mostCommonWord(String paragraph, String[] banned) { // Convert paragraph to lowercase and remove punctuation StringBuilder cleanParagraph = new StringBuilder(); for (char c : paragraph.toCharArray()) { if (c >= 'a' && c <= 'z') { cleanParagraph.append(c); } } // Split into words String[] words = cleanParagraph.toString().split(" +"); // Count frequency of each word MapfrequencyMap = new HashMap<>(); for (String word : words) { frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1); } // Create banned words set for quick lookup HashSet bannedWords = new HashSet<>(); for (String bw : banned) { bannedWords.add(bw.toLowerCase()); } // Exclude banned words and find the most frequent int maxCount = -1; String result = ""; for (Map.Entry entry : frequencyMap.entrySet()) { if (!bannedWords.contains(entry.getKey())) { if (entry.getValue() > maxCount) { maxCount = entry.getValue(); result = entry.getKey(); } } } return result; }}
Explanation
- The code first processes the input paragraph to remove punctuation and convert it to lowercase, ensuring uniformity in word processing.
- It then splits the cleaned string into individual words and uses a hash map to count each word's occurrences.
- Banned words are stored in a set for quick exclusion.
- Finally, the code iterates through the frequency map, excluding banned words, and identifies the word with the highest count, which is then returned as the result.
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年04月23日 06时51分16秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LaTeX伪代码编辑
2025-04-04
Latex相关文章
2025-04-04
Laurent级数与奇点分析
2025-04-04
Layout Team
2025-04-04
layout_weight 的解释及使用
2025-04-04
layui 表单元素
2025-04-04
layui 表单提交不执行ajax的坑
2025-04-04
layui上传文件、图片
2025-04-04
layui中如何让多个控件在一行显示
2025-04-04
LayUI之CRUD
2025-04-04
layui图标使用和自定义矢量库图标
2025-04-04
layui数据表格自定义每页条数limit设置
2025-04-04
layui的upload组件使用和上传阻止
2025-04-04
layui简单入门
2025-04-04
Leaflet中使用leaflet.browser.print插件实现打印/导出为pdf
2025-04-04
Leaflet中使用Leaflet.contextmenu插件实现地图上添加鼠标右键菜单
2025-04-04
Leaflet中使用Leaflet.MagnifyingGlass实现放大镜效果
2025-04-04
leaflet军事标绘-直线箭头修改(leaflet篇.87)
2025-04-04
leaflet军事标绘-细直线箭头绘制(leaflet篇.82)
2025-04-04