For given n find the number of valid combinations of valid parenthesis.I told him direct formula of Catalan number(becuase i encountered this prob earlier) but he specifically wanted this problem Solution using dynamic programming and wanted working solution with explanation with Test cases.I didn't managed to get to the working solution.
Eg:
n=1 valid par=0
n=2 valid par=1
Now I just want to understand this problem
I found one explanation but not getting it Please can you help me in understanding or can you provide more verbose explanation of below logic (which seems to be correct)
You need even number of paranthesis, if C(n) denotes the number of valid paranthesis with 2 * n paranthesis then
C(0)=1 and for any n>0
C(n)=C(0) * C(n-1)+C(1) * C(n-2)+...+C(n-1) * C(0)=sum(i=0,n-1,C(i) * C(n-1-i))
because you need to start with a '(' and see where is that closing bracket with a ')' if there is 2 * i paranthesis between them then the number of such cases is C(i) * C(n-1-i)
.