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.
- 1Put one string along the top and the other down the side, with an empty row and column of 0.
- 2Compare the two letters that meet in every cell.
- 3Add 1 to the diagonal cell when the letters match.
- 4Otherwise copy the larger of the cell above and the cell to the left.
- 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.
Practise Dynamic Programming Beyond LCS
Watching shows the table and solving trains you to spot it.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory checks your level, from string basics to interview ready.
- 2CodeStory explains online and campus batches, with timings that fit your schedule.
- 3You decide. No pressure, no payment on the call.