✅✅Faster || Easy To Understand || C++ Code
Trie\n\n Time Complexity :- O(N * M)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    \n    // structure for TrieNode\n    \n    struct TrieNode
* ***Trie***\n\n* ***Time Complexity :- O(N * M)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    \n    // structure for TrieNode\n    \n    struct TrieNode\n    {\n        TrieNode* child[26];\n        \n        int count;\n        \n        TrieNode()\n        {\n            count = 0;\n            \n            for(int i = 0; i < 26; i++)\n            {\n                child[i] = NULL;\n            }\n        }\n    };\n    \n    // declare a global root\n    \n    TrieNode* root = new TrieNode();\n    \n    // function for insert\n    \n    void insert(string str)\n    {\n        int n = str.size();\n        \n        TrieNode* curr = root;\n        \n        for(int i = 0; i < n; i++)\n        {\n            int idx = str[i] - \'a\';\n            \n            // insert a new node\n            \n            if(curr -> child[idx] == NULL)\n            {\n                curr -> child[idx] = new TrieNode(); \n            }\n            \n            // increment the count of prefix\n            \n            curr -> child[idx] -> count++;\n            \n            // move curr pointer\n          \n            curr = curr -> child[idx];\n        }\n    }\n    \n    // function for get_sum\n    \n    int get_sum(string str)\n    {\n        int n = str.size();\n        \n        int sum = 0;\n        \n        TrieNode* curr = root;\n        \n        for(int i = 0; i < n; i++)\n        {\n            int idx = str[i] - \'a\';\n            \n            sum += curr -> child[idx] -> count;\n            \n            curr = curr -> child[idx];\n        }\n        \n        return sum;\n    }\n    \n    vector<int> sumPrefixScores(vector<string>& words) {\n        \n        int n = words.size();\n        \n        // insert all the words into trie\n        \n        for(int i = 0; i < n; i++)\n        {\n            insert(words[i]);\n        }\n        \n        vector<int> res(n, 0);\n        \n        // find sum for every word\n        \n        for(int i = 0; i < n; i++)\n        {\n            int sum = get_sum(words[i]);\n            \n            res[i] = sum;\n        }\n        \n        return res;\n    }\n};\n```
['Trie', 'C', 'C++']