Longest common anagram subsequence from N strings | C ++ program to find longest possible subsequence anagram of N strings . ; function to store frequency of each character in each string ; function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; to get frequency of each character ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; void frequency ( int fre [ ] [ MAX_CHAR ] , string s [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { string str = s [ i ] ; for ( int j = 0 ; j < str . size ( ) ; j ++ ) fre [ i ] [ str [ j ] - ' a ' ] ++ ; } } void LongestSequence ( int fre [ ] [ MAX_CHAR ] , int n ) { for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { int mi = fre [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) mi = min ( fre [ j ] [ i ] , mi ) ; while ( mi -- ) cout << ( char ) ( ' a ' + i ) ; } } int main ( ) { string s [ ] = { " loo " , " lol " , " olive " } ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; int fre [ n ] [ 26 ] = { 0 } ; frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ; return 0 ; }