Longest Common Subsequence
Match - diagonal + 1; otherwise max of up and left; trace back to recover the subsequence.
Decision · step 2 of 17String DP: LCS of abcde and ace
(1, 1): 'a' against 'a'. They match, so both prefixes end in a shared letter: diagonal 0 + 1 = 1.
What you will see
Two strings label the axes; a match lights the diagonal arrow; the traceback zigzags to the origin.
Cost
| Best | O(n * m) |
|---|---|
| Average | O(n * m) |
| Worst | O(n * m) |
| Space | O(n * m) |
O(min(n, m)) space for length only.
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Table cells announce their state and value; each step announces which cells were read and the value written.
Reduced motion: Dependency arrows appear statically and the cell value crossfades.
Before this
Taught by the same lesson
String DP covers these too, in the same run.