Code Logo

Most Common Word

Published at24 Jul 2026
Easy 0 views
Like0

Given a string paragraph and a list of banned words, return the most frequent word that is not banned. Words are case-insensitive and punctuation should be ignored. The answer is guaranteed to be unique.

This is a classic hash map frequency problem with string preprocessing. First normalize the paragraph: convert to lowercase and replace all punctuation with spaces. Split the result into words, ignoring empty strings. Build a hash map counting each word, skipping any word in the banned set. Track the word with the highest count as you go.

The hash map provides O(1) updates and lookups, making the entire process O(n + m) where n is the paragraph length and m is the number of banned words. The banned words are stored in a hash set for O(1) exclusion checks.

Edge cases include an empty paragraph (return empty string), a paragraph with only banned words (return empty string though the problem guarantees at least one non-banned word), and punctuation-heavy text like "a,b,c" where commas must be treated as separators.

The regex [^a-z]+ matches any sequence of non-letter characters. Replacing them with spaces converts punctuation, numbers, and whitespace into uniform separators. The split then produces clean word tokens. Using a hash set for banned words ensures O(1) exclusion checks during frequency counting.

Example Input & Output

Example 1
Input
"a.", []
Output
"a"
Explanation

Punctuation removed, single word a.

Example 2
Input
"a a a b b c", ["a"]
Output
"b"
Explanation

a is banned, b appears twice.

Example 3
Input
"Bob hit a ball, the hit BALL flew far after it was hit.", ["hit"]
Output
"ball"
Explanation

ball appears twice, hit is banned.

Example 4
Input
"abc abc def", ["abc"]
Output
"def"
Explanation

abc banned, def once.

Example 5
Input
"a!b!c", ["b"]
Output
"a"
Explanation

Punctuation splits into a,b,c; b banned; a first.

Algorithm Flow

Recommendation Algorithm Flow for Most Common Word
Recommendation Algorithm Flow for Most Common Word

Solution Approach

Normalize to lowercase, remove punctuation, split into words. Count with hash map, skip banned words. Track max.

function solution(paragraph, banned) {
  var normalized = paragraph.toLowerCase().replace(/[^a-z]+/g, ' ');
  var words = normalized.split(' ');
  var bannedSet = {};
  for (var i = 0; i < banned.length; i++) bannedSet[banned[i]] = true;
  var freq = {};
  var best = '';
  var bestCount = 0;
  for (var i = 0; i < words.length; i++) {
    var w = words[i];
    if (!w || bannedSet[w]) continue;
    freq[w] = (freq[w] || 0) + 1;
    if (freq[w] > bestCount) { bestCount = freq[w]; best = w; }
  }
  return best;
}

Time O(n+m), Space O(n+m).

Best Answers

java
import java.util.*;
class Solution {
    public String solution(String paragraph, String[] banned) {
        String normalized = paragraph.toLowerCase().replaceAll("[^a-z]+", " ");
        String[] words = normalized.split(" ");
        Set<String> bannedSet = new HashSet<>(Arrays.asList(banned));
        Map<String,Integer> freq = new HashMap<>();
        String best = "";
        int bestCount = 0;
        for (String w : words) {
            if (w.isEmpty() || bannedSet.contains(w)) continue;
            int c = freq.getOrDefault(w, 0) + 1;
            freq.put(w, c);
            if (c > bestCount) { bestCount = c; best = w; }
        }
        return best;
    }
}