Workspace Edit Distance
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Edit Distance visualizer

Learn Edit Distance 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": "horse",
  "b": "ros"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,b
},s)=>{
let d=Array.from({
length:a.length+1
},(_,i)=>Array.from({
length:b.length+1
},(_,j)=>i===0?j:j===0?i: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]?d[i-1][j-1]:1+Math.min(d[i-1][j],d[i][j-1],d[i-1][j-1]);
s("Compute edit cost",{
DP:d,Cell:[i,j]
})
}
return d[a.length][b.length]
};

Execution canvas

Active Changed
STEP 1

Compute edit cost

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": "horse",
  "b": "ros"
}

Execution steps

  1. Compute edit cost

Computed output

3
JavaScript reference implementation
const solve = ({
  a,b
},s)=>{
  let d=Array.from({
    length:a.length+1
  },(_,i)=>Array.from({
    length:b.length+1
  },(_,j)=>i===0?j:j===0?i: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]?d[i-1][j-1]:1+Math.min(d[i-1][j],d[i][j-1],d[i-1][j-1]);
    s("Compute edit cost",{
      DP:d,Cell:[i,j]
    })
  }
  return d[a.length][b.length]
};

Continue learning

Practice related DSA questions in the same topic.