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

时间:2019-03-28 01:32:38

标签: c++ debugging unordered-map

从文本输入中查找最常见的单词,但不包括给定单词的列表。如果有多个最大单词,则全部显示。

我的方法是针对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 个答案:

答案 0 :(得分:1)

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