Find the length of the longest subsequence with first K alphabets having same frequency | Java program to find 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
import java . util . * ; class GFG { static int lengthOfSubsequence ( String str , int K ) { Map < Character , Integer > mp = new HashMap < > ( ) ; for ( char ch : str . toCharArray ( ) ) { mp . put ( ch , mp . getOrDefault ( ch , 0 ) + 1 ) ; } int minimum = mp . get ( ' A ' ) ; for ( int i = 1 ; i < K ; i ++ ) { minimum = Math . min ( minimum , mp . get ( ( char ) ( i + ' A ' ) ) ) ; } return minimum * K ; } public static void main ( String [ ] args ) { String str = " ACAABCCAB " ; int K = 3 ; System . out . println ( lengthOfSubsequence ( str , K ) ) ; } }