Report Spam Message
MediumUpdated: Aug 2, 2025
Practice on:
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.
Examples
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 <= 10^51 <= message[i].length, bannedWords[i].length <= 15message[i]andbannedWords[i]consist only of lowercase English letters.
Solution
Method 1 - Hash Set for Fast Lookup
Intuition
We want to quickly check how many words in the message are present in the bannedWords list. If at least two such words exist, the message is spam. Using a set for bannedWords allows O(1) lookup per word.
Approach
- Convert bannedWords to a set for fast lookup.
- Iterate through the message array, counting how many words are in the banned set.
- If the count reaches 2, return true (spam). Otherwise, return false.
Code
C++
#include <unordered_set>
#include <string>
#include <vector>
using namespace std;
bool isSpamMessage(vector<string>& message, vector<string>& bannedWords) {
unordered_set<string> banned(bannedWords.begin(), bannedWords.end());
int count = 0;
for (const string& word : message) {
if (banned.count(word)) {
count++;
if (count == 2) return true;
}
}
return false;
}
Go
func isSpamMessage(message []string, bannedWords []string) bool {
banned := make(map[string]struct{})
for _, w := range bannedWords {
banned[w] = struct{}{}
}
count := 0
for _, w := range message {
if _, ok := banned[w]; ok {
count++
if count == 2 {
return true
}
}
}
return false
}
Java
import java.util.*;
public class Solution {
public boolean isSpamMessage(String[] message, String[] bannedWords) {
Set<String> banned = new HashSet<>(Arrays.asList(bannedWords));
int count = 0;
for (String word : message) {
if (banned.contains(word)) {
count++;
if (count == 2) return true;
}
}
return false;
}
}
Kotlin
fun isSpamMessage(message: Array<String>, bannedWords: Array<String>): Boolean {
val banned = bannedWords.toSet()
var count = 0
for (word in message) {
if (word in banned) {
count++
if (count == 2) return true
}
}
return false
}
Python
def isSpamMessage(message, bannedWords):
banned = set(bannedWords)
count = 0
for word in message:
if word in banned:
count += 1
if count == 2:
return True
return False
Rust
use std::collections::HashSet;
fn is_spam_message(message: &[String], banned_words: &[String]) -> bool {
let banned: HashSet<_> = banned_words.iter().collect();
let mut count = 0;
for word in message {
if banned.contains(word) {
count += 1;
if count == 2 {
return true;
}
}
}
false
}
TypeScript
function isSpamMessage(message: string[], bannedWords: string[]): boolean {
const banned = new Set(bannedWords);
let count = 0;
for (const word of message) {
if (banned.has(word)) {
count++;
if (count === 2) return true;
}
}
return false;
}
Complexity
- ⏰ Time complexity: O(N + M), where N = message.length and M = bannedWords.length.
- 🧺 Space complexity: O(M), for the bannedWords set.