Count of maximum occurring subsequence using only those characters whose indices are in GP | Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Initialize 1 - D array and 2 - D dp array to 0 ; Iterate till the length of the given string ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Given string s ; Function call
def findMaxTimes ( S ) : NEW_LINE INDENT arr = [ 0 ] * 26 NEW_LINE dp = [ [ 0 for x in range ( 26 ) ] for y in range ( 26 ) ] NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT now = ord ( S [ i ] ) - ord ( ' a ' ) NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT dp [ j ] [ now ] += arr [ j ] NEW_LINE DEDENT arr [ now ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] ) NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT S = " ddee " NEW_LINE print ( findMaxTimes ( S ) ) NEW_LINE