Sum of Digits of String After Convert — Problem & Solution
Sum of Digits of String After Convert solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Sum of Digits of String After Convert is an easy coding problem on strings and simulation, asked in Amazon and Adobe interviews.
- Difficulty: Easy
- Topics: Strings, Simulation
- Asked at: Amazon, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Convert a string s to an integer by replacing each letter with its position in the alphabet (a → 1, b → 2, …, z → 26) and concatenating the results.
Then transform the number k times, where one transform replaces it with the sum of its digits.
Return the resulting integer.
Example 1
Input: s = "iiii", k = 1
Output: 36
Explanation: "iiii" becomes "9999", whose digits sum to 36.
Example 2
Input: s = "leetcode", k = 2
Output: 6
Explanation: "leetcode" → 12552031545 → 33 → 6.
Example 3
Input: s = "zbax", k = 2
Output: 8
Constraints
1 <= s.length <= 1001 <= k <= 10s consists of lowercase English letters.
Hints for Sum of Digits of String After Convert
Hint 1
Build the converted value as a string, not a number — it can be 200 digits long.
Hint 2
The first digit sum collapses it to a small integer immediately.
Hint 3
After that, each further transform is a plain digit sum of a small number.
How to solve Sum of Digits of String After Convert
Only the first transform faces a huge number, and it is applied to a string of digits. Everything after that operates on values below 1000, so ordinary integers suffice.
Approach
- Map each letter to
c - 'a' + 1and append its decimal digits to a string. - Repeat
ktimes: sum the characters of the current digit string, then replace the string with the sum's decimal form. - Return the final sum.
Why it works
A 100-letter string converts to at most 200 digits, so the first digit sum is at most 1800 — small enough that every later transform is trivial. Working on the string sidesteps the overflow entirely.
Complexity
- Time —
O(n + k log n) - Space —
O(n)
Pitfalls
- Parsing the converted string into an integer overflows for inputs longer than about 10 letters.
- A two-digit letter value such as
z→ 26 contributes two digits to the string. ktransforms are always applied, even once the value is a single digit.
Reference solution
Python
def getLucky(s: str, k: int) -> int:
digits = ''.join(str(ord(c) - 96) for c in s)
n = 0
for _ in range(k):
n = sum(int(d) for d in digits)
digits = str(n)
return nJava
public static int getLucky(String s, int k) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) sb.append(s.charAt(i) - 96);
String digits = sb.toString();
int n = 0;
for (int step = 0; step < k; step++) {
n = 0;
for (int i = 0; i < digits.length(); i++) n += digits.charAt(i) - '0';
digits = Integer.toString(n);
}
return n;
}C++
int getLucky(string s, int k) {
string digits;
for (char c : s) digits += to_string((int) c - 96);
int n = 0;
for (int step = 0; step < k; step++) {
n = 0;
for (char d : digits) n += d - '0';
digits = to_string(n);
}
return n;
}JavaScript
var getLucky = function(s, k) {
let digits = "";
for (let i = 0; i < s.length; i++) digits += String(s.charCodeAt(i) - 96);
let n = 0;
for (let step = 0; step < k; step++) {
n = 0;
for (let i = 0; i < digits.length; i++) n += digits.charCodeAt(i) - 48;
digits = String(n);
}
return n;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings