- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count of operations to make a binary string “ab” free in C++

We are given a string which can contain “ab” and the task is to calculate the count of operations required to remove or delete “ab” from the string. So, our task is to firstly check whether the string contains “ab” or not if yes then we have to make string “ab” free.

**Input** − string str = "ababaa"

**Output** − Count of operations to make a binary string “ab” free are − 4

**Explanation** − As we can see in the string “ab” pattern is occurring two times so we will replace “ab” with “bba” so the count of operations is 1 and now the string is bbaabaa. Again, we will replace “ab” with “bba” so the count of operations is 2 and now the string is bbabbaaa. We are having one more “ab” so the count of operations is 3 and now the string is bbabbaaa. As we continue replacing the examined “ab” with “bba” the string will become “ab” free.

**Input** − str = "abaa"

**Output** − Count of operations to make a binary string “ab” free are − 1

**Explanation** − As we can see in the string “ab” pattern is occurring one time so we will replace “ab” with “bba” so the count of operations is 1 and now the string is bbaaa. Now, the string is “ab” free and the count is 1.

## Approach used in the below program is as follows

Input a string and calculate the length of a string and pass the data to the function for further processing.

Declare a temporary variable count to store the operations required to make string “ab” free.

Create a character array of size string length + 1

Store the character of the string in an array using strcpy() method.

Start Loop FOR from 0 till the length of a string

Inside the loop, check IF arr[length - i - 1] = ‘a’ then set count as count + 0 and set total variable as total * 2

Else, increment the count for total by 1.

Return the count

Print the result.

## Example

#include<bits/stdc++.h> using namespace std; int operations_ab_free(string str, int len){ int count = 0; char arr[length + 1]; strcpy(arr, str.c_str()); int total = 0; for (int i = 0; i < len; i++){ if (arr[len - i - 1] == 'a'){ count = (count + total); total = (total * 2); } else{ total++; } } return count; } int main(){ string str = "ababaa"; int length = str.length(); cout<<"Count of operations to make a binary string “ab” free are: "<<operations_ab_free(str, length)<<endl; return 0; }

## Output

If we run the above code it will generate the following output −

Count of operations to make a binary string “ab” free are: 4

- Related Questions & Answers
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ Program to count operations to make filename valid
- C++ code to count operations to make array sorted
- C++ code to count number of operations to make two arrays same
- C++ Program to count operations to make all gifts counts same
- Program to find minimum number of operations to make string sorted in Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to count number of operations to convert binary matrix to zero matrix in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?
- C++ program to count minimum number of operations needed to make number n to 1
- Minimum swaps required to make a binary string alternating in C++
- Count of substrings of a binary string containing K ones in C++
- Minimum number of operations required to sum to binary string S using C++.
- How to Make Free calls without Internet?
- Number of flips to make binary string alternate - Set 1 in C++