#1844

Replace All Digits with Characters

newbie · 155 · lc easy +14 · premium · verified · 82.7% accepted · 896 likes · top 94%

Description

You are given a 0-indexed string s where even-indexed positions hold lowercase letters and odd-indexed positions hold digit characters.

The operation shift(c, x) returns the character x positions after c in the alphabet (wrapping is not needed here).

For every odd index i, replace the digit s[i] with the character shift(s[i-1], s[i]).

Return the final string after all replacements. It is guaranteed no result exceeds 'z'.

Example 1:

Input: s = "a1c1e1"
Output: "abcdef"
Explanation: The digits are replaced as follows:
- s[1] -> shift('a',1) = 'b'
- s[3] -> shift('c',1) = 'd'
- s[5] -> shift('e',1) = 'f'

Example 2:

Input: s = "a1b2c3d4e"
Output: "abbdcfdhe"
Explanation: The digits are replaced as follows:
- s[1] -> shift('a',1) = 'b'
- s[3] -> shift('b',2) = 'd'
- s[5] -> shift('c',3) = 'f'
- s[7] -> shift('d',4) = 'h'

Code

1
2
3