Replace All Digits with Characters - Problem
You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its odd indices.
You must perform an operation shift(c, x), where c is a character and x is a digit, that returns the xth character after c.
- For example,
shift('a', 5) = 'f'andshift('x', 0) = 'x'.
For every odd index i, you want to replace the digit s[i] with the result of the shift(s[i-1], s[i]) operation.
Return s after replacing all digits. It is guaranteed that shift(s[i-1], s[i]) will never exceed 'z'.
Note that shift(c, x) is not a preloaded function, but an operation to be implemented as part of the solution.
Input & Output
Example 1 — Basic Case
$
Input:
s = "a1c1e1"
›
Output:
"abcdef"
💡 Note:
Digits at indices 1, 3, 5: '1' shifts 'a'→'b', '1' shifts 'c'→'d', '1' shifts 'e'→'f'. Result: a + b + c + d + e + f = "abcdef"
Example 2 — Different Shifts
$
Input:
s = "a1b2c3"
›
Output:
"abbdcf"
💡 Note:
Index 1: 'a' + 1 = 'b', Index 3: 'b' + 2 = 'd', Index 5: 'c' + 3 = 'f'. Result: a + b + b + d + c + f = "abbdcf"
Example 3 — Zero Shift
$
Input:
s = "a0c0e0"
›
Output:
"acacee"
💡 Note:
Zero shift means same character: 'a' + 0 = 'a', 'c' + 0 = 'c', 'e' + 0 = 'e'
Constraints
- 1 ≤ s.length ≤ 100
- s consists of lowercase English letters and digits
- s[i] is a lowercase English letter if i is even
- s[i] is a digit if i is odd
- shift(s[i-1], s[i]) ≤ 'z' for all odd indices i
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code