Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
Intuition
We can generate all sequences of '('
and ')'
characters. Then, we will check if each one is valid.
Algorithm
To generate all sequences, we use a recursion. All sequences of length n
is just '('
plus all sequences of length n-1
, and then ')'
plus all sequences of length n-1
.
To check whether a sequence is valid, we keep track of balance
, the net number of opening brackets minus closing brackets. If it falls below zero at any time, or doesn't end in zero, the sequence is invalid - otherwise it is valid.
Complexity Analysis
Time Complexity : . For each of sequences, we need to create and validate the sequence, which takes work.
Space Complexity : . Naively, every sequence could be valid. See Approach #3 for development of a tighter asymptotic bound.
Intuition and Algorithm
Instead of adding '('
or ')'
every time as in Approach #1, let's only add them when we know it will remain a valid sequence. We can do this by keeping track of the number of opening and closing brackets we have placed so far.
We can start an opening bracket if we still have one (of n
) left to place. And we can start a closing bracket if it would not exceed the number of opening brackets.
Complexity Analysis
Our complexity analysis rests on understanding how many elements there are in generateParenthesis(n)
. This analysis is outside the scope of this article, but it turns out this is the n
-th Catalan number , which is bounded asymptotically by .
Time Complexity : . Each valid sequence has at most n
steps during the backtracking procedure.
Space Complexity : , as described above, and using space to store the sequence.
Intuition
To enumerate something, generally we would like to express it as a sum of disjoint subsets that are easier to count.
Consider the closure number of a valid parentheses sequence S
: the least index >= 0
so that S[0], S[1], ..., S[2*index+1]
is valid. Clearly, every parentheses sequence has a unique closure number. We can try to enumerate them individually.
Algorithm
For each closure number c
, we know the starting and ending brackets must be at index 0
and 2*c + 1
. Then, the 2*c
elements between must be a valid sequence, plus the rest of the elements must be a valid sequence.
Complexity Analysis
Analysis written by: @awice. Approach #2 inspired by @brobins9.