Given a string **S**, the task is to count the maximum occurrence of subsequence in the given string such that indices of the characters of the subsequence are in Arithmetic Progression.

**Examples:**

Input:S = “xxxyy”

Output:6

Explanation:

There is a subsequence “xy”, where indices of each character of the subsequence are in A.P.

The indices of the different characters that form the subsequence “xy” –

{(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)}

Input:S = “pop”

Output:2

Explanation:

There is a subsequence “p”, where indices of each character of the subsequence are in A.P.

The indices of the different characters that form the subsequence “p” –

{(1), (2)}

**Approach:** The key observation in the problem is if there are two characters in string whose collectively occurrence is greater than the occurrence of any single character, then these characters will form the maximum occurrence subsequence in the string with the character in Arithmetic progression because of every two integers will always form an arithmetic progression. Below is the illustration of the steps:

- Iterate over the string and count the frequency of the characters of the string. That is considering the subsequences of length 1.
- Iterate over the string and choose every two possible characters of the string and increment the frequency of the subsequence of the string.
- Finally, find the maximum frequency of the subsequence from length 1 and 2.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `int` `maximumOccurrence(string s) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Frequencies of subsequence ` ` ` `map<string, ` `int` `> freq; ` ` ` ` ` `// Loop to find the frequencies ` ` ` `// of subsequence of length 1 ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `string temp = ` `""` `; ` ` ` `temp += s[i]; ` ` ` `freq[temp]++; ` ` ` `} ` ` ` ` ` `// Loop to find the frequencies ` ` ` `// subsequence of length 2 ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` `string temp = ` `""` `; ` ` ` `temp += s[i]; ` ` ` `temp += s[j]; ` ` ` `freq[temp]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `answer = INT_MIN; ` ` ` ` ` `// Finding maximum frequency ` ` ` `for` `(` `auto` `it : freq) ` ` ` `answer = max(answer, it.second); ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"xxxyy"` `; ` ` ` ` ` `cout << maximumOccurrence(s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the ` ` ` `// maximum occurence of the subsequence ` ` ` `// such that the indices of characters ` ` ` `// are in arithmetic progression ` ` ` `static` `int` `maximumOccurrence(String s) ` ` ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Frequencies of subsequence ` ` ` `HashMap<String, Integer> freq = ` `new` `HashMap<String,Integer>(); ` ` ` `int` `i, j; ` ` ` ` ` `// Loop to find the frequencies ` ` ` `// of subsequence of length 1 ` ` ` `for` `( i = ` `0` `; i < n; i++) { ` ` ` `String temp = ` `""` `; ` ` ` `temp += s.charAt(i); ` ` ` `if` `(freq.containsKey(temp)){ ` ` ` `freq.put(temp,freq.get(temp)+` `1` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `freq.put(temp, ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Loop to find the frequencies ` ` ` `// subsequence of length 2 ` ` ` `for` `(i = ` `0` `; i < n; i++) { ` ` ` `for` `(j = i + ` `1` `; j < n; j++) { ` ` ` `String temp = ` `""` `; ` ` ` `temp += s.charAt(i); ` ` ` `temp += s.charAt(j); ` ` ` `if` `(freq.containsKey(temp)) ` ` ` `freq.put(temp,freq.get(temp)+` `1` `); ` ` ` `else` ` ` `freq.put(temp,` `1` `); ` ` ` `} ` ` ` `} ` ` ` `int` `answer = Integer.MIN_VALUE; ` ` ` ` ` `// Finding maximum frequency ` ` ` `for` `(` `int` `it : freq.values()) ` ` ` `answer = Math.max(answer, it); ` ` ` `return` `answer; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `String s = ` `"xxxyy"` `; ` ` ` ` ` `System.out.print(maximumOccurrence(s)); ` ` ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# maximum occurence of the subsequence ` `# such that the indices of characters ` `# are in arithmetic progression ` ` ` `# Function to find the ` `# maximum occurence of the subsequence ` `# such that the indices of characters ` `# are in arithmetic progression ` `def` `maximumOccurrence(s): ` ` ` `n ` `=` `len` `(s) ` ` ` ` ` `# Frequencies of subsequence ` ` ` `freq ` `=` `{} ` ` ` ` ` `# Loop to find the frequencies ` ` ` `# of subsequence of length 1 ` ` ` `for` `i ` `in` `s: ` ` ` `temp ` `=` `"" ` ` ` `temp ` `+` `=` `i ` ` ` `freq[temp] ` `=` `freq.get(temp, ` `0` `) ` `+` `1` ` ` ` ` `# Loop to find the frequencies ` ` ` `# subsequence of length 2 ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): ` ` ` `temp ` `=` `"" ` ` ` `temp ` `+` `=` `s[i] ` ` ` `temp ` `+` `=` `s[j] ` ` ` `freq[temp] ` `=` `freq.get(temp, ` `0` `) ` `+` `1` ` ` ` ` `answer ` `=` `-` `10` `*` `*` `9` ` ` ` ` `# Finding maximum frequency ` ` ` `for` `it ` `in` `freq: ` ` ` `answer ` `=` `max` `(answer, freq[it]) ` ` ` `return` `answer ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `s ` `=` `"xxxyy"` ` ` ` ` `print` `(maximumOccurrence(s)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** O(N^{2})

