Given a binary string **str**, the task is to find the minimum number of operations required to make all the characters of the string same i.e. either the resultant string contains all 0s or all 1s. In a single operation, any block of consecutive 0s can be converted to a block of consecutive 1s of the same length and vice versa.

**Examples:**

Input:str = “000111”

Output:1

In a single operation, either change all 0s to 1s

or change all 1s to 0s.

Input:str = “0011101010”

Output:3

All the 1s can be converted to 0s in 3 operations.

**Approach:** The problem is to convert all the characters into a single one. Now since converting a whole consecutive group of characters counts as a single step. You can calculate the number of different groups as separated by each other due to the presence of other characters in between them. Now the number of steps would be simply the minimum of both the numbers. Hence, the answer will be the minimum of the count of consecutive blocks of 0s or the count of consecutive blocks of 1s.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// minimum operations required ` `int` `minOperations(string str, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Increment count when consecutive ` ` ` `// characters are different ` ` ` `if` `(str[i] != str[i + 1]) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Answer is rounding off the ` ` ` `// (count / 2) to lower ` ` ` `return` `(count + 1) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"000111"` `; ` ` ` `int` `n = str.length(); ` ` ` ` ` `cout << minOperations(str, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// minimum operations required ` `static` `int` `minOperations(String str, ` `int` `n) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) ` ` ` `{ ` ` ` ` ` `// Increment count when consecutive ` ` ` `// characters are different ` ` ` `if` `(str.charAt(i) != str.charAt(i + ` `1` `)) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Answer is rounding off the ` ` ` `// (count / 2) to lower ` ` ` `return` `(count + ` `1` `) / ` `2` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"000111"` `; ` ` ` `int` `n = str.length(); ` ` ` ` ` `System.out.println(minOperations(str, n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# minimum operations required ` `def` `minOperations(` `str` `, n): ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `): ` ` ` ` ` `# Increment count when consecutive ` ` ` `# characters are different ` ` ` `if` `(` `str` `[i] !` `=` `str` `[i ` `+` `1` `]): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Answer is rounding off the ` ` ` `# (count / 2) to lower ` ` ` `return` `(count ` `+` `1` `) ` `/` `/` `2` ` ` `# Driver code ` `str` `=` `"000111"` `n ` `=` `len` `(` `str` `) ` ` ` `print` `(minOperations(` `str` `, n)) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// minimum operations required ` `static` `int` `minOperations(` `string` `str, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) ` ` ` `{ ` ` ` ` ` `// Increment count when consecutive ` ` ` `// characters are different ` ` ` `if` `(str[(i)] != str[(i + 1)]) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Answer is rounding off the ` ` ` `// (count / 2) to lower ` ` ` `return` `(count + 1) / 2; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `string` `str = ` `"000111"` `; ` ` ` `int` `n = str.Length; ` ` ` ` ` `Console.WriteLine(minOperations(str, n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

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 given operations required to convert a given binary string to all 1's
- Minimum number of given operations required to convert a string to another string
- Minimum operations required to convert all characters of a String to a given Character
- Minimum number of operations required to sum to binary string S
- Minimum number of operations required to obtain a given Binary String
- Minimum number of operations required to maximize the Binary String
- Minimum swaps required to convert one binary string to another
- Minimum substring flips required to convert given binary string to another
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum operations required to make the string satisfy the given condition
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to keep all 1s together in a Binary string
- Minimum reduce operations to convert a given string into a palindrome
- Minimum number of given operations required to make two strings equal

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.