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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1ms, memory 17.5MB, accepted 2025-12-29.
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