博客
关于我
LeetCode Most Common Word 最常见的词
阅读量:801 次
发布时间:2023-01-31

本文共 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        Map
    frequencyMap = 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.

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

    你可能感兴趣的文章
    LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
    查看>>
    localhost:5000在MacOS V12(蒙特利)中不可用
    查看>>
    logstash mysql 准实时同步到 elasticsearch
    查看>>
    Luogu2973:[USACO10HOL]赶小猪
    查看>>
    mabatis 中出现&lt; 以及&gt; 代表什么意思?
    查看>>
    Mac book pro打开docker出现The data couldn’t be read because it is missing
    查看>>
    MAC M1大数据0-1成神篇-25 hadoop高可用搭建
    查看>>
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MariaDB的简单使用
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>