题目:
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.
Example 1:
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.
Example 2:
Input: paragraph = "a.", banned = [] Output: "a"
Constraints:
1 <= paragraph.length <= 1000
- paragraph consists of English letters, space
' '
, or one of the symbols:"!?',;."
. 0 <= banned.length <= 100
1 <= banned[i].length <= 10
banned[i]
consists of only lowercase English letters.
思路:
通过率只有40+的简单题,读了一下发现没难度,结果做完发现是第46个case有那个大病。"a, a, a, a, b,b,b,c, c" ["a"],仔细看,b后面是没有空格的,如果用stringsstream读进来,会导致"b,b,b,c"是一个单词。不知道是不是出题人笔误,但是把它放在倒数几个case感觉是故意恶心人加的,导致通过率极低。解决方法就是在预处理时先将非字母的剔除即可。原本如果没有这个case,string大写转小写用transform(begin(s), end(s), begin(s), ::tolower)即可,但是这里要剔除符号,只能手动遍历了。
代码:
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
unordered_set<string> ban(begin(banned), end(banned));
unordered_map<string, int> record;
int count = 0;
string ans;
for (auto & c: paragraph) c = isalpha(c) ? tolower(c) : ' ';
istringstream ss(paragraph);
string tmp;
while (ss >> tmp) {
if (ban.count(tmp))
continue;
record[tmp]++;
if (record[tmp] > count) {
count = record[tmp];
ans = tmp;
}
}
return ans;
}
};