Most Common Word — Normalize + Frequency Map
Advertisement
Problem 195 · Most Common Word
Difficulty: Medium · Pattern: String Normalization + Frequency Map
Find the most frequent word in a paragraph not in the banned list.
Solutions
# Python
import re
from collections import Counter
def mostCommonWord(paragraph, banned):
banned = set(banned)
words = re.findall(r'[a-z]+', paragraph.lower())
return Counter(w for w in words if w not in banned).most_common(1)[0][0]
// Java
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> ban = new HashSet<>(Arrays.asList(banned));
String[] words = paragraph.toLowerCase().split("[^a-z]+");
Map<String,Integer> freq = new HashMap<>();
for (String w : words)
if (!ban.contains(w)) freq.merge(w, 1, Integer::sum);
return Collections.max(freq.entrySet(), Map.Entry.comparingByValue()).getKey();
}
// C++
string mostCommonWord(string paragraph, vector<string>& banned) {
unordered_set<string> ban(banned.begin(), banned.end());
for (char& c : paragraph) c = isalpha(c) ? tolower(c) : ' ';
istringstream ss(paragraph);
unordered_map<string,int> freq;
string w, best; int mx = 0;
while (ss >> w) {
if (!ban.count(w) && ++freq[w] > mx) { mx = freq[w]; best = w; }
}
return best;
}
Complexity
- Time: O(n + b) where n = paragraph length, b = banned size
- Space: O(n)
Advertisement