Skip to main content

Command Palette

Search for a command to run...

Replace All ?'s to Avoid Consecutive Repeating Characters

Published
2 min read
Replace All ?'s to Avoid Consecutive Repeating 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.

Given a string s containing only lowercase English letters and the '?' character, convert all the '?' characters into lowercase letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non '?' characters.

It is guaranteed that there are no consecutive repeating characters in the given string except for '?'.

Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.

LeetCode Problem - 1576

class Solution {
    public String modifyString(String s) {
        StringBuilder sb = new StringBuilder(); // StringBuilder to build the modified string
        int length = s.length(); // Get the length of the input string

        // Loop through each character in the input string
        for (int i = 0; i < length; i++) {
            char curr = s.charAt(i); // Get the current character

            // Check if the current character is a '?'
            if (curr == '?') {
                // Determine the previous character in the StringBuilder (or '-' if it's the first character)
                char prev = i > 0 ? sb.charAt(i - 1) : '-';
                // Determine the next character in the original string (or '-' if it's the last character)
                char next = i < length - 1 ? s.charAt(i + 1) : '-';

                // Choose 'a' if it's not the same as the previous or next character
                if (prev != 'a' && next != 'a') {
                    sb.append('a');
                } 
                // Choose 'b' if it's not the same as the previous or next character
                else if (prev != 'b' && next != 'b') {
                    sb.append('b');
                } 
                // Otherwise, choose 'c'
                else {
                    sb.append('c');
                }
            } 
            // If the current character is not a '?', append it to the StringBuilder
            else {
                sb.append(curr);
            }
        }

        // Convert the StringBuilder to a string and return it
        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.