Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given String S ; Function Call
def firstSubsequence ( s ) : NEW_LINE INDENT isMax = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] > s [ i + 1 ] ) : NEW_LINE INDENT isMax = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( isMax >= 0 ) : NEW_LINE INDENT s = s [ 0 : isMax ] + s [ isMax + 1 : len ( s ) ] NEW_LINE DEDENT s . rerase ( isMax , 1 ) ; NEW_LINE else : NEW_LINE INDENT s . erase ( s . length ( ) - 1 , 1 ) ; NEW_LINE s = s [ 0 : s . length ( ) - 1 ] NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE firstSubsequence ( S ) NEW_LINE DEDENT