Report Spam Message - Problem
Spam Detection Challenge
You're building a content moderation system for a messaging platform. Given an array of strings
A message is considered spam if it contains at least two words that exactly match any word in the
Goal: Return
Example:
• Message: ["hello", "world", "spam", "spam"]
• Banned: ["spam", "bad"]
• Result:
You're building a content moderation system for a messaging platform. Given an array of strings
message representing words in a message and an array of strings bannedWords containing prohibited terms, determine if the message should be flagged as spam.A message is considered spam if it contains at least two words that exactly match any word in the
bannedWords list. The same banned word can appear multiple times and each occurrence counts toward the spam threshold.Goal: Return
true if the message is spam, false otherwise.Example:
• Message: ["hello", "world", "spam", "spam"]
• Banned: ["spam", "bad"]
• Result:
true (contains 2 instances of "spam") Input & Output
example_1.py — Basic Spam Detection
$
Input:
message = ["hello", "world", "leetcode"], bannedWords = ["world", "hello"]
›
Output:
true
💡 Note:
The message contains "hello" and "world", both of which are in bannedWords. Since we found 2 banned words, the message is spam.
example_2.py — Not Enough Banned Words
$
Input:
message = ["hello", "programming", "fun"], bannedWords = ["world", "programming"]
›
Output:
false
💡 Note:
Only "programming" from the message matches bannedWords. Since we need at least 2 matches and only found 1, the message is not spam.
example_3.py — Duplicate Banned Words Count
$
Input:
message = ["spam", "spam", "spam"], bannedWords = ["spam"]
›
Output:
true
💡 Note:
The word "spam" appears 3 times in the message and matches the banned word. Since 3 ≥ 2, the message is spam. Each occurrence counts separately.
Constraints
- 1 ≤ message.length ≤ 104
- 1 ≤ bannedWords.length ≤ 104
- 1 ≤ message[i].length, bannedWords[i].length ≤ 2000
- message[i] and bannedWords[i] consist only of lowercase English letters
- No duplicate words within bannedWords array
Visualization
Tap to expand
Understanding the Visualization
1
Setup Scanner
Create hash set of prohibited items (banned words) for instant recognition
2
Scan Items
Check each message word against the prohibited items list
3
Count Violations
Increment counter each time a prohibited item is found
4
Trigger Alert
Flag as spam immediately when 2 prohibited items detected
Key Takeaway
🎯 Key Insight: Hash set provides O(1) lookups, and early termination saves unnecessary processing once spam is detected!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code