Replace ‘?’ in a string such that no two adjacent characters are same
Last Updated :
16 Jul, 2021
Given a string S of length N consisting of “?” and lowercase letters, the task is to replace “?” with lowercase letters such that no adjacent characters are the same. If more than one possible combination exists, print any one of them.
Examples:
Input: S = “?a?a”
Output: baba
Explanation:
Replacing all ‘?’s with ‘b’ modifies the string to “baba”.
Since no adjacent characters in “baba” are the same, print the string as the answer.
Input: S = “???”
Output: aca
Explanation:
Replace first ‘?’ with ‘a’.
Replace second ‘?’ with ‘c’.
Replace third ‘?’ with ‘a’. Now, the modified string is “aca”.
Therefore, there are no adjacent characters in “ca” which are same.
Naive Approach: The simplest approach is to try generating all possible permutations of the given string consisting of lowercase letters. There can be 26N strings. In each of these strings, check whether adjacent characters matches or not and all lowercase characters in the given string matches the chosen permutation of the string.
Time Complexity: O(N*26N), where N is the length of the given string.
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to replace every ‘?’ by the character ‘a’ and check if this character is equal to the adjacent character or not. If it is equal to the adjacent character then increment the current character. Below are the steps:
- If the first character of the string is ‘?’ then replace it with ‘a’ and if it is equal to the next character then increment the current character by 1
- Traverse the given string using a variable i over the range [1, N – 1] and if the current character is ‘?’ and do the following:
- Update character at index i as s[i] = ‘a’.
- Now if the character at index i and (i – 1) are the same then increment the current character by 1.
- Now if the character at index i and (i + 1) are the same then increment the current character by 1.
- Now if the character at index i and (i – 1) are the same again, then increment the current character by 1. This step is mandatory because after increment character in the above step it might be possible character at index i and (i – 1) are the same.
- If the last character of the string is ‘?’ then replace it with ‘a’ and if it is equal to the previous character then increment the last character by 1
- Print the string after the above steps.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
string changeString(string S)
{
string s = S;
int N = ( int )s.length();
if (s[0] == '?' ) {
s[0] = 'a' ;
if (s[0] == s[1]) {
s[0]++;
}
}
for ( int i = 1; i < N - 1; i++) {
if (s[i] == '?' ) {
s[i] = 'a' ;
if (s[i] == s[i - 1]) {
s[i]++;
}
if (s[i] == s[i + 1]) {
s[i]++;
}
if (s[i] == s[i - 1]) {
s[i]++;
}
}
}
if (s[N - 1] == '?' ) {
s[N - 1] = 'a' ;
if (s[N - 1] == s[N - 2]) {
s[N - 1]++;
}
}
return s;
}
int main()
{
string S = "?a?a" ;
cout << changeString(S);
return 0;
}
|
Java
class GFG{
static String changeString(String S)
{
char []s = S.toCharArray();
int N = ( int )S.length();
if (s[ 0 ] == '?' )
{
s[ 0 ] = 'a' ;
if (s[ 0 ] == s[ 1 ])
{
s[ 0 ]++;
}
}
for ( int i = 1 ; i < N - 1 ; i++)
{
if (s[i] == '?' )
{
s[i] = 'a' ;
if (s[i] == s[i - 1 ])
{
s[i]++;
}
if (s[i] == s[i + 1 ])
{
s[i]++;
}
if (s[i] == s[i - 1 ])
{
s[i]++;
}
}
}
if (s[N - 1 ] == '?' )
{
s[N - 1 ] = 'a' ;
if (s[N - 1 ] == s[N - 2 ])
{
s[N - 1 ]++;
}
}
String ans = "" ;
for ( int i = 0 ; i < s.length; i++)
{
ans += s[i];
}
return ans;
}
public static void main(String[] args)
{
String S = "?a?a" ;
System.out.print(changeString(S));
}
}
|
Python3
def changeString(S):
N = len (S)
s = [ ' ' ] * ( len (S))
for i in range ( len (S)):
s[i] = S[i]
if (s[ 0 ] = = '?' ):
s[ 0 ] = 'a'
if (s[ 0 ] = = s[ 1 ]):
s[ 0 ] = chr ( ord (s[ 0 ]) + 1 )
for i in range ( 1 , N - 1 ):
if (s[i] = = '?' ):
s[i] = 'a'
if (s[i] = = s[i - 1 ]):
s[i] = chr ( ord (s[i]) + 1 )
if (s[i] = = s[i + 1 ]):
s[i] = chr ( ord (s[i]) + 1 )
if (s[i] = = s[i - 1 ]):
s[i] = chr ( ord (s[i]) + 1 )
if (s[N - 1 ] = = '?' ):
s[N - 1 ] = 'a'
if (s[N - 1 ] = = s[N - 2 ]):
s[N - 1 ] + = 1
ans = ""
for i in range ( len (s)):
ans + = s[i]
return ans
if __name__ = = '__main__' :
S = "?a?a"
print (changeString(S))
|
C#
using System;
class GFG{
static string changeString( string S)
{
char []s = S.ToCharArray();
int N = S.Length;
if (s[0] == '?' )
{
s[0] = 'a' ;
if (s[0] == s[1])
{
s[0]++;
}
}
for ( int i = 1; i < N - 1; i++)
{
if (s[i] == '?' )
{
s[i] = 'a' ;
if (s[i] == s[i - 1])
{
s[i]++;
}
if (s[i] == s[i + 1])
{
s[i]++;
}
if (s[i] == s[i - 1])
{
s[i]++;
}
}
}
if (s[N - 1] == '?' )
{
s[N - 1] = 'a' ;
if (s[N - 1] == s[N - 2])
{
s[N - 1]++;
}
}
string ans = "" ;
for ( int i = 0; i < s.Length; i++)
{
ans += s[i];
}
return ans;
}
public static void Main()
{
string S = "?a?a" ;
Console.WriteLine(changeString(S));
}
}
|
Javascript
<script>
function changeString(S)
{
let s = S.split( "" );
let N = S.length;
if (s[0] == '?' )
{
s[0] = 'a' ;
if (s[0] == s[1])
{
s[0] = String.fromCharCode(s[0].charCodeAt(0)+1);
}
}
for (let i = 1; i < N - 1; i++)
{
if (s[i] == '?' )
{
s[i] = 'a' ;
if (s[i] == s[i - 1])
{
s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);
}
if (s[i] == s[i + 1])
{
s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);
}
if (s[i] == s[i - 1])
{
s[i]=String.fromCharCode(s[i].charCodeAt(0)+1);
}
}
}
if (s[N - 1] == '?' )
{
s[N - 1] = 'a' ;
if (s[N - 1] == s[N - 2])
{
s[N - 1]++;
}
}
let ans = "" ;
for (let i = 0; i < s.length; i++)
{
ans += s[i];
}
return ans;
}
let S = "?a?a" ;
document.write(changeString(S));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...