# Minimum characters required to be removed to sort binary string in ascending order

• Difficulty Level : Medium
• 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:
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:

1. Traverse the string str and find the position of the first occurrence of 1 and the last occurrence of 0.
2. Print 0 if the str has only one type of character.
3. Now, count the number of 1 is present prior to the last occurrence of 0 and store in a variable, say cnt1.
4. Now, count the number of 0s present after the first occurrence of 1 in a variable, say cnt0.
5. 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 ``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

 ``

Output

`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up