Deletions of “01” or “10” in binary string to make it free from “01” or “10”
Last Updated :
27 Feb, 2023
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, the minimum number of deletions will be equal to the minimum of the count of ‘0’ and ‘1’.
Algorithm:
Step 1: Start
Step 2: create a function that returns a value that takes one string and one int value as input.
Step 3: In the function initialize two integer values say “count0” and “count1” both to 0;
Step 4: Now traverse through the characters of the string
Step 5: If the current character is ‘0’, increment “count0”.
If the current character is ‘1’, increment “count1”.
Step 6: Return the minimum value among both count0 and count1.
Step 7: End
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int substrDeletion(string str, int len)
{
int count0 = 0, count1 = 0;
for ( int i = 0; i < len; i++) {
if (str[i] == '0' )
count0++;
else
count1++;
}
return min(count0, count1);
}
int main()
{
string str = "010" ;
int len = str.length();
cout << substrDeletion(str, len);
return 0;
}
|
Java
class GFG
{
static int substrDeletion(String str, int len)
{
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);
}
public static void main(String[] args)
{
String str = "010" ;
int len = str.length();
System.out.println(substrDeletion(str, len));
}
}
|
Python3
def substrDeletion(string, length) :
count0 = 0 ;
count1 = 0 ;
for i in range (length) :
if (string[i] = = '0' ) :
count0 + = 1 ;
else :
count1 + = 1 ;
return min (count0, count1);
if __name__ = = "__main__" :
string = "010" ;
length = len (string);
print (substrDeletion(string, length));
|
C#
using System;
class GFG
{
static int substrDeletion( string str, int len)
{
int count0 = 0, count1 = 0;
for ( int i = 0; i < len; i++)
{
if (str[i] == '0' )
count0++;
else
count1++;
}
return Math.Min(count0, count1);
}
public static void Main()
{
string str = "010" ;
int len = str.Length;
Console.Write(substrDeletion(str, len));
}
}
|
Javascript
<script>
function substrDeletion(str,len)
{
let count0 = 0, count1 = 0;
for (let i = 0; i < len; i++)
{
if (str[i] == '0' )
count0++;
else
count1++;
}
return Math.min(count0, count1);
}
let str = "010" ;
let len = str.length;
document.write(substrDeletion(str, len));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...