Workspace Minimum Window Substring
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Minimum Window Substring visualizer

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.

1 / 24Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": "ADOBECODEBANC",
  "b": "ABC"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
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
};

Execution canvas

Active Changed
STEP 1

Expand window

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

Array13 items
Missing1 items
Bounds2 items
Best1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

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

  1. Expand window
  2. 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
};

Continue learning

Practice related DSA questions in the same topic.