1844. Replace All Digits with Characters
EasyView on LeetCode
Time: O(n)
Space: O(n)
Problem Overview
Replace All Digits with Characters (Easy) asks you to solve a structured algorithmic task. This is a common String pattern in coding interviews. For each even-index letter, shift the following digit to produce the target char.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
For each even-index letter, shift the following digit to produce the target char.
Related patterns: String
1844.cs
C#
// Approach: For each even-index letter, shift the following digit to produce the target char.
// Time: O(n) Space: O(n)
public class Solution
{
public string ReplaceDigits(string s)
{
char[] chars = s.ToCharArray();
for (int i = 1; i < chars.Length; i += 2)
chars[i] = (char)(chars[i] + chars[i - 1] - '0');
return new string(chars);
}
}Was this solution helpful?