File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments