-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path819.cpp
More file actions
31 lines (31 loc) · 1.05 KB
/
Copy path819.cpp
File metadata and controls
31 lines (31 loc) · 1.05 KB
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
class Solution {
public:
string mostCommonWord(string paragraph, vector<string> &banned) {
int start = 0, end = 0;
unordered_map<string, int> counter;
unordered_set<string> ban(banned.begin(), banned.end());
while (end < paragraph.size()) {
while ((tolower(paragraph[end]) < 'a' || tolower(paragraph[end]) > 'z') && end < paragraph.size())
++end;
char curr = paragraph[end];
string str;
while (tolower(paragraph[end]) >= 'a' && tolower(paragraph[end]) <= 'z' && end < paragraph.size()) {
str += tolower(paragraph[end]);
++end;
}
if (ban.find(str) == ban.end())
++counter[str];
start = end + 1;
end = start;
}
string most_commont;
int count = 0;
for (auto &str : counter) {
if (str.second > count) {
most_commont = str.first;
count = str.second;
}
}
return most_commont;
}
};