Open In App

Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2

Improve
Improve
Like Article
Like
Save
Share
Report

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)

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


Javascript




<script>
    // Javascript program for the above approach
     
    // Function to find the length of
    // the smallest string possible by
    // removing substrings "01" and "11"
    function shortestString(S, N)
    {
        
        // Stores the length of
        // the smallest string
        let st = 0;
  
        // Traverse the string S
        for (let 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;
    }
     
    // Input
    let S = "1010";
    let N = S.length;
 
    // Function call
    document.write(shortestString(S, N));
     
    // This code is contributed by divyeshrabadiya07.
</script>


Output: 

2

 

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

 



Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads