使用unordered_map方法查找C++中最(多个)常用词



从文本输入中查找最常用的单词,不包括给定单词的列表。如果有多个最大字数,则显示所有字数。

我对 21/24 个测试用例的方法词,我似乎想不出我缺少的 3 个测试用例。

我正在添加我现在拥有的代码,据我说这是有效的。我现在不想要另一种实现它的方式(尽管非常欢迎提出建议),我只想就我缺少的可能测试用例挑选你的大脑。

vector<string> mostCommonWord(string paragraph, vector<string>& banned) {
unordered_map<string, int>m;

for(int i = 0; i < paragraph.size();){
string s = "";
while(i < paragraph.size() && isalpha(paragraph[i])) s.push_back(tolower(paragraph[i++]));  // go through till you find one word completely
while(i < paragraph.size() && !isalpha(paragraph[i])) i++; // avoid all the white spaces and other characters
m[s]++; // include the word found and increment its count
}
for(auto x: banned) m[x] = 0; // make the count of all the banned words to be 0
vector<string> result;
string res = "";
int count = INT_MIN;
// find the maximum count
for(auto x: m)
if(x.second > count) count = x.second;
// we might have the case where all the words were in banned words, which would result the count == -1, so return an empty vector in this case
if(count <= 0) return result;
// add the words corresponding to that to the final vector<string>
for(auto x: m)
if(x.second == count) result.push_back(x.first);
return result;  
}

它适用于我能想到的所有场景,但失败了 3 个测试用例。 我无法访问这些测试用例,只是想讨论一下它可能是什么!

  1. 您确定其他字符(数字)应被视为单词分隔符吗?
  2. 如果paragraph以空格开头或不是字母字符,则将空字符串插入到映射中:m[""] = 1.

最新更新