The description of problem
Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
来源:力扣(LeetCode)
链接:https:
Chinese version
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。
题目保证至少有一个词不在禁用列表中,而且答案唯一。
禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
来源:力扣(LeetCode)
链接:https:
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
example
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.
My codes
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
using namespace std;
class Solution {
public:
string toLowerCase(string str) {
stringstream ss;
for(int i = 0; i < str.size(); i++) {
if(str[i] >= 'A' && str[i] <= 'Z') {
ss << (char)(str[i] + 32);
} else {
ss << str[i];
}
}
return ss.str();
}
vector<string> split(string str) {
vector<string> res;
stringstream ss;
for(int i = 0; i < str.size(); i++) {
if(str[i] == ' ' || str[i] == ',' || str[i] == '.' || str[i] == ';' || str[i] == ':' || str[i] == '?' || str[i] == '!' || str[i] == '\'') {
if(ss.str() != "") {
res.push_back(ss.str());
ss.str("");
}
} else {
ss << str[i];
}
}
if(ss.str() != "") {
res.push_back(ss.str());
}
return res;
}
string getMax(unordered_map<string, int> m) {
string res;
int max = 0;
for (auto it = m.begin(); it != m.end(); it++) {
if (it->second > max) {
max = it->second;
res = it->first;
}
}
return res;
}
string mostCommonWord(string paragraph, vector<string>& banned) {
vector<string> words = split(paragraph);
unordered_map<string, int> m;
for (auto word : words) {
string w = toLowerCase(word);
if (m.find(w) == m.end()) {
m[w] = 1;
} else {
m[w]++;
}
if (find(banned.begin(), banned.end(), w) != banned.end()) {
m.erase(w);
}
}
for (auto it = m.begin(); it != m.end(); it++) {
cout << it->first << " " << it->second << endl;
}
return getMax(m);
}
};
int main()
{
Solution s;
string paragraph = "j. t? T. z! R, v, F' x! L; l! W. M; S. y? r! n; O. q; I? h; w. t; y; X? y, p. k! k, h, J, r? w! U! V; j' u; R! z. s. T' k. P? M' I' j! y. P, T! e; X. w? M! Y, X; G; d, X? S' F, K? V, r' v, v, D, w, K! S? Q! N. n. V. v. t? t' x! u. j; m; n! F, V' Y! h; c! V, v, X' X' t? n; N' r; x. W' P? W; p' q, S' X, J; R. x; z; z! G, U; m. P; o. P! Y; I, I' l' J? h; Q; s? U, q, x. J, T! o. z, N, L; u, w! u, S. Y! V; S? y' E! O; p' X, w. p' M, h! R; t? K? Y' z? T? w; u. q' R, q, T. R? I. R! t, X, s? u; z. u, Y, n' U; m; p? g' P? y' v, o? K? R. Q? I! c, X, x. r' u! m' y. t. W; x! K? B. v; m, k; k' x; Z! U! p. U? Q, t, u' E' n? S' w. y; W, x? r. p! Y? q, Y. t, Z' V, S. q; W. Z, z? x! k, I. n; x? z; V? s! g, U; E' m! Z? y' x? V! t, F. Z? Y' S! z, Y' T? x?";
vector<string> banned = {"m","q","e","l","c","i","z","j","g","t","w","v","h","p","d","b","a","r","x","n"};
cout << s.mostCommonWord(paragraph, banned) << endl;
return 0;
}
The corresponding results:
u 16
k 11
o 5
y 19
s 13
f 4
y
|