[Python] Explanation with pictures, 2 solutions
Solution 1. Counter of prefix\n\nUse counter C to collect every prefix of every word. \nThen sum up C[pre] for every prefix pre of word word, this is the score 
### Solution 1. Counter of prefix\n\nUse counter `C` to collect every prefix of every word. \nThen sum up `C[pre]` for every prefix `pre` of word `word`, this is the score of `word`.\n\n**python**\n\n```\nclass Solution:\n    def sumPrefixScores(self, W: List[str]) -> List[int]:\n        C = collections.defaultdict(int)\n        for w in W:\n            for i in range(len(w)):\n                C[w[:i + 1]] += 1\n                \n        ans = []\n        for w in W:\n            curr = 0\n            for i in range(len(w)):\n                curr += C[w[:i + 1]]\n            ans.append(curr)\n            \n        return ans\n```\n\n<br>\n<br>\n\n### Solution 2. Trie\n\nTake a look at how we build `trie`.\n\n![image](https://assets.leetcode.com/users/images/11a6d46a-44cd-4d8f-94a7-cd1ca5b14d1f_1663473701.177221.png)\n\nInsert `abc`.\n\n![image](https://assets.leetcode.com/users/images/05a58711-9c01-4edc-9071-2963879350ee_1663473704.8254855.png)\n\nInsert `ab`.\n\n![image](https://assets.leetcode.com/users/images/090d6123-0dd7-4f6e-b7b7-d96af187bf78_1663473707.7719986.png)\n\nInsert `b`.\n\n![image](https://assets.leetcode.com/users/images/3cda1658-6224-4212-b8f8-f3f4b254287b_1663473710.8782887.png)\n\nInsert `bc`.\n\n![image](https://assets.leetcode.com/users/images/d6e7474b-081a-4f40-8577-b9dfc735397c_1663473713.8538272.png)\n\nCount the score of `ab`.\n\n![image](https://assets.leetcode.com/users/images/9d5e4a25-a9f1-4d44-b0b8-543534c7a0e0_1663473716.9975448.png)\n\n**python**\n\n```\nclass Node:\n    def __init__(self):\n        self.children = {}\n        self.cnt = 0\n        \nclass Trie:\n    def __init__(self):\n        self.trie = Node()\n    def insert(self, word):\n        node = self.trie\n        for ch in word:\n            if ch not in node.children:\n                node.children[ch] = Node()\n            node = node.children[ch]\n            node.cnt += 1\n    def count(self, word):\n        node = self.trie\n        ans = 0\n        for ch in word:\n            ans += node.children[ch].cnt\n            node = node.children[ch]\n        return ans\n\nclass Solution:\n    def sumPrefixScores(self, A: List[str]) -> List[int]:\n        trie = Trie()\n\n        for a in A:\n            trie.insert(a)\n\n        return [trie.count(a) for a in A]\n```\n
[]