Find the length of the longest subsequence with first K alphabets having same frequency | Function to return the length of the longest subsequence with first K alphabets having same frequency ; Map to store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
function lengthOfSubsequence ( str , K ) { var mp = new Map ( ) ; str . split ( ' ' ) . forEach ( ch => { if ( mp . has ( ch ) ) mp . set ( ch , mp . get ( ch ) + 1 ) else mp . set ( ch , 1 ) } ) ; var minimum = mp . get ( ' ' ) ; for ( var i = 1 ; i < K ; i ++ ) { minimum = Math . min ( minimum , mp . get ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ) ; } return minimum * K ; } var str = " " ; var K = 3 ; document . write ( lengthOfSubsequence ( str , K ) ) ;