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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

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^5
  • 1 <= message[i].length, bannedWords[i].length <= 15
  • message[i] and bannedWords[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

  1. Convert bannedWords to a set for fast lookup.
  2. Iterate through the message array, counting how many words are in the banned set.
  3. If the count reaches 2, return true (spam). Otherwise, return false.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#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;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
}
1
2
3
4
5
6
7
8
9
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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.