Longest Common Anagram Subsequence | Python 3 implementation to find the length of the longest common anagram subsequence ; function to find the length of the longest common anagram subsequence ; List for storing frequencies of each character ; calculate frequency of each character of 'str1[] ; calculate frequency of each character of 'str2[] ; for each character add its minimum frequency out of the two strings in 'len ; required length ; Driver Code
SIZE = 26 NEW_LINE def longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) : NEW_LINE INDENT freq1 = [ 0 ] * SIZE NEW_LINE freq2 = [ 0 ] * SIZE NEW_LINE l = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( n1 ) : NEW_LINE INDENT freq1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( n2 ) : NEW_LINE INDENT freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( SIZE ) : NEW_LINE INDENT l += min ( freq1 [ i ] , freq2 [ i ] ) NEW_LINE DEDENT return l NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " abdacp " NEW_LINE str2 = " ckamb " NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE print ( " Length ▁ = ▁ " , longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) NEW_LINE DEDENT