Longest Common Anagram Subsequence | PHP 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
< ? php $ SIZE = 26 ; function longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) { global $ SIZE ; $ freq1 = array ( ) ; $ freq2 = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ freq1 [ $ i ] = 0 ; $ freq2 [ $ i ] = 0 ; } $ len = 0 ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) $ freq1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n2 ; $ i ++ ) $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ len += min ( $ freq1 [ $ i ] , $ freq2 [ $ i ] ) ; } return $ len ; } $ str1 = " abdacp " ; $ str2 = " ckamb " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; echo ( " Length ▁ = ▁ " . longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) ) ; ? >