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

Palindrome Partitioning II visualizer

Learn Palindrome Partitioning II with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice partition and interval dp.

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

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let d=Array(a.length+1).fill(Infinity);
d[0]=-1;
for(let i=1;
i<=a.length;
i++)for(let j=0;
j<i;
j++){
let x=a.slice(j,i);
if(x===[...x].reverse().join(""))d[i]=Math.min(d[i],d[j]+1);
s("Test palindromic final segment",{
Array:[...a],DP:d.map(x=>Number.isFinite(x)?x:null),Segment:[j,i]
})
}
return d.at(-1)
};

Execution canvas

Active Changed
STEP 1

Test palindromic final segment

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

Array3 items
DP4 items
Segment2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · Partition and interval DP

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

"aab"

Execution steps

  1. Test palindromic final segment

Computed output

1
JavaScript reference implementation
const solve = (a,s)=>{
  let d=Array(a.length+1).fill(Infinity);
  d[0]=-1;
  for(let i=1;
  i<=a.length;
  i++)for(let j=0;
  j<i;
  j++){
    let x=a.slice(j,i);
    if(x===[...x].reverse().join(""))d[i]=Math.min(d[i],d[j]+1);
    s("Test palindromic final segment",{
      Array:[...a],DP:d.map(x=>Number.isFinite(x)?x:null),Segment:[j,i]
    })
  }
  return d.at(-1)
};

Continue learning

Practice related DSA questions in the same topic.