forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_parentheses_iterative.py
More file actions
88 lines (69 loc) · 2.87 KB
/
generate_parentheses_iterative.py
File metadata and controls
88 lines (69 loc) · 2.87 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def generate_parentheses_iterative(length: int) -> list[str]:
"""
Generate all valid combinations of parentheses (Iterative Approach).
The algorithm works as follows:
1. Initialize an empty list to store the combinations.
2. Initialize a stack to keep track of partial combinations.
3. Start with empty string and push it onstack along with the counts of '(' and ')'.
4. While the stack is not empty:
a. Pop a partial combination and its open and close counts from the stack.
b. If the combination length is equal to 2*length, add it to the result.
c. If open count < length, push new combination with added '(' on stack.
d. If close count < open count, push new combination with added ')' on stack.
5. Return the result containing all valid combinations.
Args:
length: The desired length of the parentheses combinations
Returns:
A list of strings representing valid combinations of parentheses
Raises:
ValueError: If length is negative
TypeError: If length is not an integer
Time Complexity:
O(4^n / sqrt(n)) - Catalan number growth
Space Complexity:
O(4^n / sqrt(n)) - Storage for all valid combinations
>>> generate_parentheses_iterative(3)
['()()()', '()(())', '(())()', '(()())', '((()))']
>>> generate_parentheses_iterative(2)
['()()', '(())']
>>> generate_parentheses_iterative(1)
['()']
>>> generate_parentheses_iterative(0)
['']
>>> generate_parentheses_iterative(-1)
Traceback (most recent call last):
...
ValueError: length must be non-negative
>>> generate_parentheses_iterative(2.5)
Traceback (most recent call last):
...
TypeError: length must be an integer
"""
# Input validation
if not isinstance(length, int):
raise TypeError("length must be an integer")
if length < 0:
raise ValueError("length must be non-negative")
# Handle edge case
if length == 0:
return [""]
result: list[str] = []
# Each element in stack is a tuple (current_combination, open_count, close_count)
stack: list[tuple[str, int, int]] = [("", 0, 0)]
while stack:
current_combination, open_count, close_count = stack.pop()
# If we've used all pairs, add to result
if len(current_combination) == 2 * length:
result.append(current_combination)
continue
# Add '(' if we haven't used all open parentheses
if open_count < length:
stack.append((current_combination + "(", open_count + 1, close_count))
# Add ')' if it maintains validity
if close_count < open_count:
stack.append((current_combination + ")", open_count, close_count + 1))
return result
if __name__ == "__main__":
import doctest
doctest.testmod()
print(generate_parentheses_iterative(3))