Sponsored
Sponsored
This approach involves using a hash map to count the frequency of each word in the paragraph after converting it to lowercase and removing punctuation. Then, the word with the highest count that is not in the banned list is selected as the result.
Time Complexity: O(N + M), where N is the length of the paragraph and M is the number of banned words. Space Complexity: O(N) for storing word frequencies.
1from collections import Counter
2import re
3
4def most_common_word(paragraph, banned):
5 banned_set = set(banned)
6 words = re.findall(r'\w+', paragraph.lower())
7 word_count = Counter(word for word in words if word not in banned_set)
8 return word_count.most_common(1)[0][0]
9
10paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
11banned = ["hit"]
12print(most_common_word(paragraph, banned))
13
This Python solution uses regular expressions to extract words from the paragraph, counts their frequencies with collections.Counter
, and ignores banned words. most_common
method is used to find the most frequent non-banned word.
This approach leverages advanced string manipulation functions available in each language for efficient parsing and counting. The words are extracted, normalized, and counted using advanced language-specific methods and libraries for cleaner code.
Time Complexity: O(N log N) due to sorting, where N is total words extracted. Space Complexity: O(N).
1
This Java approach uses regex to transform the paragraph and splits on whitespace to list words. This is followed by frequency counting in a map. The Collections.max method is employed to find the most common word efficiently.