Longest Common Anagram Subsequence | C ++ 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 program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE int longCommomAnagramSubseq ( char str1 [ ] , char str2 [ ] , int n1 , int n2 ) { int freq1 [ SIZE ] , freq2 [ SIZE ] ; memset ( freq1 , 0 , sizeof ( freq1 ) ) ; memset ( freq2 , 0 , sizeof ( freq2 ) ) ; int len = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < SIZE ; i ++ ) len += min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } int main ( ) { char str1 [ ] = " abdacp " ; char str2 [ ] = " ckamb " ; int n1 = strlen ( str1 ) ; int n2 = strlen ( str2 ) ; cout << " Length ▁ = ▁ " << longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ; return 0 ; }