LeetCode solutions

2494. Sum of Prefix Scores of Strings

My accepted Python solution to LeetCode problem 2494, Sum of Prefix Scores of Strings, running in 1222ms.

  • Difficulty: Hard
  • Python
  • Runtime 1222ms
  • Memory 212.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 1222ms, memory 212.9MB, accepted 2026-01-02.

python
class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        # Build a Trie with count at each node
        trie = {}
        for word in words:
            node = trie
            for c in word:
                if c not in node:
                    node[c] = {'count': 0}
                node = node[c]
                node['count'] += 1
        
        # Calculate scores for each word
        result = []
        for word in words:
            node = trie
            score = 0
            for c in word:
                node = node[c]
                score += node['count']
            result.append(score)
        return result

Source