Given a string consisting of only three possible characters ‘a’, ‘b’ or ‘c’. The task is to replace characters of the given string with ‘a’, ‘b’ or ‘c’ only such that there are equal number of characters of ‘a’, ‘b’ and ‘c’ in the string. The task is to minimize the number of replacements and print the lexicographically smallest string possible of all such strings with the minimal replacements.
If it is not possible to obtain such a string, print -1.
Input : s = "bcabba" Output : bcabca Number of replacements done is 1 and this is the lexicographically smallest possible Input : "aaaaaa" Output : aabbcc Input : "aaaaa" Output : -1
- Count the number of ‘a’, ‘b’ and ‘c’ in the string.
- If the count of them is equal then the same string will be the answer.
- If length of the string is not a multiple of 3, then it is not possible.
- First, reduce the number of exceeding a’s in the string.
- Replace ‘c’ by ‘a’ if there are extra ‘c’ using a sliding window technique from left.
- Replace ‘b’ by ‘a’ if there are extra ‘b’ using a sliding window technique from left in case of no ‘c’ at an index.
- Secondly, reduce the number of exceeding b’s in the string by replacing ‘c’ from front using sliding window.
- Thirdly, reduce the number of exceeding c’s by reducing the number of extra ‘a’ from the back using sliding window.
- Fourthly, reduce the number of exceeding b’s in the string by reducing the number of extra ‘a’ from the back.
- Fifthly, reduce the number of exceeding c’s if any by reducing the number of extra ‘b’ from the back.
We keep on replacing from back in order to get lexicographically smallest string.
Below is the implementation of the above approach:
Time Complexity: O(N*6)
- Minimum number of replacements to make the binary string alternating | Set 2
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimize number of unique characters in string
- Minimize the cost to split a number
- Covert string X to an anagram of string Y with minimum replacements
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Minimize the length of string by removing occurrence of only one character
- String Range Queries to find the number of subsets equal to a given String
- Minimum number of given operations required to convert a string to another string
- Number of sub-strings which are anagram of any sub-string of another string
- Number of substrings of a string
- Number of even substrings in a string of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : AnkitRai01