Lexicographically smallest String with unique adjacent characters
Last Updated :
17 Apr, 2024
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++
// 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
// 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
# 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#
// 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));
}
}
Javascript
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));
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...