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++
#include <bits/stdc++.h>
using namespace std;
int maxNumPalindrome(string S)
{
int i = 0;
int freq[26] = { 0 };
int freqPair = 0;
int len = S.length() / 3;
while (i < S.length()) {
freq[S[i] - 'a' ]++;
i++;
}
for (i = 0; i < 26; i++) {
freqPair += (freq[i] / 2);
}
return min(freqPair, len);
}
int main()
{
string S = "geeksforg" ;
cout << maxNumPalindrome(S) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
String S = "geeksforg" ;
System.out.println(maxNumPalindrome(S));
}
static int maxNumPalindrome(String S)
{
int i = 0 ;
int [] freq = new int [ 26 ];
int freqPair = 0 ;
int len = S.length() / 3 ;
while (i < S.length()) {
freq[S.charAt(i) - 'a' ]++;
i++;
}
for (i = 0 ; i < 26 ; i++) {
freqPair += (freq[i] / 2 );
}
return Math.min(freqPair, len);
}
}
|
Python3
def maxNumPalindrome(S):
i = 0
freq = [ 0 ] * 26
freqPair = 0
ln = len (S) / / 3
while (i < len (S)):
freq[ ord (S[i]) - ord ( 'a' )] + = 1
i + = 1
for i in range ( 26 ):
freqPair + = (freq[i] / / 2 )
return min (freqPair, ln)
if __name__ = = '__main__' :
S = "geeksforg"
print (maxNumPalindrome(S))
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
string S = "geeksforg" ;
Console.WriteLine(maxNumPalindrome(S));
}
static int maxNumPalindrome( string S)
{
int i = 0;
int [] freq = new int [26];
int freqPair = 0;
int len = S.Length / 3;
while (i < S.Length)
{
freq[S[i] - 'a' ]++;
i++;
}
for (i = 0; i < 26; i++)
{
freqPair += (freq[i] / 2);
}
return Math.Min(freqPair, len);
}
}
|
Javascript
<script>
function maxNumPalindrome(S)
{
let i = 0;
let freq = new Array(26).fill(0);
let freqPair = 0;
let len = (S.length / 3);
while (i < S.length)
{
freq[S[i].charCodeAt(0) -
'a' .charCodeAt(0)]++;
i++;
}
for (i = 0; i < 26; i++)
{
freqPair += Math.floor(freq[i] / 2);
}
return Math.min(freqPair, len);
}
let S = "geeksforg" ;
document.write(maxNumPalindrome(S) + "<br>" );
</script>
|
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!
Last Updated :
02 Dec, 2022
Like Article
Save Article