You are given an array of strings message and an array of strings bannedWords.
An array of words is considered spam if there are at least two words in it that exactly match any word in bannedWords.
Return true if the array message is spam, and false otherwise.
Example 1:
Input: message = ["hello","world","leetcode"], bannedWords = ["world","hello"]
Output: true
Explanation:
The words "hello" and "world" from the message array both appear in the bannedWords array.
Example 2:
Input: message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]
Output: false
Explanation:
Only one word from the message array ("programming") appears in the bannedWords array.
Constraints:
1 <= message.length, bannedWords.length <= 1051 <= message[i].length, bannedWords[i].length <= 15message[i] and bannedWords[i] consist only of lowercase English letters.Problem Overview: You receive a message represented as an array of words and another list containing banned words. The task is to determine whether the message should be reported as spam. A message is considered spam if at least two words from the message appear in the banned list.
Approach 1: Hash Set for Fast Lookup (O(n + m) time, O(m) space)
The most practical solution converts the banned word list into a hash set. This gives constant-time membership checks. First iterate through the banned array and insert each word into the set. Then iterate through the message words and check whether each word exists in the set using a hash lookup. Maintain a counter for matches and stop once the count reaches two.
The key insight is that repeated membership checks become expensive if the banned list is scanned each time. A hash set reduces that lookup from O(m) to O(1). The total complexity becomes O(n + m), where n is the number of words in the message and m is the size of the banned list. Space complexity is O(m) for the set. This pattern appears frequently in problems involving fast membership checks on hash tables and word matching across arrays.
Approach 2: Frequency Count with Early Exit (O(n + m) time, O(m) space)
Another way is to track occurrences of banned words using a frequency structure. Build a hash map or set from the banned list, then iterate through the message while counting matches. As soon as the second banned word is encountered, immediately return true. This early exit avoids scanning the entire message when spam is detected early.
This approach behaves similarly to the hash set strategy but emphasizes counting and early termination. The algorithm still performs a single pass through the message with constant-time lookups. Time complexity remains O(n + m), and space complexity stays O(m). Early termination often improves real-world performance when spam appears near the beginning of the message.
Both solutions rely on efficient word comparison and are common in moderation or filtering systems where strings must be matched against restricted lists. Problems involving string matching and hash-based membership checks frequently use this exact design.
Recommended for interviews: The hash set lookup approach is what most interviewers expect. It demonstrates that you recognize repeated membership checks and optimize them with a hash-based structure. Mentioning the naive scan and then replacing it with a set shows clear reasoning. Implementing the early exit variant further shows attention to practical performance.
This approach uses a hash set to store the banned words. We then iterate through the message array, checking if each word appears in the set. We count the number of matches, and if it reaches two, we return true. Otherwise, we continue and return false if we check all words.
The C solution uses a simple hash set. It constructs a hash set with twice the size of the banned words for small collision probability. Each message word is checked against the hash set, and the algorithm stops early when two matches are found.
Time complexity is O(n + m), where n is the number of message words and m is the number of banned words. Space complexity is O(m) due to storing the banned words in a hash set.
To avoid extra space usage while maintaining efficiency, one can count occurrences of banned words directly within the message. Utilize a counter to track the number of times a banned word appears in the message array, exiting early when two are found.
This C solution utilizes nested loops to go through each word in the message and check for matches within the banned words. Although less efficient than the hash-based approach, this method does not require auxiliary data structures and relies on direct comparison and counting of matches.
Time complexity: O(n * m), where n is the number of words in the message and m is the number of banned words. Space complexity is O(1) as no additional storage is used.
We use a hash table s to store all the words in bannedWords. Then, we traverse each word in message. If the word appears in the hash table s, we increment the counter cnt by one. If cnt is greater than or equal to 2, we return true; otherwise, we return false.
The time complexity is O((n + m) times |w|), and the space complexity is O(m times |w|). Here, n is the length of the array message, and m and |w| are the length of the array bannedWords and the maximum length of the words in the array, respectively.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Hash Set for Fast Lookup | Time complexity is O(n + m), where n is the number of message words and m is the number of banned words. Space complexity is O(m) due to storing the banned words in a hash set. |
| Approach 2: Frequency Count with Early Exit | Time complexity: O(n * m), where n is the number of words in the message and m is the number of banned words. Space complexity is O(1) as no additional storage is used. |
| Hash Table | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Set for Fast Lookup | O(n + m) | O(m) | General case where repeated membership checks are needed |
| Frequency Count with Early Exit | O(n + m) | O(m) | When spam may appear early and early termination improves runtime |
Report Spam Message || LeetCode Weekly Contest 416 || Leetcode Solution • codi • 610 views views
Watch 8 more video solutions →Practice Report Spam Message with our built-in code editor and test cases.
Practice on FleetCode