Optimized Solution Using a Custom Trie for Prefix Scores | Java | C++ | [Video Explanation]
Intuition, approach, and complexity discussed in detail in the video solution\nhttps://youtu.be/HymJeL_ERlg\n# Code\njava []\nclass TrieNode{\n    TrieNode chil
# Intuition, approach, and complexity discussed in detail in the video solution\nhttps://youtu.be/HymJeL_ERlg\n# Code\n``` java []\nclass TrieNode{\n    TrieNode children[] = new TrieNode[26];\n    int nodeFreq = 0;\n    void insert(String key){\n        TrieNode curr = this;\n        for(var chr : key.toCharArray()){\n            int indx = (int) (chr - \'a\');\n            if(curr.children[indx] == null){\n                curr.children[indx] = new TrieNode();\n            }\n            curr.children[indx].nodeFreq++;\n            curr = curr.children[indx];\n        }\n    }\n    int findScore(String key){\n        TrieNode curr = this;\n        int score =  0;\n        for(var chr : key.toCharArray()){\n            int indx = (int) (chr - \'a\');\n            score += curr.children[indx].nodeFreq;\n            curr = curr.children[indx];\n        }\n        return score;\n    }\n}\nclass Solution {\n    public int[] sumPrefixScores(String[] words) {\n        TrieNode root = new TrieNode();\n        for(var word : words){\n            root.insert(word);\n        }\n        int size = words.length;\n        int answer[] = new int[size];\n        for(int indx = 0; indx < size; indx++){\n            answer[indx] = root.findScore(words[indx]);\n        }       \n        return answer;\n    }\n}\n```\n```cpp []\nclass TrieNode {\npublic:\n    TrieNode* children[26];\n    int nodeFreq;\n\n    TrieNode() {\n        for (int i = 0; i < 26; i++) {\n            children[i] = nullptr;\n        }\n        nodeFreq = 0;\n    }\n\n    void insert(const string& key) {\n        TrieNode* curr = this;\n        for (char chr : key) {\n            int indx = chr - \'a\';\n            if (curr->children[indx] == nullptr) {\n                curr->children[indx] = new TrieNode();\n            }\n            curr->children[indx]->nodeFreq++;\n            curr = curr->children[indx];\n        }\n    }\n\n    int findScore(const string& key) {\n        TrieNode* curr = this;\n        int score = 0;\n        for (char chr : key) {\n            int indx = chr - \'a\';\n            score += curr->children[indx]->nodeFreq;\n            curr = curr->children[indx];\n        }\n        return score;\n    }\n};\n\nclass Solution {\npublic:\n    vector<int> sumPrefixScores(vector<string>& words) {\n        TrieNode* root = new TrieNode();\n        for (const auto& word : words) {\n            root->insert(word);\n        }\n\n        int size = words.size();\n        vector<int> answer(size);\n        for (int i = 0; i < size; i++) {\n            answer[i] = root->findScore(words[i]);\n        }\n\n        return answer;\n    }\n};\n\n```
['Array', 'String', 'Trie', 'Counting', 'C++', 'Java']