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
function RemoveCharacters ( s ) { var ans = " " ; var c = new Array ( 26 ) ; var i ; for ( i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; var pos = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( c [ s [ i ] . charCodeAt ( ) - 97 ] == 0 ) { c [ s [ i ] . charCodeAt ( ) - 97 ] = 1 ; pos = i ; ans += s [ i ] ; } } for ( i = pos + 1 ; i < s . length ; i ++ ) { if ( s [ i ] == s [ 0 ] ) { ans += s [ i ] ; break ; } } document . write ( ans ) ; } var S = " " ; RemoveCharacters ( S ) ;