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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to Minimize the number of
// replacements to get a string with same
// number of ‘a’, ‘b’ and ‘c’ in it
  
#include <bits/stdc++.h>
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;
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to Minimize the number of 
// replacements to get a string with same 
// number of ‘a’, ‘b’ and ‘c’ in it 
  
// Function to count numbers 
function lexoSmallest($s, $n
    // Count the number of 'a', 'b' 
    // and 'c' in string 
    $ca = 0;
    $cb = 0;
    $cc = 0; 
  
    for ($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
    
  
    $cnt = floor($n / 3); 
  
    // If not a multiple of 3 
    if ($cnt * 3 != $n
    
        return "-1"
    
  
    $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 is
        // 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 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.
?>

chevron_right


Output:

aabbcc

Time Complexity: O(N*6)



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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