Most Common Word (LeetCode 819) - interview notes
Approach
1. Lowercase the paragraph and pull words with a regex [a-z]+ (this strips "!?',;." punctuation in one pass).
2. Put the banned list in a set for O(1) lookups.
3. Count with a hash map and return the max key.
Python:
import re
from collections import Counter
def mostCommonWord(paragraph, banned):
ban = set(banned)
words = re.findall(r"[a-z]+", paragraph.lower())
return Counter(w for w in words if w not in ban).most_common(1)[0][0]
Complexity: O(P + B) time, O(P + B) space.
Edge cases to say out loud: punctuation glued to words ("ball,"), mixed case,
a banned word that is also the most frequent, a single-word paragraph.
Follow-up: return the top k words (heap of size k, O(N log k)).
Disclosure: I work on Linkjob AI. If you want practice with live feedback,
see Linkjob AI copilot: https://www.linkjob.ai/