Generate Parentheses
Algorithm/Backtracking
Intuition
The problem asks us to generate all valid combinations of n
pairs of parentheses. This is a classic backtracking problem where we build solutions by adding either an opening (
or a closing )
bracket at each step, ensuring that each combination remains valid.
Approach
Below is the step-by-step breakdown of the approach:
-
Backtracking with Constraints:
- Start with an empty string and two counters for open and close parentheses remaining.
- For each recursive call, if both counters are zero, the string is complete and valid, so add it to the results.
- If there are open parentheses left, add an open parenthesis and decrease the open counter.
- If there are fewer open than close parentheses, add a closing parenthesis and decrease the close counter. This ensures parentheses are balanced.
-
Return Result:
- Initiate backtracking from an empty string and
n
open and close parentheses. - Return the list of valid combinations collected during the recursive calls.
- Initiate backtracking from an empty string and
Complexity
- Time complexity: , where n is the number of pairs. This follows from Catalan number growth in generating balanced parentheses.
- Space complexity: for recursion depth.