Given a string, **S**, the task is to find the maximum number of distinct indexed palindromic subsequences of length 3 possible from the given string.

**Examples:**

Input: str = “geekforg”Output: 2Explanation:Possible palindromic subsequences of length 3 satisfying the conditions are “gkg” and “efe”. Therefore, the required output is 2.

Input:str = “geek”Output:1Explanation:Possible palindromic subsequences of length 3 satisfying the conditions are “ege” .

**Approach: **The idea is to count the frequency of every character of the string **S**, and count the frequency pairs such that pairs are of the same characters and count the number of subsequences of length **3** by dividing the string **S** by** 3**. Finally, print the minimum of frequency pairs as the number of subsequences. Follow the steps below to solve the problem:

- Initialize an array, say
**freq[]**, to store the frequencies of every character of the string**S**. - Initialize a variable, say
**freqPair**, to store the frequency pairs having pairs are of the same characters. - Initialize a variable, say
**len**, to store the number of subsequences of length**3**of the string**S**. - Iterate over the range
**[0, str.length() – 1]**. For every**i**index of the string^{th}**S**increment the count of the character**freq[S[i] – ‘a’]**by**1**. - Iterate over the range
**[0, 26]**. For every**i**index of the array^{th}**freq[],**count the frequency pairs by dividing the array element by**2**. - Finally, print the value of the minimum of
**freqPair**and**len**.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the maximum number` `// oaf palindrome subsequences of length 3` `// considering the same index only once` `int` `maxNumPalindrome(string S)` `{` ` ` `// Index of the string S` ` ` `int` `i = 0;` ` ` `// Stores the frequency of` ` ` `// every character` ` ` `int` `freq[26] = { 0 };` ` ` `// Stores the pair of frequency` ` ` `// containing same characters` ` ` `int` `freqPair = 0;` ` ` `// Number of subsequences` ` ` `// having length 3` ` ` `int` `len = S.length() / 3;` ` ` `// Counts the frequency` ` ` `while` `(i < S.length()) {` ` ` `freq[S[i] - ` `'a'` `]++;` ` ` `i++;` ` ` `}` ` ` `// Counts the pair of frequency` ` ` `for` `(i = 0; i < 26; i++) {` ` ` `freqPair += (freq[i] / 2);` ` ` `}` ` ` `// Returns the minimum value` ` ` `return` `min(freqPair, len);` `}` `// Driver Code` `int` `main()` `{` ` ` `string S = ` `"geeksforg"` `;` ` ` `cout << maxNumPalindrome(S) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG {` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `String S = ` `"geeksforg"` `;` ` ` `System.out.println(maxNumPalindrome(S));` ` ` `}` ` ` `// Function to count the maximum number` ` ` `// of palindrome subsequences of length 3` ` ` `// considering the same index only once` ` ` `static` `int` `maxNumPalindrome(String S)` ` ` `{` ` ` `// Index of the string S` ` ` `int` `i = ` `0` `;` ` ` `// Stores the frequency of` ` ` `// every character` ` ` `int` `[] freq = ` `new` `int` `[` `26` `];` ` ` `// Stores the pair of frequency` ` ` `// containing same characters` ` ` `int` `freqPair = ` `0` `;` ` ` `// Number of subsequences` ` ` `// having length 3` ` ` `int` `len = S.length() / ` `3` `;` ` ` `// Counts the frequency` ` ` `while` `(i < S.length()) {` ` ` `freq[S.charAt(i) - ` `'a'` `]++;` ` ` `i++;` ` ` `}` ` ` `// Counts the pair of frequency` ` ` `for` `(i = ` `0` `; i < ` `26` `; i++) {` ` ` `freqPair += (freq[i] / ` `2` `);` ` ` `}` ` ` `// Returns the minimum value` ` ` `return` `Math.min(freqPair, len);` ` ` `}` `}` |

## Python3

`# Python3 program to implement` `# the above approach` `# Function to count the maximum number` `# of palindrome subsequences of length 3` `# considering the same index only once` `def` `maxNumPalindrome(S):` ` ` ` ` `# Index of the S` ` ` `i ` `=` `0` ` ` `# Stores the frequency of` ` ` `# every character` ` ` `freq ` `=` `[` `0` `] ` `*` `26` ` ` `# Stores the pair of frequency` ` ` `# containing same characters` ` ` `freqPair ` `=` `0` ` ` `# Number of subsequences` ` ` `# having length 3` ` ` `ln ` `=` `len` `(S) ` `/` `/` `3` ` ` `# Counts the frequency` ` ` `while` `(i < ` `len` `(S)):` ` ` `freq[` `ord` `(S[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` `i ` `+` `=` `1` ` ` `# Counts the pair of frequency` ` ` `for` `i ` `in` `range` `(` `26` `):` ` ` `freqPair ` `+` `=` `(freq[i] ` `/` `/` `2` `)` ` ` `# Returns the minimum value` ` ` `return` `min` `(freqPair, ln)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `S ` `=` `"geeksforg"` ` ` `print` `(maxNumPalindrome(S))` `# This code is contributed by mohit kumar 29` |

## C#

`// C# program to implement` `// the above approach ` `using` `System;` `class` `GFG` `{` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String[] args)` ` ` `{` ` ` `string` `S = ` `"geeksforg"` `;` ` ` `Console.WriteLine(maxNumPalindrome(S));` ` ` `}` ` ` `// Function to count the maximum number` ` ` `// of palindrome subsequences of length 3` ` ` `// considering the same index only once` ` ` `static` `int` `maxNumPalindrome(` `string` `S)` ` ` `{` ` ` `// Index of the string S` ` ` `int` `i = 0;` ` ` `// Stores the frequency of` ` ` `// every character` ` ` `int` `[] freq = ` `new` `int` `[26];` ` ` `// Stores the pair of frequency` ` ` `// containing same characters` ` ` `int` `freqPair = 0;` ` ` `// Number of subsequences` ` ` `// having length 3` ` ` `int` `len = S.Length / 3;` ` ` `// Counts the frequency` ` ` `while` `(i < S.Length)` ` ` `{` ` ` `freq[S[i] - ` `'a'` `]++;` ` ` `i++;` ` ` `}` ` ` `// Counts the pair of frequency` ` ` `for` `(i = 0; i < 26; i++)` ` ` `{` ` ` `freqPair += (freq[i] / 2);` ` ` `}` ` ` `// Returns the minimum value` ` ` `return` `Math.Min(freqPair, len);` ` ` `}` `}` `// This code is contributed by susmitakundugoaldanga.` |

**Output:**

2

**Time Complexity:***O(|S| + 26)***Auxiliary Space:***O(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.