Leetcode 1844. Replace All Digits with Characters
Problem Explanation
The problem statement asks us to replace the digits in a given string s
with the corresponding shifted character. The string s
contains alternating lowercase English letters (at even positions) and digits (at odd positions). The shifting is given by the function shift(c, x)
, where c
is a character and x
is a digit, which returns the xth character after c
.
The task is to replace all the digits in the given string using the shifting rule and return the modified string.
Example Walkthrough
Let's walk through the given example:
Input: s = "a1c1e1"
Output: "abcdef"
Explanation:
Here, we have to replace the digits at odd indices as follows:
- s[1] (which is '1') -> shift('a',1) = 'b'
- s[3] (which is '1') -> shift('c',1) = 'd'
- s[5] (which is '1') -> shift('e',1) = 'f'
So, the modified string is "abcdef".
Solution Approach
The solution takes advantage of the string length and the fact that the digits are at odd indices. We can iterate through the string, and for each odd index i
, we update the character at that index using the shift function. Since s[i]
is a digit, we can directly add it to the previous character (subtracting '0' to convert it from ASCII code to the integer value).
Algorithm Steps
- Initialize an empty result string.
- Iterate through the string
s
with a step of 2:- For each iteration, append the character at the current index
i
to the result string. - Calculate the shifted character using the digit at the next index
i+1
and append it to the result string.
- For each iteration, append the character at the current index
- Return the result string.
Now, let's write the code for the solution:
Python Solution
1class Solution:
2 def replaceDigits(self, s: str) -> str:
3 result = ""
4 for i in range(0, len(s), 2):
5 result += s[i] # Append the character at the current index
6 # Calculate the shifted character and append it (if within bounds)
7 if i + 1 < len(s):
8 result += chr(ord(s[i]) + int(s[i + 1]))
9 return result
Java Solution
1class Solution {
2 public String replaceDigits(String s) {
3 StringBuilder result = new StringBuilder();
4 for (int i = 0; i < s.length(); i += 2) {
5 result.append(s.charAt(i)); // Append the character at the current index
6 // Calculate the shifted character and append it (if within bounds)
7 if (i + 1 < s.length()) {
8 char shiftedChar = (char) (s.charAt(i) + Character.getNumericValue(s.charAt(i + 1)));
9 result.append(shiftedChar);
10 }
11 }
12 return result.toString();
13 }
14}
JavaScript Solution
1class Solution {
2 replaceDigits(s) {
3 let result = "";
4 for (let i = 0; i < s.length; i += 2) {
5 result += s[i]; // Append the character at the current index
6 // Calculate the shifted character and append it (if within bounds)
7 if (i + 1 < s.length) {
8 result += String.fromCharCode(s.charCodeAt(i) + parseInt(s[i + 1], 10));
9 }
10 }
11 return result;
12 }
13}
C++ Solution
1class Solution {
2public:
3 string replaceDigits(string s) {
4 string result = "";
5 for (int i = 0; i < s.length(); i += 2) {
6 result += s[i]; // Append the character at the current index
7 // Calculate the shifted character and append it (if within bounds)
8 if (i + 1 < s.length()) {
9 result += char(s[i] + (s[i + 1] - '0'));
10 }
11 }
12 return result;
13 }
14};
C# Solution
1public class Solution {
2 public string ReplaceDigits(string s) {
3 StringBuilder result = new StringBuilder();
4 for (int i = 0; i < s.Length; i += 2) {
5 result.Append(s[i]); // Append the character at the current index
6 // Calculate the shifted character and append it (if within bounds)
7 if (i + 1 < s.Length) {
8 char shiftedChar = (char) (s[i] + (s[i + 1] - '0'));
9 result.Append(shiftedChar);
10 }
11 }
12 return result.ToString();
13 }
14}
15```### Conclusion
16
17To solve this problem, we looped through the string in steps of 2 and replaced the digits with the corresponding shifted characters using the given formula. We have provided solutions in different programming languages: Python, Java, JavaScript, C++, and C#. All the solutions have a time complexity of O(n), where n is the size of the input string `s`.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.