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 ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lengthOfSubsequence ( string str , int K ) { map < char , int > mp ; for ( char ch : str ) { mp [ ch ] ++ ; } int minimum = mp [ ' A ' ] ; for ( int i = 1 ; i < K ; i ++ ) { minimum = min ( minimum , mp [ ( char ) ( i + ' A ' ) ] ) ; } return minimum * K ; } int main ( ) { string str = " ACAABCCAB " ; int K = 3 ; cout << lengthOfSubsequence ( str , K ) ; return 0 ; }