The Longest Common Subsequence (LCS) problem is finding the longest subsequence present in given two sequences in the same order, i.e., find the longest sequence which can be obtained from the first original sequence by deleting some items and from the second original sequence by deleting other items.

The problem differs from the problem of finding the longest common substring. Unlike substrings, subsequences are not required to occupy consecutive positions within the original string.

 
Prerequisite:

Longest Common Subsequence Problem

 
In the previous post, we have discussed the longest common subsequence of two sequences. In this post, we will extend the solution to three sequences. The proposed solution can be easily generalized to handle more than three sequences as well.

For example, consider the three following sequences X, Y, and Z:

X: ABCBDAB
Y: BDCABA
Z: BADACB
 
The length of the LCS is 4, and the LCS is BDAB.

Practice this problem

We have seen in the previous post, LCS problem has an optimal substructure, which means that the main problem can be broken down into smaller, simple “subproblems”, which can further be divided into yet simpler subproblems.

 
1. Let’s consider given sequences X, Y, and Z of length i, j, and k that both end in the same element. To find their LCS, shorten each sequence by removing the last element, find the LCS of the shortened sequences, and to that LCS append the removed element. So, if X[i] = Y[j] = Z[k], we can say that,

LCS(X[1…i], Y[1…j], Z[1…k]) = LCS(X[1…i-1], Y[1…j-1], Z[1…k-1]) + X[i]

2. Now, suppose that the given sequences do not end in the same symbol. Then the LCS of X, Y, and Z is the longest of the given sequences.

LCS(X[1…i-1], Y[1…j], Z[1…k]),
LCS(X[1…i], Y[1…j-1], Z[1…k]), and
LCS(X[1…i], Y[1…j], Z[1…k-1])

The following solution in C++, Java, and Python finds the length of LCS of sequences X[0…m-1], Y[0…n-1], and Z[0…o-1] recursively by using the optimal substructure property of LCS problem:

C++


Download  Run Code

Output:

The length of the LCS is 4

Java


Download  Run Code

Output:

The length of the LCS is 4

Python


Download  Run Code

Output:

The length of the LCS is 4

The worst-case time complexity of the above solution is O(3(m+n+o)), where m, n, and o is the length of X, Y, and Z, respectively. The worst case happens when there is no common subsequence present in X, Y, Z (i.e., LCS length is 0), and each recursive call ends up in three recursive calls.

 
The LCS problem exhibits overlapping subproblems. A problem is said to have overlapping subproblems if the recursive algorithm for the problem solves the same subproblem repeatedly rather than generating new subproblems. We know that problems having optimal substructure and overlapping subproblems can be solved by dynamic programming, in which subproblem solutions are memoized rather than computed repeatedly. This method can be implemented as follows in C++, Java, and Python:

 
Following is the memoized implementation in C++, Java, and Python:

C++


Download  Run Code

Output:

The length of the LCS is 4

Java


Download  Run Code

Output:

The length of the LCS is 4

Python


Download  Run Code

Output:

The length of the LCS is 4

The time complexity of the above top-down solution is O(m.n.o) and requires O(m.n.o) extra space, where m, n, and o is the length of X, Y, and Z, respectively.

 
The above memoized version follows the top-down approach since we first break the problem into subproblems and then calculate and store values. We can also solve this problem in a bottom-up manner. In the bottom-up approach, calculate the smaller values of LCS(i, j, k) first, then build larger values from them.

               | 0                               if i == 0 or j == 0 or k == 0
LCS[i][j][k] = | LCS[i-1][j-1][k-1] + 1          if X[i-1] == Y[j-1] == Z[k-1]
               | longest(LCS[i-1][j][k],         otherwise
                       LCS[i][j-1][k],
                       LCS[i][j][k-1])

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The length of the LCS is 4

Java


Download  Run Code

Output:

The length of the LCS is 4

Python


Download  Run Code

Output:

The length of the LCS is 4

The time complexity of the above bottom-up solution is O(m.n.o) and requires O(m.n.o) extra space, where m, n, and o is the length of X, Y, and Z, respectively.

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