Replace All Digits with Characters
This problem can be solved by iterating through the string s
and performing the shift operation for the digits at the odd indices. Remember that Python strings are immutable, so we cannot directly change a character at a certain index. We will create a new result string to hold the modified string.
Python code:
|
|
In the replaceDigits
method, we first initialize an empty string result
. We then iterate over the length of the string s
using a for loop.
For each index i
, we check if i
is odd by checking i % 2 != 0
. If i
is odd, we perform the shift operation on s[i-1]
by int(s[i])
using the ord
and chr
functions and add the result to result
. If i
is even, we simply add s[i]
to result
.
Finally, we return the modified string result
.