![]() |
VOOZH | about |
Given string str with uppercase characters and an integer K, the task is to find the length of the longest subsequence such that the frequency of the first K alphabet is the same.
Examples:
Input: str = "ACAABCCAB", K=3
Output: 6
Explanation: One of the possible subsequences is "ACABCB".
Input: str = "ACAABCCAB", K=4
Output: 0
Explanation: Since the string does not contain 'D', no such subsequence can be obtained.
Approach:
Traverse the string and find the least frequent of the first K alphabets. Once found, (frequency of that element) * K gives the desired result.
Below is the implementation of the above approach:
6