Can Ayabody Explain why this code piece gives wrong answer

programming
Tags: #<Tag:0x00007f24256784a0>

#1

bool comparison(pair<int,int> a, pair<int,int> b){
if( a.first == b.first )
return a.second < b.second;
return a.first > b.first;
}

vector split(string str,char sym){
vector s;
int low = 0;
for(int i=0;i<str.size();i++){
if(str[i] == sym){
s.push_back(str.substr(low,i-low));
low = i+1;
}
if(i == str.size()-1){
s.push_back(str.substr(low,str.length()-low));
}
}
return s;
}

vector Solution::solve(string A, vector &B) {

map<string,vector<int> > word_freq;
int n = B.size();
for(int i=0;i<n;i++){
    vector<string> s = split(B[i],'_');
    for(int j=0;j<s.size();j++){
        if(word_freq.find(s[j]) == word_freq.end()){
            vector<int> v;
            v.push_back(i);
            word_freq[s[j]] = v;
        }
        else
            word_freq[s[j]].push_back(i);
    }
}

vector<int> result(n,0);
vector<string> s = split(A,'_');
for(int i=0;i<s.size();i++){
    auto it = word_freq.find(s[i]);
    if(it != word_freq.end()){
        for(int j=0;j<it->second.size();j++){
            result[it->second[j]]++;
        }
    }
}

vector<pair<int,int> > res;     
for(int i=0;i<n;i++){
    res.push_back(make_pair(result[i],i));
}
sort(res.begin(),res.end(),comparison);
vector<int> ans(n);
for(int i=0;i<n;i++){
    ans[i] = res[i].second;
}

return ans;    

}