Giving wrong answer. Can Anybody pls check


#1
class Trie
{
private: 
    Trie * next[26] = {};
    bool isword = false;
public: 
    Trie() {}
    
    void insert(string word)
    {
        Trie * node = this;
        for(char ch : word)
        {
            if(!node->next[ch- 'a']) 
            {
                node->next[ch -'a'] = new Trie();
                node = node->next[ch - 'a'];
            }
        }
        node->isword = true;
    }
    
    bool search(string word)
    {
        Trie * node = this;
        for(char ch : word)
        {
            if(!node->next[ch - 'a']) return false;
            node = node->next[ch-'a'];
        }
        return node->isword;
    }
};

Trie * T = new Trie();
int wordsno(string s)
{
    int count =0;
    for(int i=0;i< s.length();i++)
    {
        string word = "";
        while(i<s.length() && s[i]!='_')
            word+=s[i++];
        if(word!="")    
            count+= T->search(word);
    }
    return count;
}

vector<int> Solution::solve(string A, vector<string> &B) {
    //root = new Trie();
    for(int i=0;i<A.length();i++)
    {
        string word = "";
        while(i<A.length() && A[i]!= '_')
            word+= A[i++];
        if(word !="") T->insert(word);
        //cout<< "filling the trie" << endl;
    }
    auto comp = [&](vector<int> a , vector<int> b)
    {
        return a[0] > b[0] || (a[0]==b[0] && a[1] < b[1] ) ;
    };
    vector<vector<int>> vec;
    for(int i =0;i<B.size();i++)
    {
        vector<int> temp = {wordsno(B[i]),i};
        vec.push_back(temp);
        //cout<< "filling the vec" << endl;
    }
    sort(vec.begin(),vec.end(),comp);
    vector<int> ans;
    for(auto x : vec)
        ans.push_back(x[1]);
    return ans;
}