Stage 1 · Code
Dynamic Programming
Longest Common Subsequence
LCS length, reconstruction, and edit distance variant.
LCS Length
Given two strings, find length of longest common subsequence (not necessarily contiguous). Classic 2D DP: dp[i][j] = LCS of text1[0..i-1] and text2[0..j-1].
Reconstructing LCS
Edit Distance
Minimum operations (insert, delete, replace) to convert word1 to word2. Very similar to LCS: dp[i][j] = edit distance of word1[0..i-1] to word2[0..j-1].
Variants
- Shortest Common Supersequence: len(s1) + len(s2) - LCS(s1,s2)
- Delete to make equal: len(s1) + len(s2) - 2*LCS(s1,s2)
- Distinct subsequences: Count how many times s2 appears as subsequence in s1
- Longest Palindromic Subsequence: LCS(s, reverse(s))
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.