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.
Input: str = “1000101”
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”
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 before 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:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Minimum characters required to be removed to make frequency of each character unique
- Minimum number of characters to be removed to make a binary string alternate
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Print number in ascending order which contains 1, 2 and 3 in their digits.
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Minimum removal of consecutive similar characters required to empty a Binary String
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Minimum adjacent swaps required to Sort Binary array
- Minimum size substring to be removed to make a given string palindromic
- Minimum operations required to convert all characters of a String to a given Character
- Minimum removal of characters required such that permutation of given string is a palindrome
- Minimum number of Appends of X or Y characters from the end to the front required to obtain given string
- Minimum removal of subsequences of distinct consecutive characters required to empty a given string
- Sort a string according to the order defined by another string
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.