Count of maximum occurring subsequence using only those characters whose indices are in GP

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:

  1. 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.
  2. For length 1 count the frequency of each alphabet in the string.
  3. 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).
  4. 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).

  5. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
int findMaxTimes(string S)
{
    long long int arr[26];
    long long int dp[26][26];
  
    // Initialize 1-D array and 2-D
    // dp array to 0
    memset(arr, 0, sizeof(arr));
    memset(dp, 0, sizeof(dp));
  
    // Iterate till the length of
    // the given string
    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;
  
    // Update ans for 1-length subsequence
    for (int i = 0; i < 26; i++)
        ans = max(ans, arr[i]);
  
    // Update ans for 2-length subsequence
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            ans = max(ans, dp[i][j]);
        }
    }
  
    // Return the answer
    return ans;
}
  
// Driver Code
int main()
{
    // Given string s
    string S = "ddee";
  
    // Function Call
    cout << findMaxTimes(S);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
static int findMaxTimes(String S)
{
    int []arr = new int[26];
    int [][]dp = new int[26][26];
  
    // Iterate till the length of
    // the given String
    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;
  
    // Update ans for 1-length subsequence
    for(int i = 0; i < 26; i++)
        ans = Math.max(ans, arr[i]);
  
    // Update ans for 2-length subsequence
    for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < 26; j++) 
        {
            ans = Math.max(ans, dp[i][j]);
        }
    }
  
    // Return the answer
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given String s
    String S = "ddee";
  
    // Function call
    System.out.print(findMaxTimes(S));
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach 
  
# Function to count maximum occurring
# subsequence using only those characters
# whose indexes are in GP
def findMaxTimes(S):
  
    # Initialize 1-D array and 2-D
    # dp array to 0
    arr = [0] * 26
    dp = [[0 for x in range(26)] 
             for y in range(26)]
  
    # Iterate till the length of
    # the given string
    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
  
    # Update ans for 1-length subsequence
    for i in range(26):
        ans = max(ans, arr[i])
  
    # Update ans for 2-length subsequence
    for i in range(26):
        for j in range(26):
            ans = max(ans, dp[i][j])
  
    # Return the answer
    return ans
  
# Driver Code
  
# Given string s
S = "ddee"
  
# Function call
print(findMaxTimes(S))
  
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
  
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
static int findMaxTimes(String S)
{
    int []arr = new int[26];
    int [,]dp = new int[26, 26];
  
    // Iterate till the length of
    // the given String
    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;
  
    // Update ans for 1-length subsequence
    for(int i = 0; i < 26; i++)
        ans = Math.Max(ans, arr[i]);
  
    // Update ans for 2-length subsequence
    for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < 26; j++) 
        {
            ans = Math.Max(ans, dp[i, j]);
        }
    }
  
    // Return the answer
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given String s
    String S = "ddee";
  
    // Function call
    Console.Write(findMaxTimes(S));
}
}
  
// This code is contributed by gauravrajput1

chevron_right


Output: 

4



 

Time Complexity: O(max(N*26, 26 * 26))
Auxiliary Space: O(26 * 26)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.