"When a problem juggles two things at once — two strings, or items-plus-a-budget — the state needs two numbers, and the DP table becomes a grid. These 2D DPs are the workhorses behind spell-check, DNA alignment, and file diffs."
Two Dimensions of State
A 2D DP has a subproblem identified by a pair of numbers, so the table is a grid. The most common shape: two sequences, where dp[i][j] answers a question about 'the first i of sequence A and the first j of sequence B.' Another: dp[i][w] = 'using the first i items within budget w.' Each cell is computed from its neighbors — usually the cell above, the cell to the left, and/or the diagonal — and you fill the grid in an order (typically row by row) that guarantees those neighbors are ready before you need them.
Edit Distance: the Canonical 2D DP
The edit distance (Levenshtein distance) between two strings is the minimum number of single-character insertions, deletions, or substitutions to turn one into the other. Define dp[i][j] = the edit distance between the first i characters of A and the first j of B. The recurrence reads like a decision: if the current characters match, dp[i][j] = dp[i-1][j-1] (no edit needed); otherwise it's 1 + min of three choices — delete (dp[i-1][j]), insert (dp[i][j-1]), or substitute (dp[i-1][j-1]). Three neighbors, one min. The base cases (turning a string into the empty string costs its length) seed the first row and column.
Where 2D DPs Run the World
These aren't academic toys. Edit distance powers spell-checkers ('did you mean…?' ranks candidates by edit distance) and fuzzy matching. Longest common subsequence is the engine of diff and version control (finding what changed between two files) and of DNA/protein sequence alignment in bioinformatics. 0/1 knapsack (max value within a weight limit, each item taken or not) models budgeting, resource allocation, and cargo loading. The 2D table that looks like an exercise is, in production, how your editor diffs files and how geneticists compare genomes.
Pippa's Confession
dp[3][2] is 'the cost to turn the first 3 letters of A into the first 2 letters of B.' Once each cell had a sentence, the three-way min stopped being a formula and became three obvious choices — delete, insert, or substitute. The trick that unlocked every 2D DP for me was refusing to write a recurrence until I could say, in plain words, exactly what dp[i][j] represents.