Find the length of the longest subsequence with first K alphabets having same frequency | Python3 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
from collections import defaultdict NEW_LINE def lengthOfSubsequence ( st , K ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE for ch in st : NEW_LINE INDENT mp [ ch ] += 1 NEW_LINE DEDENT minimum = mp [ ' A ' ] NEW_LINE for i in range ( 1 , K ) : NEW_LINE INDENT minimum = min ( minimum , mp [ chr ( i + ord ( ' A ' ) ) ] ) NEW_LINE DEDENT return ( minimum * K ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " ACAABCCAB " NEW_LINE K = 3 NEW_LINE print ( lengthOfSubsequence ( st , K ) ) NEW_LINE DEDENT