The Common Thread (LCS)

HardAcc. 72.5%
+60 XP 30

2D State Transitions

Longest Common Subsequence (LCS) finds the longest sequence of characters that appear in the same relative order in both strings.

The Assignment

Your function receives text1 and text2.

  1. Create a 2D matrix dp[m+1][n+1].
  2. If text1[i] == text2[j], dp[i][j] = 1 + dp[i-1][j-1].
  3. Else, dp[i][j] = max(dp[i-1][j], dp[i][j-1]).
  4. Print the final LCS length.

01EXAMPLE 1

Inputtext1="abcde", text2="ace"
Output3

Explanation: Sequence is "ace".

Constraints

  • O(m * n) time and space.
AlgorithmsDynamic Programming
JavaScript
Loading...
1 Hidden

Input Arguments

text1"abcde"
text2"ace"

Expected Output

3

Click RUN to test your solution