Given a string str of lowercase English alphabets, our task is to find the frequency of occurrence a subsequence of the string which occurs the maximum times.
Input: s = “aba”
For “aba”, subsequence “ab” occurs maximum times in subsequence ‘ab’ and ‘aba’.
Input: s = “acbab”
For “acbab”, “ab” occurs 3 times which is the maximum.
Approach: The problem can be solved using Dynamic Programming. To solve the problem mentioned above the key observation is that the resultant subsequence will be of length 1 or 2 because frequency of any subsequence of length > 2 will be lower than the subsequence of length 1 or 2 as they are also present in higher length subsequences. So we need to check for the subsequence of length 1 or 2 only. Below are the steps:
- For length 1 count the frequency of each alphabet in the string.
- For length 2 form a 2D array dp, where dp[i][j] tells frequency of string of char(‘a’ + i) + char(‘a’ + j).
- The recurrence relation is used in the step 2 is given by:
dp[i][j] = dp[i][j] + freq[i]
freq[i] = frequency of character char(‘a’ + i)
dp[i][j] = frequency of string formed by current_character + char(‘a’ + i).
- The maximum of frequency array and array dp gives the maximum count of any subsequence in the given string.
Below is the implementation of the above approach:
Time Complexity: O(26*N), where N is the length of the given string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Smallest occurring element in each subsequence
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Return maximum occurring character in an input string
- Maximum occurring character in an input string | Set-2
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the maximum occurring character after performing the given operations
- Count the number of vowels occurring in all the substrings of given string
- Count of Distinct Substrings occurring consecutively in a given String
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Maximum occurring character in a linked list
- Maximum repeated frequency of characters in a given string
- Substring of length K having maximum frequency in the given string
- Check if max occurring character of one string appears same no. of times in other
- Find the length of the longest subsequence with first K alphabets having same frequency
- Maximum number of removals of given subsequence from a string
- Maximum length substring with highest frequency in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.