3295. Report Spam Message LeetCode Solution

In this guide, you will get 3295. Report Spam Message LeetCode Solution with the best time and space complexity. The solution to Report Spam Message problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Report Spam Message solution in C++
  4. Report Spam Message solution in Java
  5. Report Spam Message solution in Python
  6. Additional Resources
3295. Report Spam Message LeetCode Solution image

Problem Statement of Report Spam Message

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.

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 <= 105
1 <= message[i].length, bannedWords[i].length <= 15
message[i] and bannedWords[i] consist only of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(|\texttt{message}| + |\texttt{bannedWords}|)
  • Space Complexity: O(|\texttt{bannedWords}|)

3295. Report Spam Message LeetCode Solution in C++

class Solution {
 public:
  bool reportSpam(vector<string>& message, vector<string>& bannedWords) {
    const unordered_set<string> bannedWordsSet{bannedWords.begin(),
                                               bannedWords.end()};
    int count = 0;

    for (const string& word : message)
      if (bannedWordsSet.contains(word) && ++count > 1)
        return true;

    return false;
  }
};
/* code provided by PROGIEZ */

3295. Report Spam Message LeetCode Solution in Java

class Solution {
  public boolean reportSpam(String[] message, String[] bannedWords) {
    Set<String> bannedWordsSet = new HashSet<>(Arrays.asList(bannedWords));
    int count = 0;

    for (final String word : message)
      if (bannedWordsSet.contains(word) && ++count > 1)
        return true;

    return false;
  }
}
// code provided by PROGIEZ

3295. Report Spam Message LeetCode Solution in Python

class Solution:
  def reportSpam(self, message: list[str], bannedWords: list[str]) -> bool:
    bannedWordsSet = set(bannedWords)
    return sum(word in bannedWordsSet for word in message) > 1
# code by PROGIEZ

Additional Resources

See also  633. Sum of Square Numbers LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.