Maximize count of 3 | Java program to implement the above approach ; Driver Code ; Function to count the maximum number of palindrome subsequences of length 3 considering the same index only once ; Index of the string S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value
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 ) ; } }