-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathgenerate_parentheses.py
59 lines (45 loc) · 1.57 KB
/
generate_parentheses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Input: 3
Output:
[
'((()))',
'(()())',
'(())()',
'()(())',
'()()()'
]
=========================================
This problem could be solved in several ways (using stack, queue, or just a simple list - see letter_combinations.py), all of them have the same complexity.
I'll solve it using simple recursive algorithm.
Time Complexity: O(4^N) , O(2^(2*N)) = O(4^N)
Space Complexity: O(4^N)
'''
############
# Solution #
############
def generate_parentheses(n):
result = []
if n == 0:
return result
combinations(result, n, n, '')
return result
def combinations(result, open_left, close_left, combination):
if close_left == 0:
# a new combination is created (no more open or close parentheses)
result.append(combination)
elif open_left == 0:
# no more open parentheses, so all left parentheses must be closed (just add the missing close parentheses)
result.append(combination + (')' * close_left))
else:
combinations(result, open_left - 1, close_left, combination + '(')
# check if there is a pair for this close parenthesis
if open_left < close_left:
combinations(result, open_left, close_left - 1, combination + ')')
###########
# Testing #
###########
# Test 1
# Correct result => ['((()))', '(()())', '(())()', '()(())', '()()()']
print(generate_parentheses(3))