Easy, well explained Javascript and Trie solution
Intuition\nThe task is to compute the prefix score for each word by summing how many words share each prefix. It\'s possible to solve it using a HashMap, but it
# Intuition\nThe task is to compute the prefix score for each word by summing how many words share each prefix. It\'s possible to solve it using a HashMap, but it will fail by time limit on the last casess, so more efficient data structure is needed. Using a Trie allows us to efficiently store and count shared prefixes while avoiding redundant calculations.\n\n# Approach\n- Build the Trie: Insert each word into a Trie, tracking how many words pass through each prefix.\n- Calculate Prefix Scores: For each word, traverse its characters in the Trie, summing the counts of how many words share that prefix.\n\n# Complexity\n- Time complexity:\n$$O(N\xD7L)$$ where N is the number of words and L is the average word length.\n\n- Space complexity:\n$$O(N\xD7L)$$ - to store the same data assuming minimal to NO amount of intersections\n\n# Code\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}\n\nclass Trie {\n    constructor() {\n        this.root = new TrieNode();\n    }\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++; //Adjust counter\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}\n\nvar sumPrefixScores = function(words) {\n    const trie = new Trie();\n\n    //Add words to Trie\n    for (const word of words) {\n        trie.insert(word);\n    }\n\n    //Map words and counts\n    const result = words.map(word => trie.getPrefixScore(word));\n    \n    return result;\n};\n\n```
['Trie', 'JavaScript']