惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
月光博客
月光博客
爱范儿
爱范儿
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
腾讯CDC
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
U
Unit 42
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
L
LangChain Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
LeetCode 819: Most Common Word — Simple Explanation with ...
Jerin · 2026-05-06 · via DEV Community

Jerin

Difficulty: Easy

Topics: Array, Math, Hash Table, String, Counting

Platform: Leetcode

Problem Statement

Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

The words in paragraph are case-insensitive and the answer should be returned in lowercase.

Note that words can not contain punctuation symbols.

Problem Statement Simplified
If in the paragraph string have the word in banned array, then discard it. If the paragraph string have ‘.’ or ‘ ’ then remove it. Make everything lowercase. Then give back the word that is most frequent.

Mistakes and Learning
Forgetting about removing ‘.’ or ‘ ’.
Look out for rare cases (ex:[a.])

Example 1

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.

Example 2

Input: paragraph = "a.", banned = []
Output: "a"

Key Insight

Initialize a HashMap and HashSet for paragraph String and banned array.
If not a word from banned array then add to HashMap and increment the counter
If a word from banned array then move to next word.

Algorithm

  1. Remove everything from string other than [a-z] and make it lowercase.
  2. Initialize a String array and add the string words to it using split function.
  3. Initialize HashMap and HashSet for string array and banned word array respectively.
  4. Initialize an int max and string maxWord to check and return the max repeating word.
  5. Initialize a for loop and add the banned array to HashSet. end for loop
  6. Initialize a for loop.
  7. Store the current word in String array to a string.
  8. If not a word in HashSet.
  9. Put in HashMap and increment the value if not already present.
  10. If value is greater than max.
  11. Assign the value to max.
  12. Assign word to maxWord.
  13. end if.
  14. end if
  15. end for loop.
  16. return max word.

Algorithm in simple words

First, remove everything from string other than [a-z] and make it lowercase. Then split the string and store it in an array.
Initialize a HashMap to store the string and integer - so that we can just look at this hashmap and check how many times the string repeated. everytime we loop through the string array (we created earlier), we will store the words in hashmap and assign a value to it which will increment when the word is repeated in the array.

Initialize a HashSet to store the string — so that the banned word array can be stored and compared with hashmap.

Initialize an int max and string maxWord to check and return the max repeating word.

Initialize a for loop which will go thorugh the banned words array and add them to HashSet.

Then we will loop through the string array and check the HashSet and current word from String array. If same then we will move to next word from String array. If not then we will add it to HashMap and if the word already exists then we will increment the value of it ( using map.getOrDefault(word,0) it will return the current value, if the word doesnt exists then it wil return 0 otherwise the current value of that word from HashMap ).

Then we will check of this value is greater than max, if yes then we will set this value as new max and this word as new maxWord.

Java code

class Solution {
    public String mostCommonWord(String paragraph, String[] banned) {
        paragraph= paragraph.toLowerCase().replaceAll("[^a-z]"," ");
        String[] words= paragraph.split("\\s+");
        Map<String,Integer> map= new HashMap<>();
        Set<String> bannedWord=new HashSet<>();
        int counter=0;
        int max=-1;
        String maxWord="";
        for (int i=0;i<banned.length;i++){
            bannedWord.add(banned[i].toLowerCase());
        }
        for(int i=0;i<words.length;i++){
             String word=words[i];
            if(!bannedWord.contains(word)){
            map.put(word,map.getOrDefault(word,0)+1);

            if(map.get(word)>max){
                max=map.get(word);
                maxWord=word;
            }
            }
        }
        return maxWord;

    }
}

Enter fullscreen mode Exit fullscreen mode

Time & Space Complexity

Time Complexity: O(n+m)

Space Complexity: O(n+m)