Square brackets : Mutli DP

Unable to approach the question. Pls help

Hi @aman2382000, I’ll describe this approach below (with a few minor tweaks).

The recurrence used is f(i, j) which returns the number of valid bracket sequences from position 1 to i with j more opening brackets than closing brackets. For convenience let’s call the difference between opening and closing brackets simply the difference. If we are at position i and we need j more β€œ[” than β€œ]”, then we can obtain that by two ways.

We put a β€œ[” at position i to get a difference j. This means that there must have been a difference of j-1 upto position i-1.
We put a β€œ]” at position i to get a difference j. This implies that there must have been a difference of j+1 upto position iβˆ’1.
We are also given a set of positions s which must contain β€œ[”. For such positions, option 2 is unavailable. For j<0 naturally f(i, j)=0 because we are proceeding from left to right and if we have more β€œ]” than β€œ[” we can never end up with a valid sequence. Again f(0,0)=1 as the empty sequence with difference 0 is valid. Also f(0,j) for j>0 is 0

Putting all of this together,

f(i,j)= 
0  if j<0 or i=0,j>0
1 if i=0,j=0
f(iβˆ’1,jβˆ’1)  if i is in s
f(iβˆ’1,jβˆ’1)+f(iβˆ’1,j+1) otherwise

​

The final answer will be f(2n,0) as 2n is the last position in our sequence and we want the sequence to be valid, so we need difference 0.

you can refer to the solution itself it has nice explanation

In case of any doubt feel free to ask :slight_smile:
mark your doubt as resolved if u got the answe

What is meant by improper bracket expression here ?

@aman2382000, improper bracket expression means int bracket expression which is valid i.e. equal number of opening and closing brackets and makes sense eg

]][[,[[[]],][][,[[[[[ etc

Hi @aman2382000
if u still hv any doubts related to the question, feel free to post them here.