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 replacements to make elements of a ternary array same
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Convert string X to an anagram of string Y with minimum replacements
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimize characters to be changed to make the left and right rotation of a string same
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements in a string to make adjacent characters unequal
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Maximum and minimum sums from two numbers with digit replacements
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimize length of prefix of string S containing all characters of another string T
- Minimum rotations required to get the same string
- Minimum rotations required to get the same String | Set-2
- Minimize number of unique characters in string
- Check whether two strings contain same characters in same order
- Maximize product of same-indexed elements of same size subsequences
- Minimize the length of string by removing occurrence of only one character
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
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.