Sum of Digits of String After Convert (LeetCode 1945): Solution and Explanation

Easy
String
Simulation

Replace every letter of a lowercase string with its alphabet position (a = 1 to z = 26) to form one long number, then replace the number with the sum of its digits k times. Return the final number.

Intuition

The converted number can be 200 digits long, which does not fit in any built-in integer. Luckily, the first transform only asks for its digit sum, and you never need the number itself.

The digit sum of a concatenation is the sum of the digit sums of its pieces. So each letter adds the digits of its position, for example 'z' = 26 adds 2 + 6 = 8. After that first pass the number is at most 800, and the remaining k - 1 transforms are ordinary digit sums of a small integer.

Approach: Fold conversion into the first transform

  1. Set num = 0.
  2. For each letter, compute its position v and add v / 10 + v % 10 to num. This is transform 1.
  3. Repeat k - 1 times: replace num with the sum of its digits, peeling them off with % 10 and / 10.
  4. Return num.

Step-by-step example

s = "zbax", k = 2
  1. The positions are z = 26, b = 2, a = 1, x = 24, so the converted number would be 262124.
  2. Transform 1 adds the digits letter by letter: 2 + 6 = 8, then + 2 = 10, then + 1 = 11, then + 2 + 4 = 17.
  3. Transform 2 sums the digits of 17: 7 + 1 = 8.
  4. k transforms are done, so the answer is 8.

Code: Python, Java, C++

1class Solution:
2    def getLucky(self, s: str, k: int) -> int:
3        num = 0
4        for c in s:
5            v = ord(c) - ord('a') + 1
6            num += v // 10 + v % 10
7        for _ in range(k - 1):
8            total = 0
9            while num > 0:
10                total += num % 10
11                num //= 10
12            num = total
13        return num
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + k)

One pass over the n letters, then k - 1 digit sums of a number with at most 3 digits.

Space

O(1)

Only the running number and the digit total are stored.

Other approaches

Build the digit string, then sum it k times (time O(n + k), space O(n))

Concatenate the positions into a string and sum its characters as digits, k times. It follows the statement literally but builds a string of up to 200 characters first.

Common mistakes

  • Parsing the converted string as an integer. For 100 letters it has up to 200 digits and overflows.
  • Running k digit-sum passes after the conversion pass, which applies one transform too many.
  • Treating a two-digit position like 26 as a single value 26 in the first sum instead of its digits 2 and 6.

Frequently asked questions

Why does adding each letter's digit sum give the same result as summing the long number?

Concatenation just places the digits side by side, so the multiset of digits is the same either way. A digit sum only depends on which digits appear.

How large can the number get after the first transform?

Each letter contributes at most 1 + 9 = 10 (for 's' = 19), so 100 letters give at most 1000.

What happens when k is large?

After two or three transforms the number is a single digit, and the digit sum of a single digit is itself, so extra transforms change nothing.