Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
Example 1:
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"] Output: "ball" Explanation: "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.
Example 2:
Input: paragraph = "a.", banned = [] Output: "a"
Constraints:
1 <= paragraph.length <= 1000' ', or one of the symbols: "!?',;.".0 <= banned.length <= 1001 <= banned[i].length <= 10banned[i] consists of only lowercase English letters.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.
This C solution uses an array of structures to store word frequencies, given C doesn't have default data structures for maps like other higher-level languages. It tokenizes the paragraph by spaces and punctuation, converts to lowercase, filters out banned words, and counts frequencies. The most frequent non-banned word is returned.
C++
Java
Python
C#
JavaScript
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.
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.
In this advanced C solution, we utilize qsort for sorting based on frequency after processing the paragraph. We tokenize the paragraph, convert to lowercase, check against banned set, and store in custom structure. The sorting provides the most frequent non-banned word.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N log N) due to sorting, where N is total words extracted. Space Complexity: O(N).
| Approach | Complexity |
|---|---|
| Approach 1: Frequency Count with HashMap | 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. |
| Approach 2: Advanced String Manipulation and Collection | Time Complexity: O(N log N) due to sorting, where N is total words extracted. Space Complexity: O(N). |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,140 views views
Watch 9 more video solutions →Practice Most Common Word with our built-in code editor and test cases.
Practice on FleetCode