Given a binary string **S** of length **N**, the task is to find the minimum number of removal of adjacent similar characters required to empty the given binary string.

**Examples:**

Input:S = “1100011“Output:2Explanation:

Operation 1: Removal of all 0s modifies S to “1111“.

Operation 2: Removal of all remaining 1s makes S empty.

Therefore, the minimum number of operations required is 2.

Input:S = “0010100“Output:3Explanation:

Operation 1: Removal of all 1s modifes S to “000100“.

Operation 2: Removal of all 1s modifies S = “00000“.

Operation 3: Removal of all remaining 0s makes S empty.

Therefore, the minimum number of operations required is 3.

**Approach:** The given problem can be solved using Greedy Approach. The idea is to delete the consecutive occurrences of the character with higher frequency. Follow the steps below to solve the problem:

- Traverse the given string
**S**and generate a new string, say**newString**, by removing consecutive occurrences of the character with higher frequency. - Finally, print
**(sizeof(newString) + 1)/2**as the required answer

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find minimum steps` `// to make the string empty` `int` `minSteps(string S)` `{` ` ` `// Stores the modified string` ` ` `string new_str;` ` ` `// Size of string` ` ` `int` `N = S.length();` ` ` `int` `i = 0;` ` ` `while` `(i < N) {` ` ` `new_str += S[i];` ` ` `// Remving substring of same` ` ` `// character from modified string` ` ` `int` `j = i;` ` ` `while` `(i < N && S[i] == S[j])` ` ` `++i;` ` ` `}` ` ` `// Print the minimum steps required` ` ` `cout << ` `ceil` `((new_str.size() + 1) / 2.0);` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given string S` ` ` `string S = ` `"0010100"` `;` ` ` `// Function Call` ` ` `minSteps(S);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find minimum steps` `// to make the String empty` `static` `void` `minSteps(String S)` `{` ` ` ` ` `// Stores the modified String` ` ` `String new_str = ` `""` `;` ` ` `// Size of String` ` ` `int` `N = S.length();` ` ` `int` `i = ` `0` `;` ` ` `while` `(i < N) ` ` ` `{` ` ` `new_str += S.charAt(i);` ` ` ` ` `// Remving subString of same` ` ` `// character from modified String` ` ` `int` `j = i;` ` ` `while` `(i < N && S.charAt(i) == S.charAt(j))` ` ` `++i;` ` ` `}` ` ` `// Print the minimum steps required` ` ` `System.out.print((` `int` `)Math.ceil(` ` ` `(new_str.length() + ` `1` `) / ` `2.0` `));` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given String S` ` ` `String S = ` `"0010100"` `;` ` ` `// Function Call` ` ` `minSteps(S);` `}` `}` `// This code is contributed by Princi Singh` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `from` `math ` `import` `ceil` `# Function to find minimum steps` `# to make the empty` `def` `minSteps(S):` ` ` ` ` `# Stores the modified string` ` ` `new_str ` `=` `""` ` ` `# Size of string` ` ` `N ` `=` `len` `(S)` ` ` `i ` `=` `0` ` ` `while` `(i < N):` ` ` `new_str ` `+` `=` `S[i]` ` ` `# Removing substring of same character` ` ` `# from modified string` ` ` `j ` `=` `i` ` ` `while` `(i < N ` `and` `S[i] ` `=` `=` `S[j]):` ` ` `i ` `+` `=` `1` ` ` `# Print the minimum steps required` ` ` `print` `(ceil((` `len` `(new_str) ` `+` `1` `) ` `/` `2` `))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given S` ` ` `S ` `=` `"0010100"` ` ` `# Function Call` ` ` `minSteps(S)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find minimum steps` `// to make the string empty` `static` `void` `minSteps(` `string` `S)` `{` ` ` ` ` `// Stores the modified string` ` ` `string` `new_str = ` `""` `;` ` ` ` ` `// Size of string` ` ` `int` `N = S.Length;` ` ` `int` `i = 0;` ` ` `while` `(i < N) ` ` ` `{` ` ` `new_str += S[i];` ` ` ` ` `// Remving substring of same` ` ` `// character from modified string` ` ` `int` `j = i;` ` ` ` ` `while` `(i < N && S[i] == S[j])` ` ` `++i;` ` ` `}` ` ` `// Print the minimum steps required` ` ` `Console.Write((` `int` `)Math.Ceiling(` ` ` `(new_str.Length + 1) / 2.0));` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Given string S` ` ` `string` `S = ` `"0010100"` `;` ` ` `// Function Call` ` ` `minSteps(S);` `}` `}` `// This code is contributed by SURENDRA_GANGWAR` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Minimum removal of subsequences of distinct consecutive characters required to empty a given string
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Minimum removal of characters required such that permutation of given string is a palindrome
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Sum of all possible strings obtained by removal of non-empty substrings
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum characters required to be removed to sort binary string in ascending order
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Maximum length of balanced string after swapping and removal of characters
- Lexicographically largest possible String after removal of K characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Minimum swaps to group similar characters side by side?
- Minimum adjacent swaps to group similar characters together
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Operations required to make the string empty

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.