-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy paths2.cpp
29 lines (26 loc) · 871 Bytes
/
s2.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
// OJ: https://leetcode.com/problems/most-common-word/
// Author: github.com/lzl124631x
// Time: O(N) where N is character count in paragraph
// Space: O(N)
// Ref: https://leetcode.com/problems/most-common-word/discuss/184971/C%2B%2B-4ms-straightforward-solution
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
for (auto &c : paragraph) {
c = isalpha(c) ? tolower(c) : ' ';
}
istringstream iss(paragraph);
string word;
unordered_map<string, int> cnt;
while (iss >> word) ++cnt[word];
for (auto b : banned) cnt[b] = 0;
int maxCnt = 0;
for (auto p : cnt) {
if (p.second > maxCnt) {
maxCnt = p.second;
word = p.first;
}
}
return word;
}
};