Skip to content
Related Articles

Related Articles

Improve Article
Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2
  • Last Updated : 11 Jun, 2021

Given a binary string S of length N, the task is to find the smallest string possible by removing all occurrences of substrings “01” and “11”. After removal of any substring, concatenate the remaining parts of the string.

Examples:

Input: S = “1010”
Output: 2
Explanation: Removal of substring “01” modifies string S to “10”.

Input: S = “111”
Output: 1 

 

Stack-based Approach: Refer to the previous article to find the length of the smallest string possible by given operations.
Time Complexity: O(N)
Auxiliary Space: O(N)

Spac-Optimized Approach: The above approach can be space-optimized by only storing the length of the characters not removed. Follow the steps below to solve the problem:



  • Initialize a variable, say st as 0, to store the length of the smallest string possible.
  • Iterate over the characters of the string S and perform the following steps:
    • If st is greater than 0 and S[i] is equal to ‘1‘, then pop the last element by decrementing st by 1.
    • Otherwise, increment st by 1.
  • Finally, after completing the above steps, print the answer obtained in st.

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 length of
// the smallest string possible by
// removing substrings "01" and "11"
int shortestString(string S, int N)
{
    // Stores the length of
    // the smallest string
    int st = 0;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // If st is greater
        // than 0 and S[i] is '1'
        if (st && S[i] == '1') {
 
            // Delete the last
            // character and
            // decrement st by 1
            st--;
        }
 
        // Otherwise
        else {
 
            // Increment st by 1
            st++;
        }
    }
 
    // Return the answer in st
    return st;
}
 
// Driver Code
int main()
{
    // Input
    string S = "1010";
 
    int N = S.length();
 
    // Function call
    cout << shortestString(S, N);
    return 0;
}


Java




// Java program for the above approach
public class GFG_JAVA {
 
    // Function to find the length of
    // the smallest string possible by
    // removing substrings "01" and "11"
    static int shortestString(String S, int N)
    {
        // Stores the length of
        // the smallest string
        int st = 0;
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // If st is greater
            // than 0 and S[i] is '1'
            if (st > 0 && S.charAt(i) == '1') {
 
                // Delete the last
                // character and
                // decrement st by 1
                st--;
            }
 
            // Otherwise
            else {
 
                // Increment st by 1
                st++;
            }
        }
 
        // Return the answer in st
        return st;
    }
 
    // Driver code
    public static void main(String[] args)
    {
      // Input
        String S = "1010";
        int N = S.length();
 
        // Function call
        System.out.println(shortestString(S, N));
    }
}
 
// This code is contributed by abhinavjain194


Python3




# Python3 program for the above approach
 
# Function to find the length of
# the smallest string possible by
# removing substrings "01" and "11"
def shortestString(S, N) :
     
    # Stores the length of
    # the smallest string
    st = 0;
 
    # Traverse the string S
    for i in range(N) :
 
        # If st is greater
        # than 0 and S[i] is '1'
        if (st and S[i] == '1') :
 
            # Delete the last
            # character and
            # decrement st by 1
            st -= 1;
 
        # Otherwise
        else :
 
            # Increment st by 1
            st += 1;
 
    # Return the answer in st
    return st;
 
# Driver Code
if __name__ == "__main__" :
 
    # Input
    S = "1010";
 
    N = len(S);
 
    # Function call
    print(shortestString(S, N));
     
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
public class GFG_JAVA {
 
    // Function to find the length of
    // the smallest string possible by
    // removing substrings "01" and "11"
    static int shortestString(string S, int N)
    {
       
        // Stores the length of
        // the smallest string
        int st = 0;
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // If st is greater
            // than 0 and S[i] is '1'
            if (st > 0 && S[i] == '1') {
 
                // Delete the last
                // character and
                // decrement st by 1
                st--;
            }
 
            // Otherwise
            else {
 
                // Increment st by 1
                st++;
            }
        }
 
        // Return the answer in st
        return st;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
      // Input
        string S = "1010";
        int N = S.Length;
 
        // Function call
        Console.WriteLine(shortestString(S, N));
    }
}
 
// This code is contributed by AnkThon


 
 

Output: 

2

 

 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes 




My Personal Notes arrow_drop_up
Recommended Articles
Page :