Minimum removals required to place all 0s before 1s in a Binary String
Last Updated :
22 Jul, 2022
Given a binary string S, the task is to find the minimum number of characters required to be removed from S, such that all the 0s are placed before 1s.
Examples:
Input: S = “001101”
Output: 1
Explanation:
Removing S[4] (= ‘0’) modifies the string S to “00111”.
Therefore, minimum number of deletions required is 1.
Input: S = 01001
Output: 1
Explanation:
Removing S[1] (= ‘1’) modifies the string S to “0001”.
Therefore, minimum number of deletions required is 1.
Approach: The problem can be solved by finding the minimum number of ‘0’s characters required to be deleted from the right, say right_0, and the minimum number of ‘1’s required to be deleted from the left, say left_1, to obtain the required string. The minimum sum of right_0 and left_0 obtained for any index gives the final result.
Follow the steps below to solve the given problem:
- Initialize two counter variables, say right_0 and left_1.
- Store the count of ‘0’s in the string S in right_0 and set left_1 equal to 0.
- Initialize a variable, say res, to store the required answer.
- Iterate over the characters of the string S and for each character:
- Check if s[i] is equal to ‘0’ or not.
- If found to be true, then decrease right_0 by 1.
- Otherwise, increase left_1 by 1.
- Check if the sum right_0, left_1 is less than res or not. If found to be true, then update res equal to the minimum of res and right_0 + left_1.
- After complete traversal of the array, print res as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumDeletions(string s)
{
int right_0 = count(s.begin(), s.end(), '0' );
int left_1 = 0;
int n = s.size();
int res = INT_MAX;
for ( int i = 0; i < n; i++)
{
if (s[i] == '0' )
{
right_0 -= 1;
}
else
{
left_1 += 1;
}
res = min(res, right_0 + left_1);
}
return res;
}
int main()
{
string s = "001101" ;
int count = minimumDeletions(s);
cout << count;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minimumDeletions(String s)
{
int right_0 = ( int )(s.chars().filter(ch -> ch == '0' ).count());
int left_1 = 0 ;
int n = s.length();
int res = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == '0' )
{
right_0 -= 1 ;
}
else
{
left_1 += 1 ;
}
res = Math.min(res, right_0 + left_1);
}
return res;
}
public static void main(String[] args)
{
String s = "001101" ;
int count = minimumDeletions(s);
System.out.print(count);
}
}
|
Python3
import sys
def minimumDeletions(s) :
right_0 = s.count( '0' )
left_1 = 0
n = len (s)
res = sys.maxsize
for i in range (n):
if (s[i] = = '0' ) :
right_0 - = 1
else :
left_1 + = 1
res = min (res, right_0 + left_1)
return res
s = "001101"
count = minimumDeletions(s)
print ( count)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minimumDeletions( string s)
{
int right_0 = ( int )s.Split( '0' ).Length - 1;
int left_1 = 0;
int n = s.Length;
int res = Int32.MaxValue;
for ( int i = 0; i < n; i++)
{
if (s[i] == '0' )
{
right_0 -= 1;
}
else
{
left_1 += 1;
}
res = Math.Min(res, right_0 + left_1);
}
return res;
}
public static void Main(String[] args)
{
string s = "001101" ;
int count = minimumDeletions(s);
Console.WriteLine(count);
}
}
|
Javascript
<script>
function minimumDeletions(s)
{
var right_0 = 0;
var i;
for (i=0;i<s.length;i++){
if (s[i]== '0' )
right_0++;
}
var left_1 = 0;
var n = s.length;
var res = 2147483647;
for (i = 0; i < n; i++)
{
if (s[i] == '0' )
{
right_0 -= 1;
}
else
{
left_1 += 1;
}
res = Math.min(res, right_0 + left_1);
}
return res;
}
var s = "001101" ;
var count = minimumDeletions(s);
document.write(count);
</script>
|
Time Complexity: O(|S|)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...