Solution By Dare2Solve | Detailed Explanation | Clean Code
Exlanation []\nauthorslog.com/blog/FxtvdgRwjW\n\n# Code\n\ncpp []\nclass TrieNode {\npublic:\n    unordered_map<char, TrieNode*> children;\n    int prefixCount;
```Exlanation []\nauthorslog.com/blog/FxtvdgRwjW\n```\n# Code\n\n```cpp []\nclass TrieNode {\npublic:\n    unordered_map<char, TrieNode*> children;\n    int prefixCount;\n    \n    TrieNode() {\n        prefixCount = 0;\n    }\n};\n\nclass Trie {\npublic:\n    TrieNode* root;\n    \n    Trie() {\n        root = new TrieNode();\n    }\n    \n    void insert(const string& word) {\n        TrieNode* node = root;\n        for (char c : word) {\n            if (node->children.find(c) == node->children.end()) {\n                node->children[c] = new TrieNode();\n            }\n            node = node->children[c];\n            node->prefixCount++;\n        }\n    }\n    \n    int getPrefixScore(const string& word) {\n        TrieNode* node = root;\n        int score = 0;\n        for (char c : word) {\n            node = node->children[c];\n            score += node->prefixCount;\n        }\n        return score;\n    }\n};\n\nclass Solution {\npublic:\n    vector<int> sumPrefixScores(vector<string>& words) {\n        Trie trie;\n        vector<int> result;\n        \n        // Insert all words into the trie\n        for (const string& word : words) {\n            trie.insert(word);\n        }\n        \n        // Get the prefix score for each word\n        for (const string& word : words) {\n            result.push_back(trie.getPrefixScore(word));\n        }\n        \n        return result;\n    }\n};\n```\n\n```python []\nclass TrieNode:\n    def __init__(self):\n        self.children = {}\n        self.prefixCount = 0\n\nclass Trie:\n    def __init__(self):\n        self.root = TrieNode()\n\n    def insert(self, word):\n        node = self.root\n        for char in word:\n            if char not in node.children:\n                node.children[char] = TrieNode()\n            node = node.children[char]\n            node.prefixCount += 1\n\n    def getPrefixScore(self, word):\n        node = self.root\n        score = 0\n        for char in word:\n            node = node.children[char]\n            score += node.prefixCount\n        return score\n\nclass Solution:\n    def sumPrefixScores(self, words):\n        trie = Trie()\n        result = []\n        for word in words:\n            trie.insert(word)\n        for word in words:\n            result.append(trie.getPrefixScore(word))\n        return result\n```\n\n```java []\nclass TrieNode {\n    Map<Character, TrieNode> children = new HashMap<>();\n    int prefixCount = 0;\n}\n\nclass Trie {\n    TrieNode root;\n\n    public Trie() {\n        root = new TrieNode();\n    }\n\n    public void insert(String word) {\n        TrieNode node = root;\n        for (char ch : word.toCharArray()) {\n            node.children.putIfAbsent(ch, new TrieNode());\n            node = node.children.get(ch);\n            node.prefixCount++;\n        }\n    }\n\n    public int getPrefixScore(String word) {\n        TrieNode node = root;\n        int score = 0;\n        for (char ch : word.toCharArray()) {\n            node = node.children.get(ch);\n            score += node.prefixCount;\n        }\n        return score;\n    }\n}\n\npublic class Solution {\n    public int[] sumPrefixScores(String[] words) {\n        Trie trie = new Trie();\n        int[] result = new int[words.length];\n        for (String word : words) {\n            trie.insert(word);\n        }\n        for (int i = 0; i < words.length; i++) {\n            result[i] = trie.getPrefixScore(words[i]);\n        }\n        return result;\n    }\n}\n\n```\n\n```javascript []\n/**\n * @param {string[]} words\n * @return {number[]}\n */\nclass TrieNode {\n    constructor() {\n        this.children = {}; //a trie\n        this.prefixCount = 0; \n    }\n}\nclass Trie {\n    constructor() {\n        this.root = new TrieNode();\n    }\n    insert(word) {\n        let node = this.root;\n        for (const char of word) {\n            if (!node.children[char]) { //Create a Trie if not exists\n                node.children[char] = new TrieNode();\n            }\n            node = node.children[char]; //add element to the Trie\n            node.prefixCount++;\n        }\n    }\n\n    getPrefixScore(word) {\n        let node = this.root;\n        let score = 0;\n        for (const char of word) {\n            node = node.children[char];\n            score += node.prefixCount;\n        }\n        return score;\n    }\n}\nvar sumPrefixScores = function(words) {\n    const trie = new Trie();\n    for (const word of words) {\n        trie.insert(word);\n    }\n    const result = words.map(word => trie.getPrefixScore(word));\n    \n    return result;\n};\n```
['String', 'Trie', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']