Skip to main content

Command Palette

Search for a command to run...

Replace All Digits with Characters

Updated
2 min read
Replace All Digits with Characters
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its odd indices.

There is a function shift(c, x), where c is a character and x is a digit, that returns the x<sup>th</sup> 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 shift(s[i-1], s[i]).

Return safter replacing all digits. It isguaranteedthatshift(s[i-1], s[i])will never exceed'z'.

LeetCode Problem - 1844

class Solution {
    public String replaceDigits(String s) {
        // StringBuilder to efficiently build the modified string
        StringBuilder sb = new StringBuilder();
        // Iterate through each character in the input string `s`
        for (int i = 0; i < s.length(); i++) {
            // If the index `i` is odd (representing a digit in the string)
            if (i % 2 != 0) {
                // Convert the character at index `i` into an integer value representing the digit
                int val = s.charAt(i) - '0';
                // Get the character before the digit
                char temp = s.charAt(i - 1);
                // Calculate the character that comes after `temp` by `val` positions in the alphabet
                // and convert it to a String
                String funChar = String.valueOf((char) (temp + val));
                // Append the calculated character to the StringBuilder
                sb.append(funChar);
            } else {
                // If the index `i` is even (representing a non-digit character), simply append it to the StringBuilder
                sb.append(s.charAt(i));
            }
        }
        // Convert the StringBuilder to a String and return the modified string
        return sb.toString();
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.