Workspace Boolean Parenthesization
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Boolean Parenthesization visualizer

Learn Boolean Parenthesization 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 / 4Execution step
Reference example · JavaScript
Inspect reference input
"T|F&T"

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let n=(a.length+1)/2,d=Array.from({
length:n
},()=>Array.from({
length:n
},()=>[0,0]));
for(let i=0;
i<n;
i++)d[i][i][a[2*i]==="T"?1:0]=1;
for(let len=2;
len<=n;
len++)for(let i=0;
i+len<=n;
i++){
let j=i+len-1;
for(let k=i;
k<j;
k++)for(let x=0;
x<2;
x++)for(let y=0;
y<2;
y++){
let op=a[2*k+1],v=op==="&"?x&y:op==="|"?x|y:x^y;
d[i][j][v]+=d[i][k][x]*d[k+1][j][y]
}
s("Combine truth counts at each split",{
DP:d.map(r=>r.map(x=>x.join("/"))),Interval:[i,j]
})
}
return d[0][n-1][1]
};

Execution canvas

Active Changed
STEP 1

Combine truth counts at each split

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

DP3 rows
Interval2 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

"T|F&T"

Execution steps

  1. Combine truth counts at each split

Computed output

2
JavaScript reference implementation
const solve = (a,s)=>{
  let n=(a.length+1)/2,d=Array.from({
    length:n
  },()=>Array.from({
    length:n
  },()=>[0,0]));
  for(let i=0;
  i<n;
  i++)d[i][i][a[2*i]==="T"?1:0]=1;
  for(let len=2;
  len<=n;
  len++)for(let i=0;
  i+len<=n;
  i++){
    let j=i+len-1;
    for(let k=i;
    k<j;
    k++)for(let x=0;
    x<2;
    x++)for(let y=0;
    y<2;
    y++){
      let op=a[2*k+1],v=op==="&"?x&y:op==="|"?x|y:x^y;
      d[i][j][v]+=d[i][k][x]*d[k+1][j][y]
    }
    s("Combine truth counts at each split",{
      DP:d.map(r=>r.map(x=>x.join("/"))),Interval:[i,j]
    })
  }
  return d[0][n-1][1]
};

Continue learning

Practice related DSA questions in the same topic.