# Minimize the number of replacements to get a string with same number of ‘a’, ‘b’ and ‘c’ in it

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.

Examples:

```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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• 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.
1. Replace ‘c’ by ‘a’ if there are extra ‘c’ using a sliding window technique from left.
2. 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:

## C++

 `// CPP program to Minimize the number of ` `// replacements to get a string with same ` `// number of ‘a’, ‘b’ and ‘c’ in it ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count numbers ` `string lexoSmallest(string s, ``int` `n) ` `{ ` `    ``// Count the number of 'a', 'b' and ` `    ``// 'c' in string ` `    ``int` `ca = 0, cb = 0, cc = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(s[i] == ``'a'``) ` `            ``ca++; ` `        ``else` `if` `(s[i] == ``'b'``) ` `            ``cb++; ` `        ``else` `            ``cc++; ` `    ``} ` ` `  `    ``// If equal previously ` `    ``if` `(ca == cb && cb == cc) { ` `        ``return` `s; ` `    ``} ` ` `  `    ``int` `cnt = n / 3; ` ` `  `    ``// If not a multiple of 3 ` `    ``if` `(cnt * 3 != n) { ` `        ``return` `"-1"``; ` `    ``} ` ` `  `    ``int` `i = 0; ` ` `  `    ``// Increase the number of a's by ` `    ``// removing extra 'b' and ;c; ` `    ``while` `(ca < cnt && i < n) { ` ` `  `        ``// Check if it is 'b' and it more ` `        ``// than n/3 ` `        ``if` `(s[i] == ``'b'` `&& cb > cnt) { ` `            ``cb--; ` `            ``s[i] = ``'a'``; ` `            ``ca++; ` `        ``} ` ` `  `        ``// Check if it is 'c' and it ` `        ``// more than n/3 ` `        ``else` `if` `(s[i] == ``'c'` `&& cc > cnt) { ` `            ``cc--; ` `            ``s[i] = ``'a'``; ` `            ``ca++; ` `        ``} ` ` `  `        ``i++; ` `    ``} ` ` `  `    ``i = 0; ` ` `  `    ``// Increase the number of b's by ` `    ``// removing extra 'c' ` `    ``while` `(cb < cnt && i < n) { ` ` `  `        ``// Check if it is 'c' and it more ` `        ``// than n/3 ` `        ``if` `(s[i] == ``'c'` `&& cc > cnt) { ` `            ``cc--; ` `            ``s[i] = ``'1'``; ` ` `  `            ``cb++; ` `        ``} ` `        ``i++; ` `    ``} ` ` `  `    ``i = n - 1; ` ` `  `    ``// Increase the number of c's from back ` `    ``while` `(cc < cnt && i >= 0) { ` ` `  `        ``// Check if it is 'a' and it more ` `        ``// than n/3 ` `        ``if` `(s[i] == ``'a'` `&& ca > cnt) { ` `            ``ca--; ` `            ``s[i] = ``'c'``; ` `            ``cc++; ` `        ``} ` ` `  `        ``i--; ` `    ``} ` ` `  `    ``i = n - 1; ` ` `  `    ``// Increase the number of b's from back ` `    ``while` `(cb < cnt && i >= 0) { ` ` `  `        ``// Check if it is 'a' and it more ` `        ``// than n/3 ` `        ``if` `(s[i] == ``'a'` `&& ca > cnt) { ` `            ``ca--; ` `            ``s[i] = ``'b'``; ` `            ``cb++; ` `        ``} ` ` `  `        ``i--; ` `    ``} ` ` `  `    ``i = n - 1; ` ` `  `    ``// Increase the number of c's from back ` `    ``while` `(cc < cnt && i >= 0) { ` ` `  `        ``// Check if it is 'b' and it more ` `        ``// than n/3 ` `        ``if` `(s[i] == ``'b'` `&& cb > cnt) { ` `            ``cb--; ` `            ``s[i] = ``'c'``; ` `            ``cc++; ` `        ``} ` ` `  `        ``i--; ` `    ``} ` ` `  `    ``return` `s; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string s = ``"aaaaaa"``; ` `    ``int` `n = s.size(); ` ` `  `    ``cout << lexoSmallest(s, n); ` ` `  `    ``return` `0; ` `} `

