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.
- Create a 2D matrix
dp[m+1][n+1]. - If
text1[i] == text2[j],dp[i][j] = 1 + dp[i-1][j-1]. - Else,
dp[i][j] = max(dp[i-1][j], dp[i][j-1]). - Print the final LCS length.
01EXAMPLE 1
Input
text1="abcde", text2="ace"Output
3Explanation: Sequence is "ace".
Constraints
- O(m * n) time and space.
AlgorithmsDynamic Programming
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
text1"abcde"
text2"ace"
Expected Output
3
Click RUN to test your solution