**Efficient Approach:** The idea is to use the dynamic programming paradigm to compute the frequency of the subsequences of the length 1 and 2 in the string. Below is the illustration of the steps:

- Compute the frequency of the characters of the string in a frequency array.
- For subsequences of the string of length 2, the DP state will be
dp[i][j] = Total number of times i

^{th}character occured before j^{th}character.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `int` `maximumOccurrence(string s) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Frequency for characters ` ` ` `int` `freq[26] = { 0 }; ` ` ` `int` `dp[26][26] = { 0 }; ` ` ` ` ` `// Loop to count the occurence ` ` ` `// of ith character before jth ` ` ` `// character in the given string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `c = (s[i] - ` `'a'` `); ` ` ` ` ` `for` `(` `int` `j = 0; j < 26; j++) ` ` ` `dp[j] += freq[j]; ` ` ` ` ` `// Increase the frequency ` ` ` `// of s[i] or c of string ` ` ` `freq++; ` ` ` `} ` ` ` ` ` `int` `answer = INT_MIN; ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 1 in given string ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `answer = max(answer, freq[i]); ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 2 in given string ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` `for` `(` `int` `j = 0; j < 26; j++) { ` ` ` `answer = max(answer, dp[i][j]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"xxxyy"` `; ` ` ` ` ` `cout << maximumOccurrence(s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` ` ` ` ` `class` `GFG{ ` ` ` `// Function to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `static` `int` `maximumOccurrence(String s) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Frequency for characters ` ` ` `int` `freq[] = ` `new` `int` `[` `26` `]; ` ` ` `int` `dp[][] = ` `new` `int` `[` `26` `][` `26` `]; ` ` ` ` ` `// Loop to count the occurence ` ` ` `// of ith character before jth ` ` ` `// character in the given String ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `int` `c = (s.charAt(i) - ` `'a'` `); ` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) ` ` ` `dp[j] += freq[j]; ` ` ` ` ` `// Increase the frequency ` ` ` `// of s[i] or c of String ` ` ` `freq++; ` ` ` `} ` ` ` ` ` `int` `answer = Integer.MIN_VALUE; ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 1 in given String ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `answer = Math.max(answer, freq[i]); ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 2 in given String ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) { ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) { ` ` ` `answer = Math.max(answer, dp[i][j]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String s = ` `"xxxyy"` `; ` ` ` ` ` `System.out.print(maximumOccurrence(s)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// maximum occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// occurence of the subsequence ` `// such that the indices of characters ` `// are in arithmetic progression ` `static` `int` `maximumOccurrence(` `string` `s) ` `{ ` ` ` `int` `n = s.Length; ` ` ` ` ` `// Frequency for characters ` ` ` `int` `[]freq = ` `new` `int` `[26]; ` ` ` `int` `[,]dp = ` `new` `int` `[26, 26]; ` ` ` ` ` `// Loop to count the occurence ` ` ` `// of ith character before jth ` ` ` `// character in the given String ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `int` `x = (s[i] - ` `'a'` `); ` ` ` ` ` `for` `(` `int` `j = 0; j < 26; j++) ` ` ` `dp[x, j] += freq[j]; ` ` ` ` ` `// Increase the frequency ` ` ` `// of s[i] or c of String ` ` ` `freq[x]++; ` ` ` `} ` ` ` ` ` `int` `answer = ` `int` `.MinValue; ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 1 in given String ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `answer = Math.Max(answer, freq[i]); ` ` ` ` ` `// Maximum occurence of subsequence ` ` ` `// of length 2 in given String ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < 26; j++) ` ` ` `{ ` ` ` `answer = Math.Max(answer, dp[i, j]); ` ` ` `} ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `string` `s = ` `"xxxyy"` `; ` ` ` ` ` `Console.Write(maximumOccurrence(s)); ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time complexity:** O(26 * N)

## Recommended Posts:

- Find indices of all occurrence of one string in other
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Largest subsequence such that all indices and all values are multiples individually
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Count number of indices such that s[i] = s[i+1] : Range queries
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Program to count occurrence of a given character in a string
- Count occurrence of a given character in a string using Stream API in Java
- Arrange a binary string to get maximum value within a range of indices
- Rearrange given string to maximize the occurrence of string t
- Concatenate the strings in an order which maximises the occurrence of subsequence "ab"
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Permutation of first N positive integers such that prime numbers are at prime indices
- Maximize the number of indices such that element is greater than element to its left
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum occurrence of prefix in the Array
- Count the occurrence of digit K in a given number N using Recursion

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.