## PHP

 ` ``\$cnt``)  ` `        ``{  ` `            ``\$cb``--;  ` `            ``\$s``[``\$i``] = ``'a'``;  ` `            ``\$ca``++;  ` `        ``}  ` ` `  `        ``// Check if it is 'c' and it  ` `        ``// more than n/3  ` `        ``else` `if` `(``\$s``[``\$i``] == ``'c'` `&& ``\$cc` `> ``\$cnt``)  ` `        ``{  ` `            ``\$cc``--;  ` `            ``\$s``[``\$i``] = ``'a'``;  ` `            ``\$ca``++;  ` `        ``}  ` ` `  `        ``\$i``++;  ` `    ``}  ` ` `  `    ``\$i` `= 0;  ` ` `  `    ``// Increase the number of b's by  ` `    ``// removing extra 'c'  ` `    ``while` `(``\$cb` `< ``\$cnt` `&& ``\$i` `< ``\$n``)  ` `    ``{  ` ` `  `        ``// Check if it is 'c' and it more  ` `        ``// than n/3  ` `        ``if` `(``\$s``[``\$i``] == ``'c'` `&& ``\$cc` `> ``\$cnt``)  ` `        ``{  ` `            ``\$cc``--;  ` `            ``\$s``[``\$i``] = ``'1'``;  ` ` `  `            ``\$cb``++;  ` `        ``}  ` `        ``\$i``++;  ` `    ``}  ` ` `  `    ``\$i` `= ``\$n` `- 1;  ` ` `  `    ``// Increase the number of c's from back  ` `    ``while` `(``\$cc` `< ``\$cnt` `&& ``\$i` `>= 0)  ` `    ``{  ` ` `  `        ``// Check if it is 'a' and it is  ` `        ``// more than n/3  ` `        ``if` `(``\$s``[``\$i``] == ``'a'` `&& ``\$ca` `> ``\$cnt``)  ` `        ``{  ` `            ``\$ca``--;  ` `            ``\$s``[``\$i``] = ``'c'``;  ` `            ``\$cc``++;  ` `        ``}  ` ` `  `        ``\$i``--;  ` `    ``}  ` ` `  `    ``\$i` `= ``\$n` `- 1;  ` ` `  `    ``// Increase the number of b's from back  ` `    ``while` `(``\$cb` `< ``\$cnt` `&& ``\$i` `>= 0)  ` `    ``{  ` ` `  `        ``// Check if it is 'a' and it is  ` `        ``// more than n/3  ` `        ``if` `(``\$s``[``\$i``] == ``'a'` `&& ``\$ca` `> ``\$cnt``)  ` `        ``{  ` `            ``\$ca``--;  ` `            ``\$s``[``\$i``] = ``'b'``;  ` `            ``\$cb``++;  ` `        ``}  ` ` `  `        ``\$i``--;  ` `    ``}  ` ` `  `    ``\$i` `= ``\$n` `- 1;  ` ` `  `    ``// Increase the number of c's from back  ` `    ``while` `(``\$cc` `< ``\$cnt` `&& ``\$i` `>= 0)  ` `    ``{  ` ` `  `        ``// Check if it is 'b' and it more  ` `        ``// than n/3  ` `        ``if` `(``\$s``[``\$i``] == ``'b'` `&& ``\$cb` `> ``\$cnt``)  ` `        ``{  ` `            ``\$cb``--;  ` `            ``\$s``[``\$i``] = ``'c'``;  ` `            ``\$cc``++;  ` `        ``}  ` ` `  `        ``\$i``--;  ` `    ``}  ` ` `  `    ``return` `\$s``;  ` `}  ` ` `  `// Driver Code  ` `\$s` `= ``"aaaaaa"``;  ` `\$n` `= ``strlen``(``\$s``);  ` ` `  `echo` `lexoSmallest(``\$s``, ``\$n``); ` ` `  `// This code is contributed by Ryuga. ` `?> `

 `# Write Python3 code here ` `""" ` `Minimize the number of replacements to get a string with  ` `same number of ‘a’, ‘b’ and ‘c’ in it ` `"""` `# Python Implementation of the approach ` ` `  `def` `minimize_replacements(``STR``): ` ` `  `    ``# Count of a's, b's and c's in a given string ` `    ``count_a ``=` `STR``.count(``'a'``) ` `    ``count_b ``=` `STR``.count(``'b'``) ` `    ``count_c ``=` `STR``.count(``'c'``) ` ` `  `    ``# if three of them are equal return the same string ` `    ``if` `count_a ``=``=` `count_b ``=``=` `count_c: ` `        ``return` `(``STR``) ` `     `  `    ``# if not a multiple of 3 then return -1 ` `    ``if` `len``(``STR``)``%``3` `!``=` `0``: ` `        ``return` `-``1` ` `  `    ``# Increase the number of a's by  ` `    ``# removing extra 'b' and ;c; ` `    ``i``=``0` `    ``s ``=` `list``(``STR``) ` `    ``while` `((count_a < ``len``(``STR``)``/``/``3``) & (i < ``len``(``STR``))): ` ` `  `        ``# Check if it is 'b' and it more than n/3 ` `        ``if` `((``STR``[i]``=``=``'b'``) & (count_b > ``len``(``STR``)``/``/``3``)): ` `            ``count_b``-``=``1` `            ``s[i]``=``'a'` `            ``count_a``+``=``1` ` `  `        ``# Check if it is 'c' and it more than n/3  ` `        ``elif` `((``STR``[i]``=``=``'c'``) & (count_c > ``len``(``STR``)``/``/``3``)): ` `            ``count_c``-``=``1` `            ``s[i]``=``'a'` `            ``count_a``+``=``1` `        ``i``+``=``1` `     `  `     `  `    ``i``=``0` `    ``STR` `=` `''.join(s) ` `    ``t ``=` `list``(``STR``) ` ` `  `    ``# Increase the number of b's by removing extra 'c'  ` `    ``while` `((count_b < ``len``(``STR``)``/``/``3``) & (i < ``len``(``STR``))): ` `        ``#Check if it is 'c' and it more than n/3  ` `        ``if` `((``STR``[i] ``=``=` `'c'``) & (count_c > ``len``(``STR``)``/``/``3``)): ` `            ``count_c``-``=``1`  `            ``t[i]``=``'b'` `            ``count_b``+``=``1` `        ``i``+``=``1` `     `  `     `  `    ``i ``=` `len``(``STR``) ``-` `1` `    ``STR` `=` `''.join(t) ` `    ``u ``=` `list``(``STR``) ` ` `  `    ``# Increase the number of c's from back  ` `    ``while` `((count_c < ``len``(``STR``)``/``/``3``) & (i >``=` `0``)): ` ` `  `        ``# Check if it is 'a' and it more than n/3  ` `        ``if` `((``STR``[i] ``=``=` `'a'``) & (count_a > ``len``(``STR``)``/``/``3``)): ` `            ``count_a``-``=``1` `            ``u[i] ``=` `'c'``;  ` `            ``count_c``+``=``1`  `        ``i``-``=``1` `         `  `     `  `     `  `    ``i ``=` `len``(``STR``) ``-` `1`  `    ``STR` `=` `''.join(u) ` `    ``v ``=` `list``(``STR``) ` ` `  `    ``# Increase the number of b's from back  ` `    ``while` `((count_b < ``len``(``STR``)``/``/``3``) & (i >``=` `0``)): ` ` `  `        ``# Check if it is 'a' and it more than n/3  ` `        ``if` `((``STR``[i] ``=``=` `'a'``) & (count_a > ``len``(``STR``)``/``/``3``)): ` `            ``count_a``-``=``1`  `            ``v[i] ``=` `'b'`  `            ``count_b``+``=``1` `        ``i``-``=``1` `         `  `         `  `     `  `    ``i ``=` `len``(``STR``) ``-` `1` `    ``STR` `=` `''.join(v) ` `    ``w ``=` `list``(``STR``) ` `    ``# Increase the number of c's from back  ` `    ``while` `((count_c < ``len``(``STR``)``/``/``3``) & (i >``=` `0``)): ` ` `  `        ``# Check if it is 'b' and it more than n/3  ` `        ``if` `((``STR``[i] ``=``=` `'b'``) & (count_b > ``len``(``STR``)``/``/``3``)): ` `            ``count_b``-``=``1` `            ``w[i] ``=` `'c'` `            ``count_c``+``=``1` `        ``i``-``=``1` `         `  `         `  `    ``STR` `=` `''.join(w) ` `    ``return` `STR` `     `  `if` `__name__ ``=``=` `'__main__'``: ` `    ``print` `(minimize_replacements(``'bcabba'``)) ` `    ``print` `(minimize_replacements(``'aaaaaa'``)) ` `    ``print` `(minimize_replacements(``'aaaaa'``)) `

Output:

```aabbcc
```

Time Complexity: O(N*6)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : AnkitRai01, Vikas Chitturi