博客
关于我
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/

    你可能感兴趣的文章
    mysqldump的一些用法
    查看>>
    mysqli
    查看>>
    MySQLIntegrityConstraintViolationException异常处理
    查看>>
    mysqlreport分析工具详解
    查看>>
    MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
    查看>>
    Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
    查看>>
    mysql_real_connect 参数注意
    查看>>
    mysql_secure_installation初始化数据库报Access denied
    查看>>
    MySQL_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>
    MySQL、HBase 和 Elasticsearch:特点与区别详解
    查看>>
    MySQL、Redis高频面试题汇总
    查看>>
    MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
    查看>>
    mysql一个字段为空时使用另一个字段排序
    查看>>
    MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
    查看>>
    MYSQL一直显示正在启动
    查看>>
    MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
    查看>>
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>