Make a given Binary String non-decreasing by removing the smallest subsequence
Last Updated :
21 Apr, 2021
Given a binary string str of size N, the task is to find the length of the smallest subsequence such that after erasing the subsequence the resulting string will be the longest continuous non-decreasing string.
Example :
Input: str = “10011”
Output: 1
Explanation: Removal of the first occurrence of ‘1’ results in a non-decreasing subsequence, i.e “0011”.
Input: str = “11110000”
Output: 4
Approach: The problem can be solved based on the following observations:
The non-decreasing subsequences can be of the following 3 types:
- Case 1 : 00000…..
- Case 2 : 11111…..
- Case 3 : 0000….111111….
Follow the given steps to solve the problem:
- Iterate over the characters of the string.
- Count the number of 0s and 1s present in the string
- To generate non-decreasing subsequences of the form “0000….”, minimum removals required is the count of 1s in the string
- To generate non-decreasing subsequences of the form “1111….”, minimum removals required is the count of 0s in the string
- To generate non-decreasing subsequences of the form “0000…1111….”, minimum removals required can be obtained using the following steps:
- Finally, print the minimum removals obtained in the above three cases as the required answer.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int min_length(string str)
{
int n = str.length();
int total_zeros = 0;
int total_ones = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' )
total_zeros++;
else
total_ones++;
}
int ans = min(total_zeros, total_ones);
int cur_zeros = 0, cur_ones = 0;
for ( char x : str) {
if (x == '0' )
cur_zeros++;
else
cur_ones++;
ans = min(ans, cur_ones
+ (total_zeros - cur_zeros));
}
cout << ans;
}
int main()
{
string str = "10011" ;
min_length(str);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void min_length(String str)
{
int n = str.length();
int total_zeros = 0 ;
int total_ones = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '0' ){
total_zeros++;
}
else {
total_ones++;
}
}
int ans = Math.min(total_zeros, total_ones);
int cur_zeros = 0 , cur_ones = 0 ;
for ( int i = 0 ; i<str.length(); i++)
{
char x = str.charAt(i);
if (x == '0' ){
cur_zeros++;
}
else {
cur_ones++;
}
ans = Math.min(ans, cur_ones
+ (total_zeros - cur_zeros));
}
System.out.println(ans);
}
public static void main (String[] args)
{
String str = "10011" ;
min_length(str);
}
}
|
Python3
def min_length( str ):
n = len ( str )
total_zeros = 0
total_ones = 0
for i in range (n):
if ( str [i] = = '0' ):
total_zeros + = 1
else :
total_ones + = 1
ans = min (total_zeros, total_ones)
cur_zeros = 0
cur_ones = 0
for x in str :
if (x = = '0' ):
cur_zeros + = 1
else :
cur_ones + = 1
ans = min (ans, cur_ones + (total_zeros - cur_zeros))
print (ans)
if __name__ = = '__main__' :
str = "10011"
min_length( str )
|
C#
using System;
class GFG{
public static void min_length( string str)
{
int n = str.Length;
int total_zeros = 0;
int total_ones = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
{
total_zeros++;
}
else
{
total_ones++;
}
}
int ans = Math.Min(total_zeros, total_ones);
int cur_zeros = 0, cur_ones = 0;
for ( int i = 0; i < str.Length; i++)
{
char x = str[i];
if (x == '0' )
{
cur_zeros++;
}
else
{
cur_ones++;
}
ans = Math.Min(ans, cur_ones +
(total_zeros - cur_zeros));
}
Console.WriteLine(ans);
}
static public void Main()
{
string str = "10011" ;
min_length(str);
}
}
|
Javascript
<script>
function min_length(str)
{
var n = str.length;
var total_zeros = 0;
var total_ones = 0;
for ( var i = 0; i < n; i++) {
if (str[i] == '0' )
total_zeros++;
else
total_ones++;
}
var ans = Math.min(total_zeros, total_ones);
var cur_zeros = 0, cur_ones = 0;
for ( var i =0; i< str.length; i++){
if (str[i] == '0' )
cur_zeros++;
else
cur_ones++;
ans = Math.min(ans, cur_ones
+ (total_zeros - cur_zeros));
}
document.write( ans);
}
var str = "10011" ;
min_length(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...