Workspace Shortest Palindrome
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Shortest Palindrome visualizer

Learn Shortest Palindrome with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice pattern matching.

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 / 3Execution step
Reference example · JavaScript
Inspect reference input
"aacecaaa"

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let i=a.length;
while(i>0){
let p=a.slice(0,i);
s("Test palindromic prefix",{
Prefix:[...p]
});
if(p===[...p].reverse().join(""))return [...a.slice(i)].reverse().join("")+a;
i--
}
return a
};

Execution canvas

Active Changed
STEP 1

Test palindromic prefix

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

Prefix8 items
Keyboard: Space to play · Left / Right to stepExplore next problem

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

"aacecaaa"

Execution steps

  1. Test palindromic prefix

Computed output

"aaacecaaa"
JavaScript reference implementation
const solve = (a,s)=>{
  let i=a.length;
  while(i>0){
    let p=a.slice(0,i);
    s("Test palindromic prefix",{
      Prefix:[...p]
    });
    if(p===[...p].reverse().join(""))return [...a.slice(i)].reverse().join("")+a;
    i--
  }
  return a
};

Continue learning

Practice related DSA questions in the same topic.