Count of maximum occurring subsequence using only those characters whose indices are in GP
Last Updated :
02 Jun, 2021
Given a string S, the task is to find the count of maximum occurring subsequence P from S using only those characters whose indexes are in Geometric Progression.
Note: Consider 1-based indexing in S.
Examples :
Input: S = “ddee”
Output: 4
Explanation:
If we take P = “de”, then P occurs 4 times in S. { {1, 3}, {1, 4}, {2, 3}, {2, 4} }
Input: S = “geeksforgeeks”
Output: 6
Explanation:
If we take P = “ek”, then P occurs 6 times in S. { {2, 4}, {3, 4}, {2, 12} {3, 12}, {10, 12}, {11, 12} }
Naive Approach: The idea is to generate all possible subsequences of the given string such that indexes of the string must be in geometric progression. Now for each subsequence generated, find the occurrence of each subsequence and print the maximum among those occurrences.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe that any subsequence P can be of any length. Let’s say if P = “abc” and it occurs 10 times in S (where “abc” have their index in GP in S), then we can see that subsequence “ab” (having index in GP) will also occur 10 times in S. So, to simplify the solution, the possible length of P will be less than equal to 2. Below are the steps:
- It is necessary to choose the subsequence P of length greater than 1 because P of length greater than 1 will occur much more time than of length one if S doesn’t contain only unique characters.
- 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>
using namespace std;
int findMaxTimes(string S)
{
long long int arr[26];
long long int dp[26][26];
memset (arr, 0, sizeof (arr));
memset (dp, 0, sizeof (dp));
for ( int i = 0; i < S.size(); i++) {
int now = S[i] - 'a' ;
for ( int j = 0; j < 26; j++) {
dp[j][now] += arr[j];
}
arr[now]++;
}
long long int ans = 0;
for ( int i = 0; i < 26; i++)
ans = max(ans, arr[i]);
for ( int i = 0; i < 26; i++) {
for ( int j = 0; j < 26; j++) {
ans = max(ans, dp[i][j]);
}
}
return ans;
}
int main()
{
string S = "ddee" ;
cout << findMaxTimes(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMaxTimes(String S)
{
int []arr = new int [ 26 ];
int [][]dp = new int [ 26 ][ 26 ];
for ( int i = 0 ; i < S.length(); i++)
{
int now = S.charAt(i) - 'a' ;
for ( int j = 0 ; j < 26 ; j++)
{
dp[j][now] += arr[j];
}
arr[now]++;
}
int ans = 0 ;
for ( int i = 0 ; i < 26 ; i++)
ans = Math.max(ans, arr[i]);
for ( int i = 0 ; i < 26 ; i++)
{
for ( int j = 0 ; j < 26 ; j++)
{
ans = Math.max(ans, dp[i][j]);
}
}
return ans;
}
public static void main(String[] args)
{
String S = "ddee" ;
System.out.print(findMaxTimes(S));
}
}
|
Python3
def findMaxTimes(S):
arr = [ 0 ] * 26
dp = [[ 0 for x in range ( 26 )]
for y in range ( 26 )]
for i in range ( len (S)):
now = ord (S[i]) - ord ( 'a' )
for j in range ( 26 ):
dp[j][now] + = arr[j]
arr[now] + = 1
ans = 0
for i in range ( 26 ):
ans = max (ans, arr[i])
for i in range ( 26 ):
for j in range ( 26 ):
ans = max (ans, dp[i][j])
return ans
S = "ddee"
print (findMaxTimes(S))
|
C#
using System;
class GFG{
static int findMaxTimes(String S)
{
int []arr = new int [26];
int [,]dp = new int [26, 26];
for ( int i = 0; i < S.Length; i++)
{
int now = S[i] - 'a' ;
for ( int j = 0; j < 26; j++)
{
dp[j, now] += arr[j];
}
arr[now]++;
}
int ans = 0;
for ( int i = 0; i < 26; i++)
ans = Math.Max(ans, arr[i]);
for ( int i = 0; i < 26; i++)
{
for ( int j = 0; j < 26; j++)
{
ans = Math.Max(ans, dp[i, j]);
}
}
return ans;
}
public static void Main(String[] args)
{
String S = "ddee" ;
Console.Write(findMaxTimes(S));
}
}
|
Javascript
<script>
function findMaxTimes(S)
{
var arr = Array(26).fill(0);
var dp = Array.from(Array(26), ()=>Array(26).fill(0));
for ( var i = 0; i < S.length; i++)
{
var now = S[i].charCodeAt(0) - 'a' .charCodeAt(0);
for ( var j = 0; j < 26; j++)
{
dp[j][now] += arr[j];
}
arr[now]++;
}
var ans = 0;
for ( var i = 0; i < 26; i++)
ans = Math.max(ans, arr[i]);
for ( var i = 0; i < 26; i++)
{
for ( var j = 0; j < 26; j++)
{
ans = Math.max(ans, dp[i][j]);
}
}
return ans;
}
var S = "ddee" ;
document.write( findMaxTimes(S));
</script>
|
Time Complexity: O(max(N*26, 26 * 26))
Auxiliary Space: O(26 * 26)
Share your thoughts in the comments
Please Login to comment...