Generate Parentheses
Medium Backtracking
n is a number of pairs of brackets. Each pair is an opening bracket ( and a closing bracket ).
A string of brackets is balanced when, read from the left, it never has more closing brackets than opening ones so far, and has as many of each at the end. Each closing bracket then closes the nearest opening bracket before it that is still open.
Return every balanced string made of exactly n pairs, each one once. The strings may come in any order.
Example 1
Two pairs sit side by side, ()(), or one inside the other, (()). Any other order of the four brackets, such as )(() or ())(, has a closing bracket with no open one before it to close.
Example 2
Constraints
1 ≤ n ≤ 8- The largest answer, for
n = 8, has 1,430 strings
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Backtracking
- Combination SumAI round · Medium
- PermutationsAI round · Medium
- SubsetsAI round · Medium
- Letter Combinations of a Phone NumberMedium
- Word SearchMedium