-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path819.cpp
34 lines (34 loc) · 1.03 KB
/
819.cpp
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
28
29
30
31
32
33
34
// hash.cpp
class Solution {
public:
string mostCommonWord(string paragraph, vector<string> &banned) {
unordered_map<string, int> wordmap;
transform(paragraph.begin(), paragraph.end(), paragraph.begin(),
[](char &ch) -> char {
switch (ch) {
case '!':
case '?':
case '\'':
case ',':
case ';':
case '.':
return ' ';
}
return tolower(ch);
});
unordered_set<string> bannedSet(banned.begin(), banned.end());
stringstream ss(paragraph);
string str;
while (!ss.eof()) {
ss >> str;
if (bannedSet.find(str) == bannedSet.end())
++wordmap[str];
}
return max_element(wordmap.begin(), wordmap.end(),
[](const pair<const string, int> &a,
const pair<const string, int> &b) {
return a.second < b.second;
})
->first;
}
};