Skip to content

Commit 95e9aa1

Browse files
Update generate_parentheses_iterative.py
1 parent 6c84846 commit 95e9aa1

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

backtracking/generate_parentheses_iterative.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1-
"""
2-
Generate all valid combinations of parentheses (Iterative Approach).
3-
4-
The algorithm works as follows:
5-
1. Initialize an empty list to store the combinations.
6-
2. Initialize a stack to keep track of partial combinations.
7-
3. Start with empty string and push it onstack along with the counts of '(' and ')'.
8-
9-
4. While the stack is not empty:
10-
a. Pop a partial combination and its open and close counts from the stack.
11-
b. If the combination length is equal to 2*n, add it to the result.
12-
c. If open count is < n, push new combination with added '(' onto the stack.
13-
d. If close count < open count, push new combination with added ')' on stack.
14-
5. Return the result containing all valid combinations.
15-
16-
Args:
17-
n (int): The desired length of the parentheses combinations
18-
19-
Returns:
20-
list: A list of strings representing valid combinations of parentheses
21-
22-
Time Complexity:
23-
O(2^(2n))
24-
25-
Space Complexity:
26-
O(2^(2n))
27-
"""
28-
29-
30-
def generate_parentheses_iterative(length: int = 0) -> list:
1+
def generate_parentheses_iterative(length: int) -> list:
312
"""
3+
Generate all valid combinations of parentheses (Iterative Approach).
4+
5+
The algorithm works as follows:
6+
1. Initialize an empty list to store the combinations.
7+
2. Initialize a stack to keep track of partial combinations.
8+
3. Start with empty string and push it onstack along with the counts of '(' and ')'.
9+
4. While the stack is not empty:
10+
a. Pop a partial combination and its open and close counts from the stack.
11+
b. If the combination length is equal to 2*length, add it to the result.
12+
c. If open count is < length, push new combination with added '(' onto the stack.
13+
d. If close count < open count, push new combination with added ')' on stack.
14+
5. Return the result containing all valid combinations.
15+
16+
Args:
17+
length: The desired length of the parentheses combinations
18+
19+
Returns:
20+
A list of strings representing valid combinations of parentheses
21+
22+
Time Complexity:
23+
O(2^(2n))
24+
25+
Space Complexity:
26+
O(2^(2n))
27+
3228
>>> generate_parentheses_iterative(3)
3329
['()()()', '()(())', '(())()', '(()())', '((()))']
3430
>>> generate_parentheses_iterative(2)
3531
['()()', '(())']
3632
>>> generate_parentheses_iterative(1)
3733
['()']
38-
>>> generate_parentheses_iterative()
34+
>>> generate_parentheses_iterative(0)
3935
['']
4036
"""
4137
result = []
4238
stack = []
4339

44-
# Each element in stack has a tuple (current_combination, open_count, close_count).
40+
# Each element in stack is a tuple (current_combination, open_count, close_count)
4541
stack.append(("", 0, 0))
4642

4743
while stack:
4844
current_combination, open_count, close_count = stack.pop()
4945

5046
if len(current_combination) == 2 * length:
5147
result.append(current_combination)
52-
else:
53-
if open_count < length:
54-
stack.append((current_combination + "(", open_count + 1, close_count))
55-
if close_count < open_count:
56-
stack.append((current_combination + ")", open_count, close_count + 1))
48+
49+
if open_count < length:
50+
stack.append((current_combination + "(", open_count + 1, close_count))
51+
52+
if close_count < open_count:
53+
stack.append((current_combination + ")", open_count, close_count + 1))
5754

5855
return result
5956

0 commit comments

Comments
 (0)