LeetCode 3295: Report Spam Message
LeetCode 3295 Solution Explanation
Explanation
To solve this problem, we can iterate through each word in the message
array and check if it matches any word in the bannedWords
array. For each word in message
, we compare it with each word in bannedWords
and count the number of matches. If we find at least two matches, we return true
as the message is considered spam. If no word in message
has at least two matches, we return false
.
- Time complexity: O(m * n), where m is the number of words in
message
and n is the number of words inbannedWords
. - Space complexity: O(1).
LeetCode 3295 Solutions in Java, C++, Python
public boolean isSpamMessage(String[] message, String[] bannedWords) {
for (String word : message) {
int count = 0;
for (String bannedWord : bannedWords) {
if (word.equals(bannedWord)) {
count++;
if (count >= 2) {
return true;
}
}
}
}
return false;
}
Interactive Code Editor for LeetCode 3295
Improve Your LeetCode 3295 Solution
Use the editor below to refine the provided solution for LeetCode 3295. Select a programming language and try the following:
- Add import statements if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.
Loading editor...