← capítulo

DP: LCS and edit distance

How different are two strings? A 2-D DP grid over their prefixes. The engine behind diff, spell-check, and DNA alignment.

The grid: three moves per cell

Watch the grid fill

kitten → sitting. Teal = free diagonal (chars equal). Orange path = the alignment: sub k→s, keep itt, sub e→i, keep n, +g. Distance 3.

Exponential → polynomial

Length 12: naive 42,000 calls, DP 169 cells → 250× less.

Alignment = a path through the grid

Takeaway

Sequence problems are cheapest-paths through a grid. One diagram serves coding theory, biology, and file diffing. Next: backtracking — search the space of choices, prune dead ends.