Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
function maximumLengthSubsequence ( str ) { var mp = { } ; var temp = str . split ( " " ) ; for ( const ch of temp ) { if ( mp . hasOwnProperty ( ch ) ) mp [ ch ] = mp [ ch ] + 1 ; else mp [ ch ] = 1 ; } var ans = 0 ; for ( const ch of temp ) { if ( mp . hasOwnProperty ( String . fromCharCode ( ch . charCodeAt ( 0 ) - 1 ) ) ) { var curr_max = mp [ ch ] + mp [ String . fromCharCode ( ch . charCodeAt ( 0 ) - 1 ) ] ; ans = Math . max ( ans , curr_max ) ; } } document . write ( ans ) ; } var S = " " ; maximumLengthSubsequence ( S ) ;