Longest Common Subsequence medium

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 = "ace" Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.

Example 2

Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3.

Example 3

Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no common subsequence.

Steps

The problem can be efficiently solved using dynamic programming. We'll create a 2D array dp where dp[i][j] represents the length of the longest common subsequence of text1.substring(0, i) and text2.substring(0, j).

  1. Initialization: Create a dp array of size (m+1) x (n+1), where m and n are the lengths of text1 and text2 respectively. Initialize the first row and first column to 0.

  2. Iteration: Iterate through the dp array. For each cell dp[i][j]:

    • If text1[i-1] equals text2[j-1], it means we found a common character. In this case, dp[i][j] is dp[i-1][j-1] + 1.
    • Otherwise, dp[i][j] is the maximum of dp[i-1][j] and dp[i][j-1], representing either taking the LCS from the previous row or the previous column.
  3. Result: The value at dp[m][n] will be the length of the longest common subsequence of text1 and text2.

Explanation

The dynamic programming approach works by building up the solution from smaller subproblems. Each cell dp[i][j] considers the longest common subsequence found so far. If the characters at text1[i-1] and text2[j-1] match, we extend the LCS by 1. Otherwise, we take the maximum LCS from the subproblems ending at either text1[i-1] or text2[j-1]. This avoids redundant calculations by storing and reusing the results of subproblems.

Code

function longestCommonSubsequence(text1: string, text2: string): number {
    const m = text1.length;
    const n = text2.length;

    // Create a DP array and initialize it
    const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));

    // Iterate through the DP array
    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            if (text1[i - 1] === text2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    // The bottom-right cell contains the length of the LCS
    return dp[m][n];
};

Complexity

  • Time Complexity: O(m*n), where m and n are the lengths of the input strings. This is due to the nested loops iterating through the DP array.
  • Space Complexity: O(m*n), as we use a 2D array to store the DP table. This could be optimized to O(min(m,n)) using space optimization techniques (only storing the previous row or column).