-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path819.cpp
More file actions
27 lines (27 loc) · 833 Bytes
/
819.cpp
File metadata and controls
27 lines (27 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
paragraph += '.';
unordered_set<string> ust(banned.begin(), banned.end());
unordered_map<string,int> ump;
string ans = "";
int ans_freq = 0;
string word = "";
for(auto ch: paragraph){
if(isalpha(ch))
word += tolower(ch);
else if(word.size()>0){
string final_word = word;
if(ust.find(final_word)==ust.end()){
ump[final_word]++;
if(ump[final_word]>ans_freq){
ans = final_word;
ans_freq = ump[final_word];
}
}
word = "";
}
}
return ans;
}
};