Count of maximum occurring subsequence using only

Count of maximum occurring subsequence using only

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

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

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

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

}

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.

dynamic programming mathematical strings algorithms-dynamic programming frequency-counting geometric progression subsequence

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Getting Started with dynamic programming

Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs.

Count of Distinct Substrings occurring consecutively in a given String

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Count of binary strings of length N having equal count of 0's and 1's and count

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

C program to find the frequency of characters in a string

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.