Lexicographically smallest String with unique adjacent characters
Given a string consisting of βaβ, βbβ, βcβ, or β?β, you are required to replace the question marks β?β in the string. The goal is to find a replacement that adheres to the following conditions:
- The string can only contain βaβ, βbβ, and βcβ.
- No two consecutive characters in the string can be the same.
- The first and last characters of the string cannot be identical.
- If there are multiple valid solutions, choose the lexicographically smallest string.
Examples:
Input: S = βaba?ca?β
Output: βababcabβ
Explanation: first β?β was replaced by βbβ as βaβ and βcβ are adjacent to that , and last β?β can be replaced by βbβ and βcβ but βbβ comes first lexicographically.Input: S = βba?β
Output: βbacβ
Explanation: β?β was replaced by βcβ as βaβ is adjacent and βbβ is the starting of the string and β?β is the ending of the string.
Approach: The basic way to solve the problem is as follows:
- The approach is to iterate over the string S and whenever we encounter a β?β we will check the previous and next occurring char and replace the β?β accordingly.
- First, we try to first replace it with βaβ, then βbβ, and then βcβ as follows
- if adjacent charβs are not βaβ then we can replace it with βaβ.
- else if adjacent charβs are not βbβ then we can replace it with βbβ.
- else if adjacent charβs are not βcβ then we can replace it with βcβ.
Below is the implementation of the above idea.
// C++ code for the above approach:
#include <iostream>
#include <string>
using namespace std;
string newStr(string s)
{
// Iterating and removing occurrences of '?'
int n = s.length();
for (int i = 0; i < n; ++i) {
// If the current character
// is a question mark
if (s[i] == '?') {
// Check if previous and next
// characters are not
// 'a'
if (s[(i - 1 + n) % n] != 'a'
&& s[(i + 1) % n] != 'a') {
// Replace the question
// mark with 'a'
s[i] = 'a';
}
// Check if previous and
// next characters are not
// 'b'
else if (s[(i - 1 + n) % n] != 'b'
&& s[(i + 1) % n] != 'b') {
// Replace the question
// mark with 'b'
s[i] = 'b';
}
// Check if previous and
// next characters are not
// 'c'
else if (s[(i - 1 + n) % n] != 'c'
&& s[(i + 1) % n] != 'c') {
// Replace the question
// mark with 'c'
s[i] = 'c';
}
}
}
return s;
}
// Drivers code
int main()
{
string S = "aba?ca?";
// Call the newStr function
// and print the result
cout << newStr(S) << endl;
return 0;
}
// Java code for the above approach:
import java.util.*;
class Main {
public static String newStr(String s) {
// Iterating and removing occurrences of '?'
int n = s.length();
char[] arr = s.toCharArray();
for (int i = 0; i < n; ++i) {
// If the current character
// is a question mark
if (arr[i] == '?') {
// Check if previous and next
// characters are not
// 'a'
if (arr[(i - 1 + n) % n] != 'a'
&& arr[(i + 1) % n] != 'a') {
// Replace the question
// mark with 'a'
arr[i] = 'a';
}
// Check if previous and
// next characters are not
// 'b'
else if (arr[(i - 1 + n) % n] != 'b'
&& arr[(i + 1) % n] != 'b') {
// Replace the question
// mark with 'b'
arr[i] = 'b';
}
// Check if previous and
// next characters are not
// 'c'
else if (arr[(i - 1 + n) % n] != 'c'
&& arr[(i + 1) % n] != 'c') {
// Replace the question
// mark with 'c'
arr[i] = 'c';
}
}
}
return new String(arr);
}
// Drivers code
public static void main(String[] args) {
String S = "aba?ca?";
// Call the newStr function
// and print the result
System.out.println(newStr(S));
}
}
// This code is contributed by Sakshi
# Python3 implementation of above approach
def newStr(s):
# Iterating and removing occurances of '?'
for i in range(len(s)-1):
# If the current character is a question mark
if s[i] == '?':
# Check if previous and next characters are not 'a'
if s[i-1] != 'a' and s[i+1] != 'a':
# Replace the question mark with 'a'
s = s[:i] + 'a' + s[i+1:]
# Check if previous and next characters are not 'b'
elif s[i-1] != 'b' and s[i+1] != 'b':
# Replace the question mark with 'b'
s = s[:i] + 'b' + s[i+1:]
# Check if previous and next characters are not 'c'
elif s[i-1] != 'c' and s[i+1] != 'c':
# Replace the question mark with 'c'
s = s[:i] + 'c' + s[i+1:]
# If the last character is a question mark
if s[-1] == '?':
# Check if previous and first characters are not 'a'
if s[-2] != 'a' and s[0] != 'a':
# Replace the question mark with 'a'
s = s[:-1] + 'a'
# Check if previous and first characters are not 'b'
elif s[-2] != 'b' and s[0] != 'b':
# Replace the question mark with 'b'
s = s[:-1] + 'b'
# Check if previous and first characters are not 'c'
elif s[-2] != 'c' and s[0] != 'c':
# Replace the question mark with 'c'
s = s[:-1] + 'c'
return s
# Driver Code
S = "aba?ca?"
# Call the newStr function and print the result
print(newStr(S))
# This code is contributed by the Author
// C# code for the approach
using System;
class MainClass
{
public static string NewStr(string s)
{
// Iterating and removing occurrences of '?'
int n = s.Length;
char[] arr = s.ToCharArray();
for (int i = 0; i < n; ++i)
{
// If the current character is a question mark
if (arr[i] == '?')
{
// Check if previous and next characters are not 'a'
if (arr[(i - 1 + n) % n] != 'a' && arr[(i + 1) % n] != 'a')
{
// Replace the question mark with 'a'
arr[i] = 'a';
}
// Check if previous and next characters are not 'b'
else if (arr[(i - 1 + n) % n] != 'b' && arr[(i + 1) % n] != 'b')
{
// Replace the question mark with 'b'
arr[i] = 'b';
}
// Check if previous and next characters are not 'c'
else if (arr[(i - 1 + n) % n] != 'c' && arr[(i + 1) % n] != 'c')
{
// Replace the question mark with 'c'
arr[i] = 'c';
}
}
}
return new string(arr);
}
public static void Main(string[] args)
{
string S = "aba?ca?";
// Call the NewStr function and print the result
Console.WriteLine(NewStr(S));
}
}
function newStr(s) {
const n = s.length;
const chars = s.split(''); // Convert the string to an array of characters for easier manipulation.
for (let i = 0; i < n; ++i) {
if (chars[i] === '?') {
// Check if previous and next characters are not 'a'
if (chars[(i - 1 + n) % n] !== 'a' && chars[(i + 1) % n] !== 'a') {
// Replace the question mark with 'a'
chars[i] = 'a';
}
// Check if previous and next characters are not 'b'
else if (chars[(i - 1 + n) % n] !== 'b' && chars[(i + 1) % n] !== 'b') {
// Replace the question mark with 'b'
chars[i] = 'b';
}
// Check if previous and next characters are not 'c'
else if (chars[(i - 1 + n) % n] !== 'c' && chars[(i + 1) % n] !== 'c') {
// Replace the question mark with 'c'
chars[i] = 'c';
}
}
}
// Convert the array of characters back to a string.
return chars.join('');
}
// Driver code
const S = "aba?ca?";
// Call the newStr function and print the result
console.log(newStr(S));
Output
ababcab
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).
Contact Us