Solution: Generate Parentheses
Let's solve the Generate Parentheses problem using the Subsets pattern.
We'll cover the following
Solution#
Problems where we require constructing multiple valid answers, such as permutation of any number or generating a long list of pairs of balanced parentheses, are good examples to solve using the subsets pattern.
To solve this problem using the subsets pattern, we use a recursive algorithm. The purpose of this algorithm is to correctly generate all of the possible subsets of the parentheses combination.
The key observation in constructing the solution is to keep track of the number of opening, (, and closing, ), parentheses. We’ll use this count to add parentheses in a way that keeps the sequence balanced and valid.
-
We only add an opening parenthesis if there is still one to add.
-
We only add a closing parenthesis if the number of closing parentheses does not exceed the number of opening parentheses.
The basic algorithm is as follows:
- Create a list to store all possible combinations of parentheses.
- Call the
back_trackfunction fornpairs of parentheses, count of left parentheses, count of right parentheses, an empty list for output, and an empty list for the result. - If the count of opening and closing parentheses equals
n, then we have a valid combination of parentheses, so we will append the string to our list. - Otherwise, we will check if the number of opening parentheses is less than
n. If yes, we will add opening parentheses to our string and increment the count. - Lastly, we will check the count of closing parentheses. If the count is less than the count of opening parentheses, then we will add closing parentheses to our string and increment its count.
Let’s see how this algorithm works for an example, where :
1 of 44
2 of 44
3 of 44
4 of 44
5 of 44
6 of 44
7 of 44
8 of 44
9 of 44
10 of 44
11 of 44
12 of 44
13 of 44
14 of 44
15 of 44
16 of 44
17 of 44
18 of 44
19 of 44
20 of 44
21 of 44
22 of 44
23 of 44
24 of 44
25 of 44
26 of 44
27 of 44
28 of 44
29 of 44
30 of 44
31 of 44
32 of 44
33 of 44
34 of 44
35 of 44
36 of 44
37 of 44
38 of 44
39 of 44
40 of 44
41 of 44
42 of 44
43 of 44
44 of 44
Let’s look at the code for this solution below:
Time complexity#
As visualized in the slides above, it’s easier to understand the problem by creating a tree. Given a tree with branching of and a maximum depth of , the maximum number of nodes in this tree would be the following:
This is the sum of a geometric sequence, which evaluates to the following:
So, we can say that time complexity is .
For our problem here, the following is true:
-
There is a branching of because we only add an opening or a closing brace at each step.
-
There is a maximum depth of because the length of the output is the sum of opening and closing parentheses.
This leads us to a time complexity of , that is, .
Note: This is not the tightest bound on time complexity because our algorithm rejects invalid combinations, significantly saving time. Without going into the proof, which is beyond the scope of this course, we’ll note that the upper bound on the time complexity of this solution is .
Space complexity#
The space complexity of this solution is , since the maximum size of the stack is .
Generate Parentheses
Final Remarks