Given two sequences, print all the possible longest common subsequences present in them.

For example,

Input:  X = XMJYAUZ, Y = MZJAWXU
Output: MJAU
 
 
Input:  X = ABCBDAB, Y = BDCABA
Output: BCAB, BCBA, BDAB

Practice this problem

In the previous post, we have discussed how to find the length of the longest common subsequence. This post will discuss how to print the longest common subsequence itself.

 
Let X be XMJYAUZ and Y be MZJAWXU. The longest common subsequence between X and Y is MJAU. The table below shows the lengths of the longest common subsequences between prefixes of X and Y. The i'th row and j'th column show the LCS’s length of substring X[0…i-1] and Y[0…j-1].

Longest Common subsequence

 
The highlighted numbers show the path one should follow from the bottom-right to the top-left corner when reading an LCS. If the current characters in X and Y are equal (shown in bold), they are part of the LCS, and we move diagonally. If the current characters in X and Y are different, we go up or left, depending on which cell has a higher number. This corresponds to either taking the LCS between X[0…i-2], Y[0…j-1], or X[0…i-1], Y[0…j-2].

 
The following C++, Java, and Python implementation fills the lookup table in a bottom-up manner and then recursively finds LCS in a top-down manner using values in the lookup table.

C++


Download  Run Code

Output:

MJAU

Java


Download  Run Code

Output:

MJAU

Python


Download  Run Code

Output:

MJAU

How can we modify the code so it prints all possible LCS?

The above code doesn’t generate all the longest common subsequences of given sequences. It will always print one LCS. The problem with the above code is when the last character of X and Y are different, it discards either the last character of X or the last character of Y based on the top or left cell’s value to the current cell. But what if LCS is also possible with the discarded character?

The idea remains the same except if the top cell is equal to the left cell, consider both characters; otherwise, go in the direction of greater value. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

BCAB
BCBA
BDAB

Java


Download  Run Code

Output:

[BCAB, BCBA, BDAB]

Python


Download  Run Code

Output:

{‘BCBA’, ‘BDAB’, ‘BCAB’}

References: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem