# Maximize count of 3-length palindromic subsequences with each index part of a single subsequence

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: 2
Explanation:Possible palindromic subsequences of length 3 satisfying the conditions are “gkg” and “efe”. Therefore, the required output is 2.

Input: str = “geek”
Output: 1
Explanation: 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 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 ith index of the string S increment the count of the character freq[S[i] – ‘a’] by 1.
• Iterate over the range [0, 26]. For every ith index of the array 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 ` `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 = { 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``;`   `        ``// 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.`

## Javascript

 ``

Output:

`2`

Time Complexity: O(|S| + 26)
Auxiliary Space: O(26)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next