Lexicographically smallest string formed repeatedly deleting character from substring 10 | Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Driver Code ; Input ; Function Call
def lexicographicallySmallestString ( S , N ) : NEW_LINE INDENT LastZe = - 1 NEW_LINE ans = " " NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT LastZe = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( i <= LastZe and S [ i ] == '0' ) : NEW_LINE INDENT ans += S [ i ] NEW_LINE DEDENT elif ( i > LastZe ) : NEW_LINE INDENT ans += S [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "11001101" NEW_LINE N = len ( S ) NEW_LINE print ( lexicographicallySmallestString ( S , N ) ) NEW_LINE DEDENT