class Solution:
def replaceDigits(self, s: str) -> str:
res = []
for i in range(len(s)):
if i % 2 == 0:
res.append(s[i])
else:
# s[i] is a digit, s[i-1] is a letter
shift = int(s[i])
res.append(chr(ord(s[i-1]) + shift))
return ''.join(res)
class Solution {
public:
string replaceDigits(string s) {
for (int i = 1; i < s.size(); i += 2) {
s[i] = s[i-1] + (s[i] - '0');
}
return s;
}
};
class Solution {
public String replaceDigits(String s) {
char[] arr = s.toCharArray();
for (int i = 1; i < arr.length; i += 2) {
arr[i] = (char)(arr[i-1] + (arr[i] - '0'));
}
return new String(arr);
}
}
var replaceDigits = function(s) {
let arr = s.split('');
for (let i = 1; i < arr.length; i += 2) {
arr[i] = String.fromCharCode(arr[i-1].charCodeAt(0) + parseInt(arr[i]));
}
return arr.join('');
};
You are given a string s that consists of lowercase English letters and digits. The string has the following property: for every even index i, s[i] is a lowercase English letter, and for every odd index i, s[i] is a digit from '0' to '9'.
Your task is to replace every digit at an odd index i with a character that is the result of shifting the previous letter s[i-1] forward in the alphabet by the integer value of s[i]. For example, if s[i-1] = 'a' and s[i] = '2', then s[i] should be replaced with 'c'.
Return the resulting string after all replacements have been made.
At first glance, the problem looks like it could involve a lot of string manipulation and edge cases. A brute-force approach might involve building a new string character by character, checking each index to see if it's a digit, and then replacing it accordingly.
However, the structure of the input makes things simpler: every even index is a letter, and every odd index is a digit that should be replaced by shifting the previous letter. This regular pattern means we can process the string in pairs: for each even index (the letter), and the following odd index (the digit), we can easily compute the replacement.
Instead of building a complicated solution, we can iterate through the string, and whenever we encounter a digit, we use the previous character and the digit to compute the shifted letter. This is more efficient and easier to implement.
i:
i is even (i.e., i % 2 == 0), append s[i] (the letter) to the result.i is odd, calculate the new character by:
s[i-1] (previous letter) to its ASCII value.s[i] (digit) to an integer.This approach is efficient because:
Let's consider the input s = "a1c1e1".
At each odd index, we look at the previous character and the digit, and replace the digit with the shifted letter.
s. Each character is processed exactly once.The "Replace All Digits with Characters" problem leverages the regular structure of the input string: alternating letters and digits. By iterating through the string and using simple arithmetic to shift letters by digit values, we can efficiently build the resulting string in a single pass. The key insight is recognizing that each digit only depends on its immediately preceding letter, allowing for a straightforward and elegant solution with linear time and space complexity.