1844. Replace All Digits with Characters LeetCode Solution

In this guide, you will get 1844. Replace All Digits with Characters LeetCode Solution with the best time and space complexity. The solution to Replace All Digits with Characters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Replace All Digits with Characters solution in C++
  4. Replace All Digits with Characters solution in Java
  5. Replace All Digits with Characters solution in Python
  6. Additional Resources
1844. Replace All Digits with Characters LeetCode Solution image

Problem Statement of Replace All Digits with Characters

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’ and shift(‘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.

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’

See also  162. Find Peak ElementLeetCode Solution

Constraints:

1 <= s.length <= 100
s consists only of lowercase English letters and digits.
shift(s[i-1], s[i]) <= 'z' for all odd indices i.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1844. Replace All Digits with Characters LeetCode Solution in C++

class Solution {
 public:
  string replaceDigits(string s) {
    for (int i = 1; i < s.length(); i += 2)
      s[i] += s[i - 1] - '0';
    return s;
  }
};
/* code provided by PROGIEZ */

1844. Replace All Digits with Characters LeetCode Solution in Java

class Solution {
  public String replaceDigits(String s) {
    char[] chars = s.toCharArray();
    for (int i = 1; i < chars.length; i += 2)
      chars[i] += chars[i - 1] - '0';
    return String.valueOf(chars);
  }
}
// code provided by PROGIEZ

1844. Replace All Digits with Characters LeetCode Solution in Python

class Solution:
  def replaceDigits(self, s: str) -> str:
    return ''.join(
        c if i % 2 == 0 else chr(ord(s[i - 1]) + int(c))
        for i, c in enumerate(s)
    )
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.