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 Code
using System ; class GFG { static int SIZE = 26 ; static int longCommomAnagramSubseq ( string str1 , string str2 , int n1 , int n2 ) { int [ ] freq1 = new int [ SIZE ] ; int [ ] freq2 = new int [ SIZE ] ; for ( int i = 0 ; i < SIZE ; i ++ ) { freq1 [ i ] = 0 ; freq2 [ i ] = 0 ; } 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 += Math . Min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } static void Main ( ) { string str1 = " abdacp " ; string str2 = " ckamb " ; int n1 = str1 . Length ; int n2 = str2 . Length ; Console . Write ( " Length ▁ = ▁ " + longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) ; } }