Replace All Digits with Characters — Problem & Solution
Replace All Digits with Characters solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Replace All Digits with Characters is an easy coding problem on strings, asked in Amazon and Infosys interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Infosys
- 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
You are given a 0-indexed string s whose even indices hold lowercase letters and whose odd indices hold digits.
For every odd index i, replace s[i] with the letter obtained by shifting s[i-1] forward by s[i] positions in the alphabet. Shifting never goes past 'z'.
Return the resulting string.
Example 1
Input: s = "a1c1e1"
Output: abcdef
Explanation: a shifted by 1 is b, c shifted by 1 is d, e shifted by 1 is f.
Example 2
Input: s = "a1b2c3d4"
Output: abbdcfdh
Example 3
Input: s = "z"
Output: z
Explanation: No odd index exists.
Constraints
1 <= s.length <= 100Even indices hold lowercase letters; odd indices hold digits.The shift never moves a letter past 'z'.
Hints for Replace All Digits with Characters
Hint 1
Work on a mutable character array, not the immutable string.
Hint 2
Only odd indices change, and each depends on the character immediately before it.
Hint 3
Shifting means adding the digit's numeric value to the letter's code point.
How to solve Replace All Digits with Characters
Each odd position is a pure function of the letter at the previous even position and the digit itself — no state carries across pairs.
Approach
- Copy the string into a character array.
- For every odd index
i, seta[i] = a[i-1] + (a[i] - '0')as a character. - Join the array back into a string.
Why it works
The problem guarantees a letter at every even index, so a[i-1] is always a letter when i is odd — and because only odd slots are written, the letters the shifts read are never modified.
Complexity
- Time —
O(n) - Space —
O(n)
Pitfalls
- Using the already replaced previous character chains the shifts together; each pair must use the original even-index letter, which is untouched anyway if you only write odd slots.
- Converting the digit character with its raw code point instead of its numeric value shifts by about 48 positions.
Reference solution
Python
def replaceDigits(s: str) -> str:
a = list(s)
for i in range(1, len(a), 2):
a[i] = chr(ord(a[i - 1]) + int(a[i]))
return ''.join(a)Java
public static String replaceDigits(String s) {
char[] a = s.toCharArray();
for (int i = 1; i < a.length; i += 2) {
a[i] = (char) (a[i - 1] + (a[i] - '0'));
}
return new String(a);
}C++
string replaceDigits(string s) {
for (int i = 1; i < (int) s.size(); i += 2) {
s[i] = (char) (s[i - 1] + (s[i] - '0'));
}
return s;
}JavaScript
var replaceDigits = function(s) {
const a = s.split("");
for (let i = 1; i < a.length; i += 2) {
a[i] = String.fromCharCode(a[i - 1].charCodeAt(0) + Number(a[i]));
}
return a.join("");
};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