Replace All ?'s to Avoid Consecutive Repeating Characters

Given a string s containing only lower case English letters and the '?' character, convert all the '?' characters into lower case 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.

Example 1:

Input: s = "?zs"
Output: "azs"

Approach

Java

public class AvoidConsecutiveRepeatingChars {
    public static void main(String[] args) {
        String s = "?zs";
        System.out.println(modifyString(s));
    }

    static String modifyString(String str) {
        char[] s = str.toCharArray();
        int n = s.length;
        int i = 0;
        while (i < n) {
            if (i == 0) {
                if (n == 1 && s[i] == '?')
                    s[i] = 'a';
                else if (s[i] == '?') {
                    char x = s[i + 1];
                    if (x == '?')
                        s[i] = 'a';
                    else {
                        for (int j = 0; j < 26; j++) {
                            if (('a' + j) != x) {
                                s[i] = (char) ('a' + j);
                                break;
                            }
                        }
                    }
                }
            } else if (i == n - 1) {
                if (s[i] == '?') {
                    if (s[i - 1] == '?')
                        s[i] = 'a';
                    else {
                        for (int j = 0; j < 26; j++) {
                            if (s[i - 1] != ('a' + j)) {
                                s[i] = (char) ('a' + j);
                                break;
                            }
                        }
                    }
                }
            } else {
                if (s[i] == '?') {
                    char x = s[i - 1];
                    if (s[i + 1] == '?') {
                        for (int j = 0; j < 26; j++) {
                            if (x != ('a' + j)) {
                                s[i] = (char) ('a' + j);
                                break;
                            }
                        }
                    } else {
                        x = s[i - 1];
                        char y = s[i + 1];
                        for (int j = 0; j < 26; j++) {
                            if (x != ('a' + j) && y != ('a' + j)) {
                                s[i] = (char) ('a' + j);
                                break;
                            }
                        }
                    }
                }
            }
            i++;
        }
        return new String(s);
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

string modifyString(string s)
{
    int n = s.size();
    int i = 0;
    while (i < n)
    {
        if (i == 0)
        {
            if (n == 1 && s[i] == '?')
                s[i] = 'a';
            else if (s[i] == '?')
            {
                char x = s[i + 1];
                if (x == '?')
                    s[i] = 'a';
                else
                {
                    for (int j = 0j < 26j++)
                    {
                        if (('a' + j) != x)
                        {
                            s[i] = 'a' + j;
                            break;
                        }
                    }
                }
            }
        }
        else if (i == n - 1)
        {
            if (s[i] == '?')
            {
                if (s[i - 1] == '?')
                    s[i] = 'a';
                else
                {
                    for (int j = 0j < 26j++)
                    {
                        if (s[i - 1] != ('a' + j))
                        {
                            s[i] = 'a' + j;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            if (s[i] == '?')
            {
                char x = s[i - 1];
                if (s[i + 1] == '?')
                {
                    for (int j = 0j < 26j++)
                    {
                        if (x != ('a' + j))
                        {
                            s[i] = 'a' + j;
                            break;
                        }
                    }
                }
                else
                {
                    char x = s[i - 1];
                    char y = s[i + 1];
                    for (int j = 0j < 26j++)
                    {
                        if (x != ('a' + j) && y != ('a' + j))
                        {
                            s[i] = 'a' + j;
                            break;
                        }
                    }
                }
            }
        }
        i++;
    }
    return s;
}

int main()
{
    string s = "?zs";
    cout << modifyString(s);
    return 0;
}


No comments:

Post a Comment