Back to questionsDSA Visualizers: 388 Coding Questions Z Algorithm visualizer Learn Z Algorithm with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice pattern matching.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 7 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input "aabxaab" Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let z=Array(a.length).fill(0 ),l=0 ,r=0 ;
3 for (let i=1 ;
4 i<a.length;
5 i++){
6 if (i<=r)z[i]=Math.min(r-i+1 ,z[i-l]);
7 while (i+z[i]<a.length&&a[z[i]]===a[i+z[i]])z[i]++;
8 if (i+z[i]-1 >r){
9 l=i;
10 r=i+z[i]-1
11 }
12 s("Extend prefix match",{
13 Array:[...a],Z:z,Bounds:[l,r]
14 },[i])
15 }
16 return z
17 };
Execution canvas Active ChangedSTEP 1 Extend prefix match Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Strings · Pattern matching
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 "aabxaab" Execution steps Extend prefix match Computed output [
0,
1,
0,
0,
3,
1,
0
] JavaScript reference implementation const solve = (a,s)=>{
let z=Array(a.length).fill(0),l=0,r=0;
for(let i=1;
i<a.length;
i++){
if(i<=r)z[i]=Math.min(r-i+1,z[i-l]);
while(i+z[i]<a.length&&a[z[i]]===a[i+z[i]])z[i]++;
if(i+z[i]-1>r){
l=i;
r=i+z[i]-1
}
s("Extend prefix match",{
Array:[...a],Z:z,Bounds:[l,r]
},[i])
}
return z
};Continue learning Practice related DSA questions in the same topic.