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.
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
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
};