Minimum number of removals required such that no subsequence of length 2 occurs more than once | Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver Code
def RemoveCharacters ( s ) : NEW_LINE INDENT ans = " " NEW_LINE c = [ 0 for i in range ( 26 ) ] NEW_LINE pos = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( c [ ord ( s [ i ] ) - 97 ] == 0 ) : NEW_LINE INDENT c [ ord ( s [ i ] ) - 97 ] = 1 NEW_LINE pos = i NEW_LINE ans += s [ i ] NEW_LINE DEDENT DEDENT for i in range ( pos + 1 , len ( s ) , 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ 0 ] ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcaadbcd " NEW_LINE RemoveCharacters ( S ) NEW_LINE DEDENT