[Python3] Simple trie O (n*k) with line by line comments.
The idea is to use trie. When initializing the trie, instead of storing the end of the word, we are tracking the number of times this prefix has occurred.\nOnce
The idea is to use trie. When initializing the trie, instead of storing the end of the word, we are tracking the number of times this prefix has occurred.\nOnce we have the trie, we will go through all the words one more time and count how many times its prefix has occurred to build our result.\n\n```\nclass Solution:\n    def sumPrefixScores(self, words: List[str]) -> List[int]:\n        trie = {} ### Initialize a hash map for trie\n        for w in words:\n            cur = trie ### At the begining of each word, the pointer should point to the begining of the trie\n            for c in w:\n                ### Go through each letter to build the trie.\n                if c not in cur:\n                    cur[c] = {}\n                cur = cur[c]\n                ### Instead of store the end of the word in the regular trie, \n                ### we are storing the occurrences of the prefix at each letter\n                if \'count\' not in cur:\n                    cur[\'count\'] = 0\n                cur[\'count\'] += 1\n        res = []\n        ### Go through each word one more time to build the result\n        for w in words:\n            cur = trie ### Pointer point to the begining of the trie\n            count = 0 ### Keep track the count of the occurrences of each prefix (letter) in this word\n            for c in w:\n                cur = cur[c]\n                ### At each letter, we add the count we previously stored. \n                ### This is the number of occurrence of this prefix among all words.\n                count += cur[\'count\']\n            res.append(count)\n        return res\n```\n\n**Upvote** if you like this post.\n\n**Connect with me on [LinkedIn](https://www.linkedin.com/in/meida-chen-938a265b/)** if you\'d like to discuss other related topics\n\n\uD83C\uDF1F If you are interested in **Machine Learning** || **Deep Learning** || **Computer Vision** || **Computer Graphics** related projects and topics, check out my **[YouTube Channel](https://www.youtube.com/@meidachen8489)**! Subscribe for regular updates and be part of our growing community.
[]