VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-length-of-the-longest-subsequence-with-first-k-alphabets-having-same-frequency/

⇱ Find the length of the longest subsequence with first K alphabets having same frequency - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the length of the longest subsequence with first K alphabets having same frequency

Last Updated : 28 May, 2021

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:
Explanation: One of the possible subsequences is "ACABCB".


Input: str = "ACAABCCAB", K=4 
Output:
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:


Output: 
6

 
Comment
Article Tags: