Lexicographically largest string formed from the characters in range L and R | Function to return the lexicographically largest String ; hash array ; make 0 - based indexing ; iterate and count frequencies of character ; ans String ; iterate in frequency array ; add til all characters are added ; Driver Code
function printLargestString ( s , l , r ) { var freq = Array ( 26 ) . fill ( 0 ) ; l -- ; r -- ; for ( i = Math . min ( l , r ) ; i <= Math . max ( l , r ) ; i ++ ) { freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; } var ans = " " ; for ( var i = 25 ; i >= 0 ; i -- ) { while ( freq [ i ] > 0 ) { ans += String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; freq [ i ] -- ; } } return ans ; } var s = " " ; var l = 3 , r = 5 ; document . write ( printLargestString ( s , l , r ) ) ;