Javatpoint Logo
Javatpoint Logo

Longest Common Subsequence in Python

A subsequence is a sequence that can be created by removing part or all of the items from another sequence while maintaining the order of the remaining components. The longest subsequence that appears in all of them is the longest common subsequence of two or more strings.

Here is a Python implementation of finding the longest common subsequence among two given strings using dynamic programming:

Output:

AC

Explanation:

The lcs function computes the longest common subsequence between two strings of length m and n, respectively. It uses a dynamic programming approach to fill a table of size (m+1) x (n+1). As a result, this function has an O(mn) time complexity and an O(mn) space complexity.

The lcs_multiple function computes the longest common subsequence among multiple strings. It does this by repeatedly calling the lcs function on pairs of strings, and then updating the result with the new longest common subsequence. For k strings, this function requires k-1 calls to the lcs function, each with a pair of strings of length m and n. As a result, this function has an O((k-1)mn) time complexity and an O(m*n) space complexity.

Overall, the time complexity of the algorithm is dominated by the lcs_multiple function, which is O((k-1)mn). However, in practice, the actual time complexity will depend on the specific strings being compared and the size of their longest common subsequence. Large strings may be problematic due to the algorithm's O(m*n) space complexity.

Using Divide and Conquer Algorithm

Another approach is the divide-and-conquer algorithm, which is a recursive algorithm that divides the set of input strings into smaller subsets and computes the longest common subsequence of each subset.

  • Divide the set of input strings into two subsets.
  • Recursively compute the longest common subsequence of each subset.
  • Merge the two longest common subsequences to obtain the longest common subsequence of the entire set.

Code:

Output:

AC

Explanation:

The lcs_multiple function first checks if there are 0 or 1 strings in the input list and returns an empty string or the single string if that is the case. If there are two strings in the list, it simply computes their longest common subsequence using the lcs function. If there are more than two strings, it divides the list into two halves, recursively computes the longest common subsequence of each half, and then merges the two longest common subsequences to obtain the longest common subsequence of the entire set.

The time complexity of the divide-and-conquer algorithm is O(k * n * log k), where k is the number of input strings and n is the length of the longest input string. The space complexity of the algorithm is also O(k * n * log k).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA