Skip to content

Longest Common Subsequence
DP Table Step by Step

Every cell compares two letters. Matches add 1 to the diagonal, and mismatches keep the larger of the cell above or the cell to the left.

Move through the table in either direction, then share a link to the step where the letters stopped matching.

What You Watch While Two Strings Compare

Every cell compares two letters, and the visualiser shows the result.

Every cell holds the LCS length of two prefixes.

CodeStory's visualiser fills the table one cell at a time and keeps the executing line of code highlighted. Use the controls to step back, step forward or play.

Open Reversed to see order matter. Then load One inside the other to see a subsequence skip letters.

How the LCS Table Fills Up

Steps below follow the table you see.

  1. 1Put one string along the top and the other down the side, with an empty row and column of 0.
  2. 2Compare the two letters that meet in every cell.
  3. 3Add 1 to the diagonal cell when the letters match.
  4. 4Otherwise copy the larger of the cell above and the cell to the left.
  5. 5Find the LCS length in the bottom right cell.

LCS Inputs Worth Trying

Six presets cover ties, empty matches and skipped letters.

  • ClassicSeveral LCSs of length 4 exist, and the tie rule selects one.
  • No common lettersAll cells stay 0.
  • Identical stringsEvery letter matches along the main diagonal.
  • One letterA single row makes the smallest real table.
  • One inside the otherAll of ACE survives because a subsequence can skip letters.
  • ReversedOrder matters, so only one letter survives.

LCS Cost in Time and Space

Interviewers ask why the table needs m times n cells, and the visualiser counts real operations.

Time
O(m·n)counted from the cells and comparisons you step through
Space
O(m·n)extra memory for the m by n table
Pattern
Recursion & DPPrefix pairs get solved once and reused.

Everything in the App in One Place

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

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

LCS Visualiser FAQs

Open the visualiser and follow the letters cell by cell.

Open the visualiser
What does the LCS visualiser show?

CodeStory's visualiser fills the table for your two strings, with the call tree and the highlighted line of code beside it. You can move one cell at a time in either direction or play it.

What is the time complexity of longest common subsequence?

O(m·n) time and O(m·n) space, where m and n are the two string lengths. CodeStory's visualiser counts real operations while it plays, so the number grows in front of you instead of sitting in a formula.

What is the difference between a subsequence and a substring?

Subsequences keep order but can skip letters, while substrings stay in one block. One inside the other shows ACE surviving as a subsequence.

Why can an LCS have more than one answer?

Ties happen when the cell above and the cell to the left hold the same value. Classic has several LCSs of length 4, and the tie rule selects one.

Can I use my own input?

Yes. Enter your own two strings and the visualiser fills the table for them.

Is the visualiser free?

Yes. Sign up free for 100 AI credits, get 100 more every month and watch any algorithm without spending credits.

Stuck on LCS? Get a Free Callback

Tell CodeStory which DP step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from string basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings that fit your schedule.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.