Deletions of “01” or “10” in binary string to make it free from “01” or “10”
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 codeint main(){ string str = "010"; int len = str.length(); cout << substrDeletion(str, len); return 0;} |
Java
// Java implementation of the approachclass 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 codepublic 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. |
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 codeif __name__ == "__main__" : string = "010"; length = len(string); print(substrDeletion(string, length));# This code is contributed by Ryuga |
C#
// C# implementation of the approachusing 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 codepublic static void Main(){ string str = "010"; int len = str.Length; Console.Write(substrDeletion(str, len));}}// This code is contributed by Ita_c. |
Javascript
<script>// Javascript implementation of the approach// Function to return the count of deletions// of sub-strings "01" or "10"function substrDeletion(str,len){ // To store the count of 0s and 1s let count0 = 0, count1 = 0; for (let i = 0; i < len; i++) { if (str[i] == '0') count0++; else count1++; } return Math.min(count0, count1);}// Driver codelet str = "010";let len = str.length;document.write(substrDeletion(str, len)); // This code is contributed by rag2127</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


