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.
- Maximum occurring character in an input string | Set-2
- 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 repeated frequency of characters in a given string
- Maximum length substring with highest frequency in a string
- Maximum number of removals of given subsequence from a string
- Maximum length prefix of one string that occurs as subsequence in another
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find the length of the longest subsequence with first K alphabets having same frequency
- Maximum occurring character in a linked list
- Check if max occurring character of one string appears same no. of times in other
- Count of Distinct Substrings occurring consecutively in a given String
- Count the number of vowels occurring in all the substrings of given string
- Maximum length Subsequence with alternating sign and maximum Sum
- Find number of times a string occurs as a subsequence in given string
- Find length of longest subsequence of one string which is substring of another string
- Sum of all maximum frequency elements in Matrix
- Maximum element in an array which is equal to its frequency
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.
Improved By : Amal Kumar Choubey