Given a binary string str, the task is to remove the minimum number of characters from the given binary string such that the characters in the remaining string form a sorted order.
Examples:
Input: str = “1000101”
Output: 2
Explanation:
Removal of the first two occurrences of ‘1’ modifies the string to “00001”, which is a sorted order.
Therefore, the minimum count of characters to be removed is 2.
Input: str = “001111”
Output: 0
Explanation:
The string is already sorted.
Therefore, the minimum count of character to be removed is 0.
Approach: The idea is to count the number of 1s before the last occurrence of 0 and the number of 0s after the first occurrence of 1. The minimum of the two counts is the required number of characters to be removed. Below are the steps:
- Traverse the string str and find the position of the first occurrence of 1 and the last occurrence of 0.
- Print 0 if the str has only one type of character.
- Now, count the number of 1 is present prior to the last occurrence of 0 and store in a variable, say cnt1.
- Now, count the number of 0s present after the first occurrence of 1 in a variable, say cnt0.
- Print the minimum of cnt0 and cnt1 as the minimum count of character required to be removed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDeletion(string str)
{
int n = str.length();
int firstIdx1 = -1;
int lastIdx0 = -1;
for ( int i = 0; i < n; i++)
{
if (str[i] == '1' )
{
firstIdx1 = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--)
{
if (str[i] == '0' )
{
lastIdx0 = i;
break ;
}
}
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
int count1 = 0, count0 = 0;
for ( int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1' )
{
count1++;
}
}
for ( int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1' )
{
count0++;
}
}
return min(count0, count1);
}
int main()
{
string str = "1000101" ;
cout << minDeletion(str);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int minDeletion(String str)
{
int n = str.length();
int firstIdx1 = - 1 ;
int lastIdx0 = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '1' ) {
firstIdx1 = i;
break ;
}
}
for ( int i = n - 1 ; i >= 0 ; i--) {
if (str.charAt(i) == '0' ) {
lastIdx0 = i;
break ;
}
}
if (firstIdx1 == - 1
|| lastIdx0 == - 1 )
return 0 ;
int count1 = 0 , count0 = 0 ;
for ( int i = 0 ; i < lastIdx0; i++) {
if (str.charAt(i) == '1' ) {
count1++;
}
}
for ( int i = firstIdx1 + 1 ; i < n; i++) {
if (str.charAt(i) == '1' ) {
count0++;
}
}
return Math.min(count0, count1);
}
public static void main(String[] args)
{
String str = "1000101" ;
System.out.println(minDeletion(str));
}
}
|
Python3
def minDeletion(s):
n = len (s)
firstIdx1 = - 1
lastIdx0 = - 1
for i in range ( 0 , n):
if ( str [i] = = '1' ):
firstIdx1 = i
break
for i in range (n - 1 , - 1 , - 1 ):
if ( str [i] = = '0' ):
lastIdx0 = i
break
if (firstIdx1 = = - 1 or
lastIdx0 = = - 1 ):
return 0
count1 = 0
count0 = 0
for i in range ( 0 , lastIdx0):
if ( str [i] = = '1' ):
count1 + = 1
for i in range (firstIdx1 + 1 , n):
if ( str [i] = = '1' ):
count0 + = 1
return min (count0, count1)
str = "1000101"
print (minDeletion( str ))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static int minDeletion( string str)
{
int n = str.Length;
int firstIdx1 = -1;
int lastIdx0 = -1;
for ( int i = 0; i < n; i++)
{
if (str[i] == '1' )
{
firstIdx1 = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--)
{
if (str[i] == '0' )
{
lastIdx0 = i;
break ;
}
}
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
int count1 = 0, count0 = 0;
for ( int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1' )
{
count1++;
}
}
for ( int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1' )
{
count0++;
}
}
return Math.Min(count0, count1);
}
public static void Main()
{
string str = "1000101" ;
Console.WriteLine(minDeletion(str));
}
}
|
Javascript
<script>
function minDeletion(str)
{
let n = str.length;
let firstIdx1 = -1;
let lastIdx0 = -1;
for (let i = 0; i < n; i++)
{
if (str[i] == '1' )
{
firstIdx1 = i;
break ;
}
}
for (let i = n - 1; i >= 0; i--)
{
if (str[i] == '0' )
{
lastIdx0 = i;
break ;
}
}
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
let count1 = 0, count0 = 0;
for (let i = 0; i < lastIdx0; i++)
{
if (str[i] == '1' )
{
count1++;
}
}
for (let i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1' )
{
count0++;
}
}
return Math.min(count0, count1);
}
let str = "1000101" ;
document.write(minDeletion(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)