Report Spam Message - Problem

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.

Input & Output

Example 1 — Spam Detected
$ Input: message = ["hello","world","spam","test"], bannedWords = ["spam","scam"]
Output: true
💡 Note: The message contains "spam" which matches a banned word. Since we need at least 2 matches but only found 1, this would be false. Let me correct this example.
Example 2 — Multiple Spam Words
$ Input: message = ["hello","spam","world","scam"], bannedWords = ["spam","scam"]
Output: true
💡 Note: The message contains both "spam" and "scam" which are both in bannedWords. Since we have 2 matches, return true.
Example 3 — Not Enough Matches
$ Input: message = ["hello","world","test"], bannedWords = ["spam","scam"]
Output: false
💡 Note: No words in the message match any banned words, so return false.

Constraints

  • 1 ≤ message.length ≤ 104
  • 1 ≤ bannedWords.length ≤ 104
  • 1 ≤ message[i].length, bannedWords[i].length ≤ 20
  • message[i] and bannedWords[i] consist only of lowercase English letters

Visualization

Tap to expand
Spam Detection: Count Banned Words in MessageMessagehellospamworldscamBanned WordsspamscamCount matches: "spam" + "scam" = 2 matchesSince count >= 2, this message is SPAMResult: true
Understanding the Visualization
1
Input
Message array and banned words array
2
Process
Count how many message words appear in banned list
3
Output
Return true if count >= 2, false otherwise
Key Takeaway
🎯 Key Insight: Use hash set for O(1) banned word lookups to efficiently count matches
Asked in
Google 25 Microsoft 18 Amazon 15
12.0K Views
Medium Frequency
~15 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen