Longest common anagram subsequence from N strings | Function to store frequency of each character in each string ; Function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; To get frequency of each character ; Function call
def frequency ( fre , s , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT string = s [ i ] NEW_LINE for j in range ( 0 , len ( string ) ) : NEW_LINE INDENT fre [ i ] [ ord ( string [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT def LongestSequence ( fre , n ) : NEW_LINE INDENT for i in range ( MAX_CHAR - 1 , - 1 , - 1 ) : NEW_LINE INDENT mi = fre [ 0 ] [ i ] NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT mi = min ( fre [ j ] [ i ] , mi ) NEW_LINE DEDENT while mi : NEW_LINE INDENT print ( chr ( ord ( ' a ' ) + i ) , end = " " ) NEW_LINE mi -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ " loo " , " lol " , " olive " ] NEW_LINE n = len ( s ) NEW_LINE MAX_CHAR = 26 NEW_LINE fre = [ [ 0 for i in range ( 26 ) ] for j in range ( n ) ] NEW_LINE frequency ( fre , s , n ) NEW_LINE LongestSequence ( fre , n ) NEW_LINE DEDENT