Minimize a binary string by repeatedly removing even length substrings of same characters | Recursive function to print stack elements from bottom to top without changing their order ; If stack is empty ; Pop top element of the stack ; Recursively call the function PrintStack ; Print the stack element from the bottom ; Push the same element onto the stack to preserve the order ; Function to minimize binary string by removing substrings consisting of same character ; Declare a stack of characters ; Push the first character of the string into the stack ; Traverse the string s ; If Stack is empty ; Push current character into the stack ; Check if the current character is same as the top of the stack ; If true , pop the top of the stack ; Otherwise , push the current element ; Print stack from bottom to top ; Driver Code
def PrintStack ( s ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT x = s [ - 1 ] ; NEW_LINE s . pop ( ) ; NEW_LINE PrintStack ( s ) ; NEW_LINE print ( x , end = " " ) ; NEW_LINE s . append ( x ) ; NEW_LINE DEDENT def minString ( s ) : NEW_LINE INDENT Stack = [ ] ; NEW_LINE Stack . append ( s [ 0 ] ) ; NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( len ( Stack ) == 0 ) : NEW_LINE INDENT Stack . append ( s [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( Stack [ - 1 ] == s [ i ] ) : NEW_LINE INDENT Stack . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT Stack . append ( s [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT PrintStack ( Stack ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "101001" ; NEW_LINE minString ( string ) ; NEW_LINE DEDENT