Learn Minimum Window Substring with a step-by-step Sliding Window & Two Pointers visualizer, JavaScript source code and a worked example. Practice fixed and variable windows.
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: Sliding Window & Two Pointers · Fixed and variable windows
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": "ADOBECODEBANC",
"b": "ABC"
}
Execution steps
Expand window
Shrink complete window
Computed output
"BANC"
JavaScript reference implementation
const solve = ({
a,b
},s)=>{
let need=new Map;
for(let x of b)need.set(x,(need.get(x)||0)+1);
let missing=b.length,l=0,best="";
for(let r=0;
r<a.length;
r++){
let x=a[r],n=need.get(x)||0;
if(n>0)missing--;
need.set(x,n-1);
while(!missing){
if(!best||r-l+1<best.length)best=a.slice(l,r+1);
let y=a[l++];
need.set(y,(need.get(y)||0)+1);
if(need.get(y)>0)missing++;
s("Shrink complete window",{
Array:[...a],Bounds:[l-1,r],Best:[best]
})
}
s("Expand window",{
Array:[...a],Missing:[missing],Bounds:[l,r],Best:[best]
})
}
return best
};