Найти наиболее распространенное слово из текстового ввода, исключая список заданных слов. Если максимальных слов несколько, отобразите их все.
Мои методические слова для 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 теста.
Мне не дали доступ к этим тестам, я просто хотел бы обсудить, что это может быть!