Frequency of maximum occurring subsequence in given string
Last Updated :
04 Jan, 2023
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: 2
Explanation:
For “aba”, subsequence “ab” occurs maximum times in subsequence ‘ab’ and ‘aba’.
Input: s = “acbab”
Output: 3
Explanation:
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++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll findCount(string s)
{
ll freq[26];
ll dp[26][26];
memset (freq, 0, sizeof freq);
memset (dp, 0, sizeof dp);
for ( int i = 0; i < s.size(); ++i) {
for ( int j = 0; j < 26; j++) {
dp[j][s[i] - 'a' ] += freq[j];
}
freq[s[i] - 'a' ]++;
}
ll ans = 0;
for ( int i = 0; i < 26; i++)
ans = max(freq[i], ans);
for ( int i = 0; i < 26; i++) {
for ( int j = 0; j < 26; j++) {
ans = max(dp[i][j], ans);
}
}
return ans;
}
int main()
{
string str = "acbab" ;
cout << findCount(str);
return 0;
}
|
Java
class GFG{
static int findCount(String s)
{
int []freq = new int [ 26 ];
int [][]dp = new int [ 26 ][ 26 ];
for ( int i = 0 ; i < s.length(); ++i)
{
for ( int j = 0 ; j < 26 ; j++)
{
dp[j][s.charAt(i) - 'a' ] += freq[j];
}
freq[s.charAt(i) - 'a' ]++;
}
int ans = 0 ;
for ( int i = 0 ; i < 26 ; i++)
ans = Math.max(freq[i], ans);
for ( int i = 0 ; i < 26 ; i++)
{
for ( int j = 0 ; j < 26 ; j++)
{
ans = Math.max(dp[i][j], ans);
}
}
return ans;
}
public static void main(String[] args)
{
String str = "acbab" ;
System.out.print(findCount(str));
}
}
|
Python3
import numpy
def findCount(s):
freq = [ 0 ] * 26
dp = [[ 0 ] * 26 ] * 26
freq = numpy.zeros( 26 )
dp = numpy.zeros([ 26 , 26 ])
for i in range ( 0 , len (s)):
for j in range ( 26 ):
dp[j][ ord (s[i]) - ord ( 'a' )] + = freq[j]
freq[ ord (s[i]) - ord ( 'a' )] + = 1
ans = 0
for i in range ( 26 ):
ans = max (freq[i], ans)
for i in range ( 0 , 26 ):
for j in range ( 0 , 26 ):
ans = max (dp[i][j], ans)
return int (ans)
str = "acbab"
print (findCount( str ))
|
C#
using System;
class GFG{
static int findCount(String s)
{
int []freq = new int [26];
int [,]dp = new int [26, 26];
for ( int i = 0; i < s.Length; ++i)
{
for ( int j = 0; j < 26; j++)
{
dp[j, s[i] - 'a' ] += freq[j];
}
freq[s[i] - 'a' ]++;
}
int ans = 0;
for ( int i = 0; i < 26; i++)
ans = Math.Max(freq[i], ans);
for ( int i = 0; i < 26; i++)
{
for ( int j = 0; j < 26; j++)
{
ans = Math.Max(dp[i, j], ans);
}
}
return ans;
}
public static void Main(String[] args)
{
String str = "acbab" ;
Console.Write(findCount(str));
}
}
|
Javascript
<script>
function findCount(s)
{
var freq = Array(26).fill(0);
var dp = Array.from(Array(26), ()=>Array(26).fill(0));
for ( var i = 0; i < s.length; ++i) {
for ( var j = 0; j < 26; j++) {
dp[j][s[i].charCodeAt(0) -
'a' .charCodeAt(0)] += freq[j];
}
freq[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
var ans = 0;
for ( var i = 0; i < 26; i++)
ans = Math.max(freq[i], ans);
for ( var i = 0; i < 26; i++) {
for ( var j = 0; j < 26; j++) {
ans = Math.max(dp[i][j], ans);
}
}
return ans;
}
var str = "acbab" ;
document.write( findCount(str));
</script>
|
Time Complexity: O(26*N), where N is the length of the given string.
Auxiliary Space: O(M), where M = 26*26
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...