Find the length of the longest subsequence with first K alphabets having same frequency | C # 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 ; Store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int lengthOfSubsequence ( string str , int K ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; foreach ( char ch in str . ToCharArray ( ) ) { mp [ ch ] = mp . GetValueOrDefault ( ch , 0 ) + 1 ; } int minimum = mp [ ' A ' ] ; for ( int i = 1 ; i < K ; i ++ ) { minimum = Math . Min ( minimum , mp [ ( char ) ( i + ' A ' ) ] ) ; } return minimum * K ; } public static void Main ( string [ ] args ) { string str = " ACAABCCAB " ; int K = 3 ; Console . Write ( lengthOfSubsequence ( str , K ) ) ; } }