AlgoScope

Longest Common Subsequence

Algorithmintermediate2D Dynamic Programmingalso called LCS

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
aceabcde000✓0✓01✓0000

(1, 1): 'a' against 'a'. They match, so both prefixes end in a shared letter: diagonal 0 + 1 = 1.

Open in the player →or start at step 2

What you will see

Two strings label the axes; a match lights the diagonal arrow; the traceback zigzags to the origin.

How string dp works →

Cost

BestO(n * m)
AverageO(n * m)
WorstO(n * m)
SpaceO(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

Leads to

Topics that need this one first.

Taught by the same lesson

String DP covers these too, in the same run.