Longest Common Subsequence - JS Solution (2D dp array)

JS
Editorial

Solution explanation

Solution explanation

Approach

Approach (LCS via Dynamic Programming)

We use a Dynamic Programming (DP) approach to compute the length of the Longest Common Subsequence (LCS) between two strings text1 (length m) and text2 (length n). The idea is to build a 2D DP table dp where dp[i][j] represents the length of the LCS between the first i characters of text1 (text1[0..i-1]) and the first j characters of text2 (text2[0..j-1]).

Initialization

  • Create a DP table of size (m + 1) × (n + 1) initialized with 0.
  • The first row (i = 0) and first column (j = 0) remain 0, since an empty prefix of either string means there can be no common subsequence — i.e., the LCS length is 0 when at least one of the strings is empty.

Transition & Meaning of Neighboring Cells

To compute dp[i][j], we compare the current characters text1[i-1] and text2[j-1] and refer to three neighboring cells:

  • dp[i-1][j-1] (diagonal) — LCS of prefixes excluding the current characters text1[i-1] and text2[j-1].
  • dp[i-1][j] (up) — best LCS if we exclude the current character of text1 (text1[i-1]) while keeping text2[0..j-1].
  • dp[i][j-1] (left) — best LCS if we exclude the current character of text2 (text2[j-1]) while keeping text1[0..i-1].

Transition Rules

Case 1 — Match: If text1[i-1] === text2[j-1], the current characters match. This means we can extend the LCS found so far by one character:

• Use the diagonal value dp[i-1][j-1] because it counts the LCS excluding these matching characters.
• Add +1 to include the current matching character:
dp[i][j] = dp[i-1][j-1] + 1.

Case 2 — No Match: If text1[i-1] !== text2[j-1], the current characters differ, so we cannot include both. Instead, we take the better of two possibilities:

• Consider the up cell dp[i-1][j] — best LCS if we exclude the current character of text1 (text1[i-1]).
• Consider the left cell dp[i][j-1] — best LCS if we exclude the current character of text2 (text2[j-1]).
• The optimal choice is the larger of the two — since the current characters don’t match, we keep the longer subsequence excluding the current character from either string:
dp[i][j] = max(dp[i-1][j], dp[i][j-1]).

Iteration Order & Visualization Cues

  • Fill the DP table row by row for i = 1..m, and within each row, process each column for j = 1..n. (We start from 1 for both since i = 0 and j = 0 represent empty prefixes, already initialized to 0.)
  • While computing a cell, it can be visualized as in progress; once finalized, mark it as done.

Result

After filling the entire table, the length of the Longest Common Subsequence is given by the bottom-right cell: dp[m][n].

Time Complexity

O(m × n), where m = text1.length and n = text2.length.

We fill a DP table of size (m + 1) × (n + 1), computing each cell in constant time O(1). Therefore, the total running time grows linearly with the number of cells, resulting in O(m × n).

Space Complexity

No content provided

Visucodize editorial solution titled JS Solution (2D dp array) for LeetCode's Longest Common Subsequence coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/longest-common-subsequence.