Workspace Longest Common Subsequence
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Longest Common Subsequence visualizer

Learn Longest Common Subsequence with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice string dp.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 16Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": "abcde",
  "b": "ace"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,b
},s)=>{
let d=Array.from({
length:a.length+1
},()=>Array(b.length+1).fill(0));
for(let i=1;
i<=a.length;
i++)for(let j=1;
j<=b.length;
j++){
d[i][j]=a[i-1]===b[j-1]?1+d[i-1][j-1]:Math.max(d[i-1][j],d[i][j-1]);
s("Match or skip character",{
DP:d,Cell:[i,j]
})
}
return d[a.length][b.length]
};

Execution canvas

Active Changed
STEP 1

Match or skip character

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

DP6 rows
Cell2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · String DP

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": "abcde",
  "b": "ace"
}

Execution steps

  1. Match or skip character

Computed output

3
JavaScript reference implementation
const solve = ({
  a,b
},s)=>{
  let d=Array.from({
    length:a.length+1
  },()=>Array(b.length+1).fill(0));
  for(let i=1;
  i<=a.length;
  i++)for(let j=1;
  j<=b.length;
  j++){
    d[i][j]=a[i-1]===b[j-1]?1+d[i-1][j-1]:Math.max(d[i-1][j],d[i][j-1]);
    s("Match or skip character",{
      DP:d,Cell:[i,j]
    })
  }
  return d[a.length][b.length]
};

Continue learning

Practice related DSA questions in the same topic.