Sum of Digits of String After Convert: Letter-to-Number Simulation
LeetCode 1945. Sum of Digits of String After Convert gives you a lowercase string s and an integer k. You need to convert each character to its position in the alphabet (a=1, b=2, ..., z=26), concatenate all those numbers into a single digit string, and then repeatedly sum the digits of that string k times. Return the resulting integer.
It is a simulation problem. There is no trick or clever data structure needed. You just follow the instructions carefully.
The problem
Given s = "leetcode" and k = 2:
- Replace each letter with its alphabet position: l=12, e=5, e=5, t=20, c=3, o=15, d=4, e=5.
- Concatenate to get the digit string
"1255203154". - Sum the digits (round 1): 1+2+5+5+2+0+3+1+5+4 = 28.
- Sum the digits again (round 2): 2+8 = 10.
- Return
10.
The approach
Break the problem into two phases.
Phase 1: Convert. Iterate through each character in the string. For each character c, compute ord(c) - ord('a') + 1 to get its alphabet position. Convert that integer to a string and append it to a running result. Note that letters like t (position 20) contribute two digits, not one.
Phase 2: Sum digits k times. Take the digit string and sum all its individual digits to produce an integer. Repeat this k times total. After the first sum you already have an integer, so for subsequent rounds you convert that integer back to a string, sum its digits, and repeat.
Visual walkthrough
Here is the full process for s = "leetcode" with k = 2, shown step by step.
Step 1: Convert letters to digit string
l=12, e=5, e=5, t=20, c=3, o=15, d=4, e=5
Replace each letter with its position in the alphabet and concatenate everything into one string of digits.
Step 2: First digit sum (k=1)
1+2+5+5+2+0+3+1+5+4 = 28Sum every individual digit in the string. 1+2+5+5+2+0+3+1+5+4 = 28. One round of summing done.
Step 3: Second digit sum (k=2)
2+8 = 10Sum the digits of 28: 2+8 = 10. We have completed k=2 rounds. Return 10.
After two rounds of digit summing, the final answer is 10. If k were 3, you would do one more round: 1+0 = 1.
The code
def get_lucky(s: str, k: int) -> int:
digit_str = "".join(str(ord(c) - ord("a") + 1) for c in s)
for _ in range(k):
digit_str = str(sum(int(d) for d in digit_str))
return int(digit_str)
The first line builds the digit string. For each character, ord(c) - ord('a') + 1 gives its alphabet position (a=1, z=26), and str(...) converts multi-digit positions like 20 into the characters "2" and "0". Then the for loop runs k times, each iteration summing the digits and converting back to a string. The final int(...) returns the answer as an integer.
Complexity analysis
| Complexity | |
|---|---|
| Time | O(n + k * m) |
| Space | O(n) |
Here n is the length of the input string and m is the number of digits in the concatenated number string. In the worst case (all z's), m is at most 2n since each letter maps to at most a two-digit number. The k factor is tiny in practice because digit sums shrink rapidly. After the first summation the number is already small, so subsequent rounds are essentially O(1). The space comes from building the digit string.
Building blocks
1. Character-to-number mapping
Converting a character to its alphabet position with ord(c) - ord('a') + 1 is a micro-pattern that appears in many string problems. You will see it in column title conversions, Caesar ciphers, and encoding problems.
2. Digit sum (repeated reduction)
Summing the digits of a number until you reach a target condition is its own recurring pattern. The same idea shows up in Add Digits (LeetCode 258), where you reduce repeatedly until you get a single digit. Recognizing this as a standalone building block makes it easy to apply whenever a problem asks you to collapse a number by summing its parts.
Edge cases
- Single character with k=1:
s = "a",k = 1. The digit string is"1", and summing its digits gives 1. No iteration needed beyond the one required round. - All same letters:
s = "zzzz",k = 1. Each z maps to 26, so the digit string is"26262626". The sum is 2+6+2+6+2+6+2+6 = 32. - k is large: Even with a long string, after one or two rounds of digit summing the number is small. Additional rounds have almost no effect. For example, 32 becomes 5 on the next round and stays at 5 forever.
- Letters that map to single digits: Characters a through i map to positions 1 through 9 (single digit). Characters j through z map to 10 through 26 (two digits). Make sure your conversion handles both correctly by converting the position to a string rather than treating it as a single character.
From understanding to recall
You can read this solution and understand every line. But in two weeks, when you sit down for a coding interview, will you remember the details? Will you remember to convert each letter's position to a string before concatenating? Will you remember that the digit-sum loop runs exactly k times, not until convergence?
Spaced repetition locks in these details. CodeBricks breaks this problem into its building blocks, character mapping and digit summing, and quizzes you on each one at increasing intervals. Instead of re-reading the solution, you reconstruct it from understanding. After a few review sessions, the pattern becomes automatic.
Related posts
- Add Digits - Repeatedly sum digits until a single digit remains, the same digit-sum reduction pattern taken to its limit
- Add Strings - Digit-by-digit string arithmetic, another problem where you process numeric characters one at a time
- Happy Number - Repeated numeric transformation with cycle detection, a similar "keep processing until done" simulation