Trie
\nFirst, we add all strings into a Trie, incrementing the count for each prefix.\n    \nThen, for each string, we aggregate the count for all its prefixes. \n  
\nFirst, we add all strings into a Trie, incrementing the count for each prefix.\n    \nThen, for each string, we aggregate the count for all its prefixes. \n    \n**C++**\n```cpp\nstruct Trie {\n    Trie* ch[26] = {};\n    int cnt = 0;\n    void insert(string &w, int i = 0) {\n        auto n = this;\n        for (auto c : w) {\n            if (n->ch[c - \'a\'] == nullptr)\n                n->ch[c - \'a\'] = new Trie();\n            n = n->ch[c - \'a\'];\n            ++n->cnt;\n        }\n    }\n    int count(string &w, int i = 0) {\n        return cnt + (i == w.size() ? 0 : ch[w[i] - \'a\']->count(w, i + 1));\n    }\n}; \nvector<int> sumPrefixScores(vector<string>& words) {\n    Trie t;\n    for (auto &w : words)\n        t.insert(w);\n    vector<int> res;\n    for (auto &w : words)\n        res.push_back(t.count(w));\n    return res;\n}\n// ```
[]