Skip to content

Longest common subsequence
one step at a time.

Fill a table of prefix pairs: a matching letter extends the diagonal by one, otherwise keep the better of dropping a letter from either string.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

Fill a table of prefix pairs: a matching letter extends the diagonal by one, otherwise keep the better of dropping a letter from either string. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the call tree, and the table filling in as answers stop being recomputed. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. Longest common subsequence on a clean example teaches you the shape; longest common subsequence on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Break the problem into a smaller copy of itself.
  2. 2Solve the smallest cases directly: those are the base cases.
  3. 3Store each answer the first time it is computed.
  4. 4Reuse the stored answer instead of solving the same call twice.

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

  • ClassicSeveral LCSs of length 4 exist; the tie rule picks one.
  • No common lettersEvery cell stays 0.
  • Identical stringsThe answer runs straight down the diagonal.
  • One letterA single row: the smallest real table.
  • One inside the otherAll of ACE survives: a subsequence may skip letters.
  • ReversedOrder matters: only one letter survives.

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(m·n)counted as real operations while it runs, not quoted from a table
Space
O(m·n)what the algorithm holds besides the input itself
Pattern
Recursion & DPSolve smaller copies, then stop solving them twice.

Everything in the app, in one place.

Practice, interviews and the tools, all free to start. Swipe to see the rest.

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About longest common subsequence.

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the longest common subsequence visualiser show?

Every step of the algorithm on input you choose: the call tree, and the table filling in as answers stop being recomputed, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of longest common subsequence?

O(m·n) time and O(m·n) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.