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 <= 1051 <= message[i].length, bannedWords[i].length <= 15message[i] and bannedWords[i] consist only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to identifying spam messages involves checking every possible combination of words and comparing them against a list of known spam phrases. It’s like trying every key on a keyring until you find the one that opens the lock. This method guarantees finding spam but might take a very long time.
Here's how the algorithm would work step-by-step:
def report_spam_message(messages, spam_patterns):
spam_report = []
for message in messages:
is_spam = False
# Iterate through each spam pattern to check against the message
for spam_pattern in spam_patterns:
# If the spam pattern exists in the message, flag it as spam
if spam_pattern in message:
is_spam = True
break
spam_report.append(is_spam)
return spam_reportTo report spam messages efficiently, we'll use a technique similar to how email providers filter unwanted content. The main idea is to break down the message and check it against known spam indicators.
Here's how the algorithm would work step-by-step:
def report_spam_message(messages):
seen_messages = set()
spam_reports = []
for message in messages:
# Use a set to efficiently check if we've seen this message before.
if message in seen_messages:
spam_reports.append(message)
else:
# Add the new message to the set of seen messages.
seen_messages.add(message)
return spam_reports| Case | How to Handle |
|---|---|
| messages is null or empty | Return an empty list if messages is null or empty to avoid NullPointerException or incorrect processing. |
| keywords is null or empty | If keywords is null or empty, return an empty list because no spam can be detected. |
| messages contains empty strings | Handle empty strings in messages by treating them as non-spam, i.e., skipping them during keyword search. |
| keywords contains empty strings | An empty string in keywords will match every message, so return indices of all messages. |
| Case sensitivity of keywords in messages | Convert both messages and keywords to lowercase for case-insensitive matching. |
| Large number of messages and keywords | Ensure efficient string searching (e.g., using a set for keywords lookup) to maintain acceptable performance with large datasets. |
| Special characters in messages or keywords | Sanitize or escape special characters in messages and keywords to prevent regex-related issues or incorrect matches. |
| Message contains multiple instances of the same keyword | The problem requires only one instance of a keyword to flag as spam, so no special handling is needed, presence is the key. |