Sum of Digits of String After Convert (LeetCode 1945): Solution and Explanation
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
- Set
num = 0. - For each letter, compute its position
vand addv / 10 + v % 10tonum. This is transform 1. - Repeat
k - 1times: replacenumwith the sum of its digits, peeling them off with% 10and/ 10. - Return
num.
Step-by-step example
s = "zbax", k = 2
- The positions are z = 26, b = 2, a = 1, x = 24, so the converted number would be 262124.
- Transform 1 adds the digits letter by letter: 2 + 6 = 8, then + 2 = 10, then + 1 = 11, then + 2 + 4 = 17.
- Transform 2 sums the digits of 17: 7 + 1 = 8.
- 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 num1class Solution {
2 public int getLucky(String s, int k) {
3 int num = 0;
4 for (char c : s.toCharArray()) {
5 int v = c - 'a' + 1;
6 num += v / 10 + v % 10;
7 }
8 for (int r = 1; r < k; r++) {
9 int total = 0;
10 while (num > 0) {
11 total += num % 10;
12 num /= 10;
13 }
14 num = total;
15 }
16 return num;
17 }
18}1class Solution {
2public:
3 int getLucky(string s, int k) {
4 int num = 0;
5 for (char c : s) {
6 int v = c - 'a' + 1;
7 num += v / 10 + v % 10;
8 }
9 for (int r = 1; r < k; r++) {
10 int total = 0;
11 while (num > 0) {
12 total += num % 10;
13 num /= 10;
14 }
15 num = total;
16 }
17 return num;
18 }
19};Time and space complexity
O(n + k)
One pass over the n letters, then k - 1 digit sums of a number with at most 3 digits.
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.