Workspace Longest String Chain
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Longest String Chain visualizer

Learn Longest String Chain with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice stocks and increasing subsequences.

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 / 7Execution step
Reference example · JavaScript
Inspect reference input
[
  "a",
  "b",
  "ba",
  "bca",
  "bda",
  "bdca"
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
a.sort((x,y)=>x.length-y.length);
let m=new Map,b=0;
for(let w of a){
let v=1;
for(let i=0;
i<w.length;
i++)v=Math.max(v,1+(m.get(w.slice(0,i)+w.slice(i+1))||0));
m.set(w,v);
b=Math.max(b,v);
s("Delete one character to find predecessor",{
Words:a,Chains:[...m].map(x=>x.join(":")),Best:[b]
})
}
return b
};

Execution canvas

Active Changed
STEP 1

Delete one character to find predecessor

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

Words6 items
Chains1 items
Best1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · Stocks and increasing subsequences

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",
  "b",
  "ba",
  "bca",
  "bda",
  "bdca"
]

Execution steps

  1. Delete one character to find predecessor

Computed output

4
JavaScript reference implementation
const solve = (a,s)=>{
  a.sort((x,y)=>x.length-y.length);
  let m=new Map,b=0;
  for(let w of a){
    let v=1;
    for(let i=0;
    i<w.length;
    i++)v=Math.max(v,1+(m.get(w.slice(0,i)+w.slice(i+1))||0));
    m.set(w,v);
    b=Math.max(b,v);
    s("Delete one character to find predecessor",{
      Words:a,Chains:[...m].map(x=>x.join(":")),Best:[b]
    })
  }
  return b
};

Continue learning

Practice related DSA questions in the same topic.