Count of operations to make a binary string”ab” free

Given a string containing characters ‘a’ and ‘b’ only. Convert the given string into a string in which there is no ‘ab’ substring. To make string ‘ab’ free we can perform an operation in which we select a ‘ab’ substring and replace it by ‘bba’.
Find the total number of operations required to convert the given string.

Examples:

Input : s = 'abbaa'
Output : 2
Explanation:
Here, ['ab'baa] is replaced s = [bbabaa]
[bb'ab'aa] is replaced s = [bbbbaaa]
which is ab free. Hence, 2 operations required.

Input : s = 'aab'
Output : 3
Explanation:
Here, [a'ab'] is replaced s = [abba]
['ab'ba] is replaced s = [bbaba]
[bb'ab'a] is replaced s = [bbbbaa]
which is ab free. Hence, 3 operations required.

Approach:
The final state will be some character ‘a’ after ‘b’: “bbb…baaa…a”
It’s obvious to prove all ‘b’s are distinctive to each other(i.e. Each ‘b’ in the initial state, will add some number of ‘b’s to the final state disjoint from other ‘b’s). For a character ‘b’ from the initial state it will double after seeing a character ‘a’. For each i-th character ‘b’, consider ti the number of a before it. So the final number of ‘b’s can be defined as sumation of 2^ti.

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find all subsets of given set. Any
// repeated subset is considered only once in the output
#include<bits/stdc++.h>
using namespace std;
  
// code to make 'ab' free string
int abFree(string s) 
{
    int n = s.length();
    char char_array[n + 1];
      
    // convert string into char array
    strcpy(char_array, s.c_str());
      
    // Traverse from end. Keep track of count
    // b's. For every 'a' encountered, add b_count 
    // to result and double b_count.
    int b_count = 0;
    int res = 0;
    for (int i = 0; i < n; i++) 
    {
        if (char_array[n - i - 1] == 'a'
        {
            res = (res + b_count);
            b_count = (b_count * 2);
        } else {
            b_count += 1;
        }
    }
    return res;
}
  
// Driver code
int main()
{
    string s = "abbaa";
    cout<<abFree(s)<<endl;
      
    s = "aab";
    cout<<abFree(s)<<endl;
      
    s = "ababab";
    cout<<abFree(s)<<endl;
      
    return 0;
}
  
// This code is contributed by Rajput-Ji

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find all subsets of given set. Any
// repeated subset is considered only once in the output
import java.util.*;
  
class GFG 
{
  
    // code to make 'ab' free string
    static int abFree(char[] s) 
    {
  
        // Traverse from end. Keep track of count
        // b's. For every 'a' encountered, add b_count 
        // to result and double b_count.
        int b_count = 0;
        int res = 0;
        for (int i = 0; i < s.length; i++) 
        {
            if (s[s.length - i - 1] == 'a'
            {
                res = (res + b_count);
                b_count = (b_count * 2);
            } else {
                b_count += 1;
            }
        }
        return res;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "abbaa";
        System.out.println(abFree(s.toCharArray()));
  
        s = "aab";
        System.out.println(abFree(s.toCharArray()));
  
        s = "ababab";
        System.out.println(abFree(s.toCharArray()));
    }
}
  
// This code is contributed by Princi Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# code to make 'ab' free string
def abFree(s):
     
    # Traverse from end. Keep track of count
    # b's. For every 'a' encountered, add b_count 
    # to result and double b_count.
    b_count = 0
    res = 0
    for i in range(len(s)):
        if s[~i] == 'a':
            res = (res + b_count)
            b_count  = (b_count  * 2)
        else:
            b_count  += 1
    return res
  
# driver code
s = 'abbaa'
print(abFree(s))
  
s = 'aab'
print(abFree(s))
  
s ='ababab'
print(abFree(s))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find all subsets of given set. 
// Any repeated subset is considered 
// only once in the output 
using System;
using System.Collections.Generic;
  
class GFG 
  
    // code to make 'ab' free string 
    static int abFree(char[] s) 
    
  
        // Traverse from end. Keep track of count 
        // b's. For every 'a' encountered, add b_count 
        // to result and double b_count. 
        int b_count = 0; 
        int res = 0; 
        for (int i = 0; i < s.Length; i++) 
        
            if (s[s.Length - i - 1] == 'a'
            
                res = (res + b_count); 
                b_count = (b_count * 2); 
            } else 
            
                b_count += 1; 
            
        
        return res; 
    
  
    // Driver code 
    public static void Main(String[] args) 
    
        String s = "abbaa"
        Console.WriteLine(abFree(s.ToCharArray())); 
  
        s = "aab"
        Console.WriteLine(abFree(s.ToCharArray())); 
  
        s = "ababab"
        Console.WriteLine(abFree(s.ToCharArray())); 
    
  
// This code contributed by Rajput-Ji

chevron_right


Output:

2
3
11


My Personal Notes arrow_drop_up


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 : princi singh, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.