Back to questionsDSA Visualizers: 388 Coding Questions Longest Happy Prefix visualizer Learn Longest Happy Prefix 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 / 5 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input "level" Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let p=Array(a.length).fill(0 ),j=0 ;
3 for (let i=1 ;
4 i<a.length;
5 i++){
6 while (j&&a[i]!==a[j])j=p[j-1 ];
7 if (a[i]===a[j])j++;
8 p[i]=j;
9 s("Update prefix function",{
10 Array:[...a],LPS:p
11 },[i])
12 }
13 return a.slice(0 ,p.at(-1 ))
14 };
Execution canvas Active ChangedSTEP 1 Update prefix function 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 "level" Execution steps Update prefix function Computed output "l" JavaScript reference implementation const solve = (a,s)=>{
let p=Array(a.length).fill(0),j=0;
for(let i=1;
i<a.length;
i++){
while(j&&a[i]!==a[j])j=p[j-1];
if(a[i]===a[j])j++;
p[i]=j;
s("Update prefix function",{
Array:[...a],LPS:p
},[i])
}
return a.slice(0,p.at(-1))
};Continue learning Practice related DSA questions in the same topic.