Skip to main content
Back to problems
#3295
Medium Algorithms

Report spam message

Array Hash Table String
48.5% acceptance
Feb 25, 2026
96
25
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn report_spam(message: Vec<String>, banned_words: Vec<String>) -> bool {
    let banned: std::collections::HashSet<&str> = banned_words.iter().map(|s| s.as_str()).collect();
    let mut count = 0;
    for word in &message {
      if banned.contains(word.as_str()) {
        count += 1;
        if count >= 2 {
          return true;
        }
      }
    }
    false
  }
}