Skip to content

Commit 710d638

Browse files
authored
Added Spam message solution written during leetcode contest
1 parent 1aedbf9 commit 710d638

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

ReportSpamMessage/solution.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <unordered_map>
2+
3+
class Solution {
4+
public:
5+
bool reportSpam(vector<string>& message, vector<string>& bannedWords)
6+
{
7+
// use hashmap to efficiently keep count of each word in both lists
8+
unordered_map<string, int> hashmap;
9+
for (const auto& word : message)
10+
{
11+
hashmap[word]++;
12+
}
13+
14+
// keep banned words in a set so there are no duplicates
15+
set<string> bannedSet;
16+
for (const auto& word : bannedWords)
17+
bannedSet.insert(word);
18+
19+
// check the count of the hashmap words against the banned words
20+
int totalBannedWordsFound = 0;
21+
for (const auto& word : bannedSet)
22+
{
23+
int bannedWordCount = hashmap[word];
24+
if (bannedWordCount > 0)
25+
{
26+
totalBannedWordsFound += bannedWordCount;
27+
28+
if (totalBannedWordsFound >= 2)
29+
{
30+
// the message meets the critera to be considered spam
31+
return true;
32+
}
33+
}
34+
}
35+
36+
// if we reached here then then there were not enough baned words to be considered spam
37+
return false;
38+
}
39+
};

0 commit comments

Comments
 (0)