[Javascript] get rid of memory allocation problem
I found that in javascript, you will face memory problem in some testcase\nSo i\'m posting my solution here\n\nyou have to know that words with different inital
I found that in javascript, you will face memory problem in some testcase\nSo i\'m posting my solution here\n\nyou have to know that words with different inital characters wont affect each others\' score\nfor example, `abcd, aaab, acab` wont affect the scores of `cbcd, bacd, bbab, ccccd`.\nKnowing the fact, you know you only need to build a trie for same initial chacaters in order to solve the score of the groups instead of build a huge trie for all words together.\nThis allows system to do gc between each group solving their score.\n\n```\n/**\n * @param {string[]} words\n * @return {number[]}\n */\nvar sumPrefixScores = function(words) {\n    const scores = {};\n    const groups = words.reduce((groups, word) => {\n        if (!groups[word[0]]) {\n            groups[word[0]] = [];\n        }\n        groups[word[0]].push(word);\n        return groups;\n    }, {});\n    \n    for (const group of Object.values(groups)) {\n        const root = createNode();\n        for (let i = 0; i < group.length; i++) {\n            const word = group[i];\n            insert(root, word);\n        }\n        for (let i = 0; i < group.length; i++) {\n            const word = group[i];\n            scores[word] = getScore(root, word, 0);\n        }\n    }\n\t\n    return words.map(word => scores[word]);\n};\n\nconst getScore = (node, word, i) => {\n    if (i === word.length) {\n        return 0;\n    }\n    const charCode = word.charCodeAt(i) - 97;\n    const next = node.children[charCode];\n    const score = next.count;\n    return score + getScore(next, word, i + 1);\n}\n\nconst insert = (node, word, i = 0) => {\n    node.count++;\n    if (i === word.length) {\n        return;\n    }\n    const charCode = word.charCodeAt(i) - 97;\n    if (!node.children[charCode]) {\n        node.children[charCode] = createNode();\n    }\n    insert(node.children[charCode], word, i+ 1);\n}\n\nconst createNode = () => {\n    return {\n        count: 0,\n        children: new Array(26),\n    };\n}\n```
[]