Length of longest prefix anagram which are common in given two strings | Javascript 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 string str1 ; Store the count of characters in string 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 str2 [ i ] in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally print the ans ; Driver code ; Function Call
let SIZE = 26 ; function longHelper ( freq1 , freq2 ) { for ( let i = 0 ; i < SIZE ; ++ i ) { if ( freq1 [ i ] != freq2 [ i ] ) { return false ; } } return true ; } function longCommomPrefixAnagram ( s1 , s2 , n1 , n2 ) { let freq1 = new Array ( 26 ) ; freq1 . fill ( 0 ) ; let freq2 = new Array ( 26 ) ; freq2 . fill ( 0 ) ; let ans = 0 ; let mini_len = Math . min ( n1 , n2 ) ; for ( let i = 0 ; i < mini_len ; ++ i ) { freq1 [ s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; freq2 [ s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; if ( longHelper ( freq1 , freq2 ) ) { ans = i + 1 ; } } document . write ( ans ) ; } let str1 = " " ; let str2 = " " ; let N = str1 . length ; let M = str2 . length ; longCommomPrefixAnagram ( str1 , str2 , N , M ) ;