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.
Input: S = “?a?a”
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 = “???”
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:
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- String with k distinct characters and no same characters adjacent
- Rearrange string such that no pair of adjacent characters are of the same type
- Minimum string such that every adjacent character of given string is still adjacent
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Check whether two strings contain same characters in same order
- Replace minimal number of characters to make all characters pair wise distinct
- Min flips of continuous characters to make all characters same in a string
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Replace even-indexed characters of minimum number of substrings to convert a string to another
- Minimum characters to be replaced to make frequency of all characters same
- Permutations of string such that no two vowels are adjacent
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.