Longest common anagram subsequence from N strings

Given N strings. Find the longest possible subsequence from each of these N strings such that they are anagram to each other. The task is to print the lexicographically largest subsequence among all the subsequences.

Examples:

Input: s[] = { geeks, esrka, efrsk }
Output: ske
First string has “eks”, Second string has “esk”, third string has “esk”. These three are anagrams. “ske” is lexoigrapically large.

Input: string s[] = { loop, lol, olive }
Output: ol



Approach :

  • Make a 2-D array of n*26 to store the frequency of each character in string.
  • After making frequency array, traverse in reverse direction for each digit and find the string which has the minimum characters of this type.
  • After complete reverse traversal, print the character that occurs the minimum number of times since it gives the lexicographically largest string.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find longest
// possible subsequence anagram
// of N strings.
class GFG
final int MAX_CHAR = 26;
  
// function to store frequency 
// of each character in each 
// string
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']++;     
    }
}
  
// function to Find longest 
// possible sequence of N
// strings which is anagram 
// to each other
static void LongestSequence(int fre[][], 
                            int n)
{
    // to get lexicographical 
    // largest sequence.
    for (int i = 24; i >= 0; i--)
    {
  
        // find minimum of
        // that character
        int mi = fre[0][i];
        for (int j = 1; j < n; j++) 
            mi = Math.min(fre[j][i], mi);     
  
        // print that character
        // minimum number of times
        while (mi--!=0
            System.out.print((char)('a' + i));     
    }
}
  
// Driver code
public static void main(String args[])
{
  
    String s[] = { "loo", "lol", "olive" };
    int n = s.length;
  
    // to strore frequency of each
    // character in each string
    int fre[][] = new int[n][26] ;
  
    // to get frequency 
    // of each character
    frequency(fre, s, n);
  
    // function call
    LongestSequence(fre, n);
}
}
  
// This code is contributed
// by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// c# program to find longest 
// possible subsequence anagram 
// of N strings. 
using System;
  
class GFG
{
public readonly int MAX_CHAR = 26;
  
// function to store frequency 
// of each character in each 
// string 
public 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[j] - 'a']++;
        }
    }
}
  
// function to Find longest 
// possible sequence of N 
// strings which is anagram 
// to each other 
public static void LongestSequence(int[, ] fre, 
                                   int n)
{
    // to get lexicographical 
    // largest sequence. 
    for (int i = 24; i >= 0; i--)
    {
  
        // find minimum of 
        // that character 
        int mi = fre[0, i];
        for (int j = 1; j < n; j++)
        {
            mi = Math.Min(fre[j, i], mi);
        }
  
        // print that character 
        // minimum number of times 
        while (mi--!=0)
        {
            Console.Write((char)('a' + i));
        }
    }
}
  
// Driver code 
public static void Main(string[] args)
{
  
    string[] s = new string[] {"loo", "lol", "olive"};
    int n = s.Length;
  
    // to strore frequency of each 
    // character in each string 
    int[, ] fre = new int[n, 26];
  
    // to get frequency 
    // of each character 
    frequency(fre, s, n);
  
    // function call 
    LongestSequence(fre, n);
}
}
  
// This code is contributed by Shrikanth13

chevron_right


Output:

ol


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234, shrikanth13