Longest Common Subsequence — 2D DP Preview
LCS of two strings: dp[i][j] = LCS of first i chars of s1 and first j chars of s2. Full 2D DP treatment in the 2D DP section.
webcoderspeed.com
6 articles
LCS of two strings: dp[i][j] = LCS of first i chars of s1 and first j chars of s2. Full 2D DP treatment in the 2D DP section.
Master 2D DP: LCS, Edit Distance, grid path counting, matrix chain, interval DP, and stock patterns with 5-language templates.
Classic LCS: dp[i][j] = LCS length of s1[:i] and s2[:j]. If chars match add 1 to diagonal; else take max of skip either string.
Find shortest string containing both strings as subsequences. Length = len(s1)+len(s2)-LCS. Reconstruct by tracing DP table.
Find the longest common substring between two strings. DP approach O(nm), binary search + rolling hash O((n+m) log(n+m)).
Core string DP patterns: edit distance (Wagner-Fischer), longest common subsequence, interleaving strings, and regular expression matching.