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.
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.
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.
2 3 11
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Minimum cost to make a string free of a subsequence
- Minimize count of given operations required to make two given strings permutations of each other
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Minimum move to end operations to make all strings equal
- Minimal operations to make a number magical
- Minimum number of given operations required to make two strings equal
- Operations required to make the string empty
- Minimum operations to make frequency of all characters equal K
- Minimum operations required to make the string satisfy the given condition
- Minimum number of operations required to make two strings equal
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Binary array after M range toggle operations
- Minimum number of operations required to sum to binary string S
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of operations required to obtain a given Binary String
- Minimum number of operations required to maximize the Binary String
- Mth bit in Nth binary string from a sequence generated by the given operations
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.