Problem Description
Given a paragraph of text and a list of banned words, return the most frequent word that is not in the banned list. The search is case-insensitive, punctuation should be ignored, and the result must be in lowercase.
Key Insights
- Normalize the paragraph by converting all characters to lowercase.
- Replace all punctuation with spaces to correctly extract words.
- Use a hash set for the banned words to enable fast lookups.
- Count the frequency of each non-banned word using a hash map.
- Determine the word with the highest frequency from the count map.
Space and Time Complexity
Time Complexity: O(N) where N is the length of the paragraph (processing each character and word). Space Complexity: O(N) for storing the words and their counts.
Solution
The solution first normalizes the text by converting it to lowercase and replacing punctuation with spaces. It then splits the paragraph into words. A hash set is used to store banned words for quick lookups. As words are processed, those not in the banned set are stored in a hash map with their corresponding counts. Finally, the word with the maximum count is determined and returned.