The "Most Common Word" problem asks you to find the most frequent word in a given paragraph, excluding any words that appear in a provided list of banned words.
You are given:
paragraph
that may contain letters, punctuation, and spaces.banned
containing words that must be ignored when counting frequencies.The problem is about counting word frequencies, but with a few twists:
Let's break the solution into clear steps:
banned
list into a set for O(1) lookup.Sample Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
"bob hit a ball the hit ball flew far after it was hit"
["bob", "hit", "a", "ball", "the", "hit", "ball", "flew", "far", "after", "it", "was", "hit"]
{"hit"}
"ball"
Brute-force approach:
To solve the "Most Common Word" problem efficiently, we:
import re
from collections import Counter
class Solution:
def mostCommonWord(self, paragraph: str, banned: list[str]) -> str:
# Normalize paragraph: lowercase and replace punctuation with spaces
words = re.findall(r'\w+', paragraph.lower())
banned_set = set(banned)
counts = Counter(word for word in words if word not in banned_set)
return counts.most_common(1)[0][0]
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <cctype>
using namespace std;
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
unordered_set<string> banned_set(banned.begin(), banned.end());
unordered_map<string, int> count;
string word, res;
int max_count = 0;
for (int i = 0; i <= paragraph.size(); ++i) {
if (i < paragraph.size() && isalpha(paragraph[i])) {
word += tolower(paragraph[i]);
} else if (!word.empty()) {
if (!banned_set.count(word)) {
count[word]++;
if (count[word] > max_count) {
max_count = count[word];
res = word;
}
}
word = "";
}
}
return res;
}
};
import java.util.*;
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> bannedSet = new HashSet<>(Arrays.asList(banned));
Map<String, Integer> count = new HashMap<>();
String[] words = paragraph.toLowerCase().split("\\W+");
for (String word : words) {
if (!bannedSet.contains(word) && word.length() > 0) {
count.put(word, count.getOrDefault(word, 0) + 1);
}
}
String res = "";
int max = 0;
for (String word : count.keySet()) {
if (count.get(word) > max) {
max = count.get(word);
res = word;
}
}
return res;
}
}
/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
var mostCommonWord = function(paragraph, banned) {
let bannedSet = new Set(banned.map(w => w.toLowerCase()));
let words = paragraph.toLowerCase().match(/\w+/g);
let count = {};
let maxWord = '';
let maxCount = 0;
for (let word of words) {
if (!bannedSet.has(word)) {
count[word] = (count[word] || 0) + 1;
if (count[word] > maxCount) {
maxCount = count[word];
maxWord = word;
}
}
}
return maxWord;
};