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.**Examples:**

Input:s = “aba”Output:2Explanation:

For “aba”, subsequence “ab” occurs maximum times in subsequence ‘ab’ and ‘aba’.Input:s = “acbab”Output:3Explanation:

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[26][26]**, 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]

where,

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:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `#define ll long long ` `using` `namespace` `std; ` ` ` `// Function to find the frequency ` `ll findCount(string s) ` `{ ` ` ` `// freq stores frequnecy of each ` ` ` `// english lowercase character ` ` ` `ll freq[26]; ` ` ` ` ` `// dp[i][j] stores the count of ` ` ` `// subsequnce with 'a' + i ` ` ` `// and 'a' + j character ` ` ` `ll dp[26][26]; ` ` ` ` ` `memset` `(freq, 0, ` `sizeof` `freq); ` ` ` ` ` `// Intialize dp to 0 ` ` ` `memset` `(dp, 0, ` `sizeof` `dp); ` ` ` ` ` `for` `(` `int` `i = 0; i < s.size(); ++i) { ` ` ` ` ` `for` `(` `int` `j = 0; j < 26; j++) { ` ` ` ` ` `// Increment the count of ` ` ` `// subsequence j and s[i] ` ` ` `dp[j][s[i] - ` `'a'` `] += freq[j]; ` ` ` `} ` ` ` ` ` `// Update the frequency array ` ` ` `freq[s[i] - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `ll ans = 0; ` ` ` ` ` `// For 1 length subsequence ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `ans = max(freq[i], ans); ` ` ` ` ` `// For 2 length subsequence ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` `for` `(` `int` `j = 0; j < 26; j++) { ` ` ` ` ` `ans = max(dp[i][j], ans); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final result ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given string str ` ` ` `string str = ` `"acbab"` `; ` ` ` ` ` `// Function Call ` ` ` `cout << findCount(str); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to find the frequency ` `static` `int` `findCount(String s) ` `{ ` ` ` ` ` `// freq stores frequnecy of each ` ` ` `// english lowercase character ` ` ` `int` `[]freq = ` `new` `int` `[` `26` `]; ` ` ` ` ` `// dp[i][j] stores the count of ` ` ` `// subsequnce with 'a' + i ` ` ` `// and 'a' + j character ` ` ` `int` `[][]dp = ` `new` `int` `[` `26` `][` `26` `]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < s.length(); ++i) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) ` ` ` `{ ` ` ` ` ` `// Increment the count of ` ` ` `// subsequence j and s[i] ` ` ` `dp[j][s.charAt(i) - ` `'a'` `] += freq[j]; ` ` ` `} ` ` ` ` ` `// Update the frequency array ` ` ` `freq[s.charAt(i) - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// For 1 length subsequence ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `ans = Math.max(freq[i], ans); ` ` ` ` ` `// For 2 length subsequence ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) ` ` ` `{ ` ` ` `ans = Math.max(dp[i][j], ans); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final result ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given String str ` ` ` `String str = ` `"acbab"` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(findCount(str)); ` `} ` `} ` ` ` `// This code contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

3

**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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.org. 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.