Minimum characters required to be removed to sort binary string in ascending order
Last Updated :
14 Apr, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
int minDeletion(string str)
{
// Length of given string
int n = str.length();
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for(int i = 0; i < n; i++)
{
if (str[i] == '1')
{
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for(int i = n - 1; i >= 0; i--)
{
if (str[i] == '0')
{
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for(int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1')
{
count1++;
}
}
// Traverse the string to count1
for(int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1')
{
count0++;
}
}
// Return the minimum of
// count0 and count1
return min(count0, count1);
}
// Driver code
int main()
{
// Given string str
string str = "1000101";
// Function call
cout << minDeletion(str);
return 0;
}
// This code is contributed by bikram2001jha
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG {
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
static int minDeletion(String str)
{
// Length of given string
int n = str.length();
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for (int i = 0; i < n; i++) {
if (str.charAt(i) == '1') {
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for (int i = n - 1; i >= 0; i--) {
if (str.charAt(i) == '0') {
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1
|| lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for (int i = 0; i < lastIdx0; i++) {
if (str.charAt(i) == '1') {
count1++;
}
}
// Traverse the string to count1
for (int i = firstIdx1 + 1; i < n; i++) {
if (str.charAt(i) == '1') {
count0++;
}
}
// Return the minimum of
// count0 and count1
return Math.min(count0, count1);
}
// Driver Code
public static void main(String[] args)
{
// Given string str
String str = "1000101";
// Function Call
System.out.println(minDeletion(str));
}
}
Python3
# Python3 program for the above approach
# Function to find the minimum count
# of characters to be removed to make
# the string sorted in ascending order
def minDeletion(s):
# Length of given string
n = len(s)
# Stores the first
# occurrence of '1'
firstIdx1 = -1
# Stores the last
# occurrence of '0'
lastIdx0 = -1
# Traverse the string to find
# the first occurrence of '1'
for i in range(0, n):
if (str[i] == '1'):
firstIdx1 = i
break
# Traverse the string to find
# the last occurrence of '0'
for i in range(n - 1, -1, -1):
if (str[i] == '0'):
lastIdx0 = i
break
# Return 0 if the str have
# only one type of character
if (firstIdx1 == -1 or
lastIdx0 == -1):
return 0
# Initialize count1 and count0 to
# count '1's before lastIdx0
# and '0's after firstIdx1
count1 = 0
count0 = 0
# Traverse the string to count0
for i in range(0, lastIdx0):
if (str[i] == '1'):
count1 += 1
# Traverse the string to count1
for i in range(firstIdx1 + 1, n):
if (str[i] == '1'):
count0 += 1
# Return the minimum of
# count0 and count1
return min(count0, count1)
# Driver code
# Given string str
str = "1000101"
# Function call
print(minDeletion(str))
# This code is contributed by Stream_Cipher
C#
// C# program for the above approach
using System.Collections.Generic;
using System;
class GFG{
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
static int minDeletion(string str)
{
// Length of given string
int n = str.Length;
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for(int i = 0; i < n; i++)
{
if (str[i] == '1')
{
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for(int i = n - 1; i >= 0; i--)
{
if (str[i] == '0')
{
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for(int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1')
{
count1++;
}
}
// Traverse the string to count1
for(int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1')
{
count0++;
}
}
// Return the minimum of
// count0 and count1
return Math.Min(count0, count1);
}
// Driver Code
public static void Main()
{
// Given string str
string str = "1000101";
// Function call
Console.WriteLine(minDeletion(str));
}
}
// This code is contributed by Stream_Cipher
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
function minDeletion(str)
{
// Length of given string
let n = str.length;
// Stores the first
// occurrence of '1'
let firstIdx1 = -1;
// Stores the last
// occurrence of '0'
let lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for(let i = 0; i < n; i++)
{
if (str[i] == '1')
{
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for(let i = n - 1; i >= 0; i--)
{
if (str[i] == '0')
{
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
let count1 = 0, count0 = 0;
// Traverse the string to count0
for(let i = 0; i < lastIdx0; i++)
{
if (str[i] == '1')
{
count1++;
}
}
// Traverse the string to count1
for(let i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1')
{
count0++;
}
}
// Return the minimum of
// count0 and count1
return Math.min(count0, count1);
}
// Driver code
// Given string str
let str = "1000101";
// Function call
document.write(minDeletion(str));
// This code is contributed by target_2.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum characters required to be removed to sort binary string in ascending order - Set 2 Given binary string str of size N, the task is to remove the minimum number of characters from the given binary string such that the characters in the remaining string are in sorted order. Examples: Input: str = â1000101âOutput: 2Explanation: Removal of the first two occurrences of â1â modifies the
10 min read
Minimum number of characters to be removed to make a binary string alternate Given a binary string, the task is to find minimum number of characters to be removed from it so that it becomes alternate. A binary string is alternate if there are no two consecutive 0s or 1s.Examples : Input : s = "000111" Output : 4 We need to delete two 0s and two 1s to make string alternate. I
4 min read
Minimum removal of consecutive similar characters required to empty a Binary String Given a binary string S of length N, the task is to find the minimum number of removal of adjacent similar characters required to empty the given binary string. Examples: Input: S = â1100011âOutput: 2Explanation:Operation 1: Removal of all 0s modifies S to â1111â.Operation 2: Removal of all remainin
8 min read
Minimum cost of flipping characters required to convert Binary String to 0s only Given binary string str, and integers A, which denotes the cost of converting consecutive 1s to 0s, and B, which denotes the cost of converting 0s to 1s. The task is to find the minimum cost to reduce the string str to 0s only. Examples: Input: str = â01101110â, A = 5, B = 1Output: 6Explanation:Conv
8 min read
Minimum non-adjacent pair flips required to remove all 0s from a Binary String The problem statement asks us to find the minimum number of operations required to remove all 0s from a given binary string S. An operation involves flipping at most two non-adjacent characters (i.e., characters that are not next to each other) in the string. Let's take the first example given: S =
11 min read