Brackets all over

I can’t understand how to solve the question.
Search all over the internet. Can anyone provide a video explanation for the code.

This question is a clear dynamic programming question and can be solved by dp only because it is a clear combinatorial optimization problem.
what you have to do is think of a recursive approach first.

I am explaining the question to you.

Actually the question says that you have to generate the string of size n given m is the length of given string
Where m<=n

Now you have to generate a bracket string which is balanced
By adding a prefix and suffix string to given string s
Now the prefix string should have opening brackets greater than closing brackets, because the string should be overall be balanced.

For example if you are given s = β€œ))” and n = 6(m = 2 obviously) then you can generate following pairs of a and b to make a+s+b a valid sequence:

β€œ()((” β€œ))” β€œβ€ // Remeber a and b can be blank also.
β€œ((” β€œ))” β€œ()”
β€œ(((” β€œ))” β€œ)”
β€œ(()(” β€œ))” β€œβ€
β€œ((()” β€œ))” β€œβ€

Hence, the ans for this testcase is 5. If there is no possible combination then print -1.

Approach

  1. It is quite obvious that we would only add opening brackets β€˜(’ to string A and only closing brackets β€˜)’ to string B
  2. Since the number of open and close brackets must be equal, we can never generate a valid string if n is odd.
  3. Count the total number of opening and closing brackets required to balanced the string. The opening brackets will go in string A and the closing brackets will go in string B.
  4. We start from pos = 0 and go till pos = n - m + 1. At each instance we consider three possibilities.
  • if closingBrackets have not been implemented yet (close==0) and count of open brackets that we have implemented is more than required (open >= openBrackets) then, we implement the closing brackets required for string B and raise a flag for the same by marking close=1 also, change the open bracket count accordingly for this case
  • if we have some open brackets then, put a closing bracket to balance it and decrease open count to denote there is one less unbalanced open bracket now
  • add another opening bracket to the string so far. This bracket will be initially unbalanced.
  1. We add the results obtained from the three possibilities. We store and return this result.
  2. Continue the above process till pos < n-m+1.
  3. At pos == n-m+1, we must ensure that all required opening and closing brackets have been implemented. We do so by checking the status of out flag variable close i.e. check close==1. Also make sure there are no pending unbalanced open brackets (open==0).

You can refer to this code

if this solves your doubt, please mark it as resolved :slight_smile:

Mam, can i get the recursive code, I am not able to understand DP solution directly

what is position given in the code? How is position = n-m+1

pos refers to the position counter we keep on moving it fwd till it reaches the end i.e(n-m+1)
This question is a clear dynamic programming question and can be solved by dp only because it is a clear combinatorial optimization problem.
if you dont Know dynamic programming . leave it for now

Mam why have you done open-openBrackets + closeBrackets? when the no. of open brackets >= open Brackets required