Advertisement

Longest Common Subsequence - LeetCode 1143 Solution

Longest Common Subsequence - Complete Solution Guide

Longest Common Subsequence is LeetCode problem 1143, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3.

Problem Statement

Given two strings text1 and text2 , return the length of their longest common subsequence . If there is no common subsequence , return 0 . A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde" . A common subsequence of two strings is a subsequence that is common to both strings. Example 1: Input: text1 = "abcde", text2 =

Detailed Explanation

The problem asks us to find the length of the longest common subsequence between two given strings, `text1` and `text2`. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. A common subsequence is a subsequence that is common to both input strings. The input consists of two strings, `text1` and `text2`, composed of lowercase English characters. The output is an integer representing the length of their longest common subsequence. If there is no common subsequence, we return 0. The length of each input string is between 1 and 1000.

Solution Approach

The provided code implements a dynamic programming approach to solve the Longest Common Subsequence problem. It constructs a 2D array `dp` where `dp[i][j]` stores the length of the LCS of `text1[0...i-1]` and `text2[0...j-1]`. The array is built iteratively, starting from the base case of empty prefixes (`dp[0][j]` and `dp[i][0]` are initialized to 0). If `text1[i-1]` and `text2[j-1]` are equal, it means that we can extend the LCS by one character, so `dp[i][j]` is set to `dp[i-1][j-1] + 1`. If they are not equal, we take the maximum of the LCS lengths obtained by either excluding the last character of `text1` or excluding the last character of `text2`. Finally, the length of the LCS of the entire strings `text1` and `text2` is stored in `dp[m][n]`, where `m` and `n` are the lengths of `text1` and `text2`, respectively.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D array `dp` of size `(m+1) x (n+1)` with all elements set to 0, where `m` is the length of `text1` and `n` is the length of `text2`. The extra row and column are used to handle the base case of empty prefixes.
  2. Step 2: Iterate through the `dp` array starting from index `(1, 1)` to `(m, n)`. For each cell `dp[i][j]`, compare the characters `text1[i-1]` and `text2[j-1]`.
  3. Step 3: If `text1[i-1]` is equal to `text2[j-1]`, it means that the characters contribute to the LCS. Set `dp[i][j]` to `dp[i-1][j-1] + 1`.
  4. Step 4: If `text1[i-1]` is not equal to `text2[j-1]`, it means that we need to consider the LCS without including either `text1[i-1]` or `text2[j-1]`. Set `dp[i][j]` to `max(dp[i-1][j], dp[i][j-1])`.
  5. Step 5: After iterating through the entire `dp` array, the value `dp[m][n]` will contain the length of the LCS of `text1` and `text2`. Return this value.

Key Insights

  • Insight 1: Recognizing that the problem can be solved using dynamic programming by building a table of solutions to subproblems.
  • Insight 2: Understanding that the length of the LCS at `text1[i]` and `text2[j]` depends on whether `text1[i]` and `text2[j]` are equal or not. If they are equal, the LCS length is 1 plus the LCS length of `text1[0...i-1]` and `text2[0...j-1]`. If they are not equal, the LCS length is the maximum of the LCS length of `text1[0...i-1]` and `text2[0...j]` and the LCS length of `text1[0...i]` and `text2[0...j-1]`.
  • Insight 3: Using a 2D array to store the lengths of LCS for all possible prefixes of the two strings avoids redundant calculations.

Complexity Analysis

Time Complexity: O(m*n), where m and n are the lengths of text1 and text2, respectively. The code iterates through the entire dp array of size (m+1) x (n+1) once.

Space Complexity: O(m*n), where m and n are the lengths of text1 and text2, respectively. The code uses a 2D array `dp` of size (m+1) x (n+1) to store the intermediate results.

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Accolite, Arista Networks, BP, ByteDance, Citadel, DoorDash, Nutanix, Optum, tcs.