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 = “neveropen”
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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Javascript program for the above approach // Function to count maximum occurring // subsequence using only those characters // whose indexes are in GP function findMaxTimes(S) { var arr = Array(26).fill(0); var dp = Array.from(Array(26), ()=>Array(26).fill(0)); // Iterate till the length of // the given string 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; // Update ans for 1-length subsequence for ( var i = 0; i < 26; i++) ans = Math.max(ans, arr[i]); // Update ans for 2-length subsequence for ( var i = 0; i < 26; i++) { for ( var j = 0; j < 26; j++) { ans = Math.max(ans, dp[i][j]); } } // Return the answer return ans; } // Driver Code // Given string s var S = "ddee" ; // Function Call document.write( findMaxTimes(S)); // This code is contributed by noob2000. </script> |
4
Time Complexity: O(max(N*26, 26 * 26))
Auxiliary Space: O(26 * 26)