Given a paragraph and a list of banned words, return the most frequent word that is not banned. It is guaranteed there is at least one word that isn’t banned, and that the answer is unique.
The words in the paragraph are case-insensitive and the answer should be returned in lowercase.
Example 1:
Note:
,
or .
.import re
from collections import defaultdict
def mostCommonWord(paragraph, banned):
# Normalize the paragraph to lowercase
paragraph = paragraph.lower()
# Replace punctuations with spaces using regex
paragraph = re.sub(r'[^\w\s]', ' ', paragraph)
# Split paragraph into words
words = paragraph.split()
# Freq dict to count occurrences of each word
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
# Remove banned words from the frequency dict
for word in banned:
if word in word_count:
del word_count[word]
# Find the word with the maximum frequency
most_common = max(word_count, key=word_count.get)
return most_common
# Example usage
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
print(mostCommonWord(paragraph, banned)) # Output: "ball"
n
is the length of the paragraph string due to conversion to lowercase and regex replacement.n
is the length of the cleaned paragraph string.m
is the number of words in the paragraph.b
is the number of banned words.k
is the number of unique words in the frequency dictionary.Overall, the time complexity is O(n + m + b + k)
, which simplifies generally to O(n)
assuming m
, b
, and k
are small compared to n
.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?