2416. Sum of Prefix Scores of Strings
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 61% Topics: Array, String, Trie, Counting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n * l), n is the number of words, l is the max length of words
# Space: O(t), t is the size of trie
import collections
# trie
class Solution(object):
def sumPrefixScores(self, words):
"""
:type words: List[str]
:rtype: List[int]
"""
_trie = lambda: collections.defaultdict(_trie)
trie = _trie()
for w in words:
curr = trie
for c in w:
curr = curr[c]
curr["_cnt"] = curr["_cnt"]+1 if "_cnt" in curr else 1
result = []
for w in words:
cnt = 0
curr = trie
for c in w:
curr = curr[c]
cnt += curr["_cnt"]
result.append(cnt)
return result
Solution from kamyu104/LeetCode-Solutions · MIT