Longest Common Anagram Subsequence | Javascript implementation to find the length ( ) of the longest common anagram subsequence ; function to find the length ( ) of the longest common anagram subsequence ; hash tables 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
let SIZE = 26 ; function longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) { let freq1 = new Array ( SIZE ) ; let freq2 = new Array ( SIZE ) ; for ( let i = 0 ; i < SIZE ; i ++ ) { freq1 [ i ] = 0 ; freq2 [ i ] = 0 ; } let len = 0 ; for ( let i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < SIZE ; i ++ ) len += Math . min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } let str1 = " " ; let str2 = " " ; let n1 = str1 . length ; let n2 = str2 . length ; document . write ( " " + longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) ;