Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIMinimum characters required to be removed to sort binary string in ascending...

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

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:

  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 <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>


Output

2

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments