Length of longest prefix anagram which are common in given two strings | Python3 program for the above approach ; Function to check if two arrays are identical or not ; Iterate over the range [ 0 , SIZE ] ; If frequency any character is not same in both the strings ; Otherwise ; Function to find the maximum length of the required string ; Store the count of characters in str1 ; Store the count of characters in str2 ; Stores the maximum length ; Minimum length of str1 and str2 ; Increment the count of characters of str1 [ i ] in freq1 [ ] by one ; Increment the count of characters of stord ( r2 [ i ] ) in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally prthe ans ; Driver Code ; Function Call
SIZE = 26 NEW_LINE def longHelper ( freq1 , freq2 ) : NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq1 [ i ] != freq2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def longCommomPrefixAnagram ( s1 , s2 , n1 , n2 ) : NEW_LINE INDENT freq1 = [ 0 ] * 26 NEW_LINE freq2 = [ 0 ] * 26 NEW_LINE ans = 0 NEW_LINE mini_len = min ( n1 , n2 ) NEW_LINE for i in range ( mini_len ) : NEW_LINE INDENT freq1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE freq2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( longHelper ( freq1 , freq2 ) ) : NEW_LINE INDENT ans = i + 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abaabcdezzwer " NEW_LINE str2 = " caaabbttyh " NEW_LINE N = len ( str1 ) NEW_LINE M = len ( str2 ) NEW_LINE longCommomPrefixAnagram ( str1 , str2 , N , M ) NEW_LINE DEDENT