Frequency of maximum occurring subsequence in given string | Python3 program for the above approach ; Function to find the frequency ; freq stores frequency of each english lowercase character ; dp [ i ] [ j ] stores the count of subsequence with ' a ' + i and ' a ' + j character ; Increment the count of subsequence j and s [ i ] ; Update the frequency array ; For 1 length subsequence ; For 2 length subsequence ; Return the final result ; Given string str ; Function call
import numpy NEW_LINE def findCount ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE dp = [ [ 0 ] * 26 ] * 26 NEW_LINE freq = numpy . zeros ( 26 ) NEW_LINE dp = numpy . zeros ( [ 26 , 26 ] ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT dp [ j ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += freq [ j ] NEW_LINE DEDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT ans = max ( freq [ i ] , ans ) NEW_LINE DEDENT for i in range ( 0 , 26 ) : NEW_LINE INDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT ans = max ( dp [ i ] [ j ] , ans ) NEW_LINE DEDENT DEDENT return int ( ans ) NEW_LINE DEDENT str = " acbab " NEW_LINE print ( findCount ( str ) ) NEW_LINE