Given two strings text1 and text2, return the length of their longest common subsequence — characters appearing left-to-right but not necessarily contiguous. e.g. 'abcde' and 'ace' → 3 ('ace').
Recognising a two-sequence problem as a 2D grid DP, and defining the state as the LCS of the two prefixes.
When a DP ranges over two sequences, the state is almost always a 2D table indexed by a prefix of each: dp[i][j] = LCS length of text1[0..i) and text2[0..j). The transition reads off the last characters — if text1[i-1] === text2[j-1] they extend a common subsequence, so dp[i][j] = dp[i-1][j-1] + 1; otherwise the best comes from dropping one character, dp[i][j] = max(dp[i-1][j], dp[i][j-1]). Row 0 and column 0 are the empty-prefix base cases (0). This same grid powers edit distance, longest common substring (reset to 0 on mismatch), and diff tools. Recognition signal: two strings/arrays plus 'subsequence' or 'transform A into B' → 2D DP over prefixes. Space drops to O(min(m,n)) by keeping only the previous row.
Two sequences and you need an optimal alignment/subsequence/edit measure → dp[i][j] over the two prefixes, transition on whether the last elements match.
Time O(m·n) · Space O(m·n), reducible to O(min(m,n))