Longest common anagram subsequence from N strings | Java 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
class GFG { final int MAX_CHAR = 26 ; static void frequency ( int fre [ ] [ ] , String s [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { String str = s [ i ] ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) fre [ i ] [ str . charAt ( j ) - ' a ' ] ++ ; } } static void LongestSequence ( int fre [ ] [ ] , int n ) { for ( int i = 24 ; i >= 0 ; i -- ) { int mi = fre [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) mi = Math . min ( fre [ j ] [ i ] , mi ) ; while ( mi -- != 0 ) System . out . print ( ( char ) ( ' a ' + i ) ) ; } } public static void main ( String args [ ] ) { String s [ ] = { " loo " , " lol " , " olive " } ; int n = s . length ; int fre [ ] [ ] = new int [ n ] [ 26 ] ; frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ; } }