Generate Parentheses Question

https://ide.codingblocks.com/s/61488
Only 1 test case passed.

Hi Monika, your code is very complicated and does not produce output for n>=5. The problem is very simple. You have to use backtracking. Here is the algo for your help:

-Keep track of counts of open and close brackets.
-Initialize these counts as 0.
-Recursively call the generateParenthesis() function until open bracket count is less than the given n.
-If open bracket count becomes more than the close bracket count, then put a closing bracket and recursively call for the remaining brackets.
-If open bracket count is less than n, then put an opening bracket and call printParenthesis() for the remaining brackets.

Reply to this thread in case of further queries.

2 Likes

hi @Khushboo. I have also used Backtracking.
->I have stored the the parentheses in the array[eg-> (,(,),) if the input is 2].
->Then Permutate the the array of the Parentheses(using Backtracking) and stored it in the list.
->Apply operation on the list, so that there will be no duplicacy among the outputs.

Is this approach right?

please Reply…

@Khushboo Please Reply

Hi Monika, you’re creating a vector first, and then pushing brackets inside it using a loop and then you’re moving those brackets. And then you’re pushing more brackets depending on various conditions. All this is increasing the complexity of the solution and thus your code is not giving output for greater values of n when the number of combinations increase.
I’m sharing the code of the correct approach that you should follow for this problem. Go through it and reply to this thread in case of doubts.
https://ide.codingblocks.com/s/61872

1 Like

ok Thank you…

@Khushboo How does it get printed lexicographically???