Longest Common Subsequence
Longest subsequence (order kept, gaps allowed) present in both strings text1 and text2.
Intuition
Compare the strings from the front, one prefix pair at a time. dp[i][j] = LCS of text1[:i] and text2[:j]. If the two current characters match, they surely end a common subsequence: 1 + the diagonal. If not, one of them is useless — drop either and take the better result. Every subproblem is a smaller prefix pair, so the table fills row by row.
Approach
Define the state
dp[i][j] = LCS length of the first i chars of text1 and first j chars of text2. dp[0][] = dp[][0] = 0 (empty prefix shares nothing).
The two-case transition
Match (text1[i-1]==text2[j-1]): dp[i][j] = dp[i-1][j-1] + 1. Mismatch: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) — best of skipping one char from either string.
Answer & backbone
dp[m][n] is the length. This exact table is the backbone of edit distance, diff tools, and many string DPs — worth knowing cold.
Solution & live demo
Edge cases
Table stays 0 everywhere — answer 0.
Row/column 0 base case handles it.
Diagonal fills 1,2,3,… — answer is the full length.