Maximum non | Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the final answer ; Count this character if freq 1 ; Return count of distinct characters ; Given string ; Given k ; Function Call
function maxDistinctChar ( s , n , k ) { var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq . has ( s [ i ] ) ) freq . set ( s [ i ] , freq . get ( s [ i ] ) + 1 ) else freq . set ( s [ i ] , 1 ) } var v = [ ] ; freq . forEach ( ( value , key ) => { v . push ( value ) ; } ) ; v . sort ( ) for ( var i = 0 ; i < v . length ; i ++ ) { var mn = Math . min ( v [ i ] - 1 , k ) ; v [ i ] -= mn ; k -= mn ; } if ( k > 0 ) { for ( var i = 0 ; i < v . length ; i ++ ) { var mn = Math . min ( v [ i ] , k ) ; v [ i ] -= mn ; k -= mn ; } } var res = 0 ; for ( var i = 0 ; i < v . length ; i ++ ) if ( v [ i ] == 1 ) res ++ ; return res ; } var S = " " ; var N = S . length ; var K = 1 ; document . write ( maxDistinctChar ( S , N , K ) ) ;