LeetCode solutions

2076. Sum of Digits of String After Convert

My accepted Python solution to LeetCode problem 2076, Sum of Digits of String After Convert, running in 1ms.

  • Difficulty: Easy
  • Python
  • Runtime 1ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 1ms, memory 17.5MB, accepted 2025-12-29.

python
class Solution:
    def getLucky(self, s: str, k: int) -> int:
        # Convert to numbers, then sum digits k times
        # Time: O(n + k*d), Space: O(d) where d is max digits
        
        # Convert string to number string
        num_str = ''.join(str(ord(c) - ord('a') + 1) for c in s)
        
        # Sum digits k times
        result = sum(int(d) for d in num_str)
        for _ in range(k - 1):
            result = sum(int(d) for d in str(result))
        
        return result

Source