Given a binary string **str**, the task is to find the count of deletion of the sub-string **“01”** or **“10”** from the string so that the given string is free from these sub-strings. Print the minimum number of deletions.

**Examples:**

Input:str = “11010”

Output:2

The resultant string will be “1”

Input:str = “1000101”

Output:3

Resultant string, str = “0”

**Approach: ** We are deleting **“01”** and **“10”** and the binary string contains only characters **‘0’** and **‘1’**. Therefore, minimum number of deletions will be equal to the minimum of the count of ‘0’ and ‘1’.

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 deletions ` `// of sub-strings "01" or "10" ` `int` `substrDeletion(string str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the count of 0s and 1s ` ` ` `int` `count0 = 0, count1 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` `if` `(str[i] == ` `'0'` `) ` ` ` `count0++; ` ` ` `else` ` ` `count1++; ` ` ` `} ` ` ` ` ` `return` `min(count0, count1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"010"` `; ` ` ` `int` `len = str.length(); ` ` ` `cout << substrDeletion(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of deletions ` `// of sub-strings "01" or "10" ` `static` `int` `substrDeletion(String str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the count of 0s and 1s ` ` ` `int` `count0 = ` `0` `, count1 = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` `if` `(str.charAt(i) == ` `'0'` `) ` ` ` `count0++; ` ` ` `else` ` ` `count1++; ` ` ` `} ` ` ` ` ` `return` `Math.min(count0, count1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"010"` `; ` ` ` `int` `len = str.length(); ` ` ` `System.out.println(substrDeletion(str, len)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# deletions of sub-strings "01" or "10" ` `def` `substrDeletion(string, length) : ` ` ` ` ` `# To store the count of 0s and 1s ` ` ` `count0 ` `=` `0` `; ` ` ` `count1 ` `=` `0` `; ` ` ` ` ` `for` `i ` `in` `range` `(length) : ` ` ` `if` `(string[i] ` `=` `=` `'0'` `) : ` ` ` `count0 ` `+` `=` `1` `; ` ` ` `else` `: ` ` ` `count1 ` `+` `=` `1` `; ` ` ` ` ` `return` `min` `(count0, count1); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `string ` `=` `"010"` `; ` ` ` `length ` `=` `len` `(string); ` ` ` ` ` `print` `(substrDeletion(string, length)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of deletions ` `// of sub-strings "01" or "10" ` `static` `int` `substrDeletion(` `string` `str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the count of 0s and 1s ` ` ` `int` `count0 = 0, count1 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` `if` `(str[i] == ` `'0'` `) ` ` ` `count0++; ` ` ` `else` ` ` `count1++; ` ` ` `} ` ` ` ` ` `return` `Math.Min(count0, count1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `string` `str = ` `"010"` `; ` ` ` `int` `len = str.Length; ` ` ` `Console.Write(substrDeletion(str, len)); ` `} ` `} ` ` ` `// This code is contributed by Ita_c. ` |

*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 number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Count of operations to make a binary string"ab" free
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Minimum cost to make a string free of a subsequence
- Minimum number of deletions so that no two consecutive are same
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Number of flips to make binary string alternate | Set 1
- Minimum number of characters to be removed to make a binary string alternate
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Minimum swaps required to make a binary string alternating
- Minimum number of replacements to make the binary string alternating | Set 2
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Minimum number of swaps to make two binary string equal
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum number of flips with rotation to make binary string alternating
- Minimum changes required to make first string substring of second string

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.