Count of maximum occurring subsequence using only those characters whose indices are in GP | Java program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Iterate till the length of the given String ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given String s ; Function call
import java . util . * ; class GFG { static int findMaxTimes ( String S ) { int [ ] arr = new int [ 26 ] ; int [ ] [ ] dp = new int [ 26 ] [ 26 ] ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { int now = S . charAt ( i ) - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = Math . max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = Math . max ( ans , dp [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { String S = " ddee " ; System . out . print ( findMaxTimes ( S ) ) ; } }