Skip to content

Commit 09f6b10

Browse files
committed
Add type hints and improve code quality for generate_parentheses_iterative
1 parent 7d9dd2d commit 09f6b10

1 file changed

Lines changed: 7 additions & 30 deletions

File tree

backtracking/generate_parentheses_iterative.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def generate_parentheses_iterative(length: int) -> list[str]:
55
The algorithm works as follows:
66
1. Initialize an empty list to store the combinations.
77
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 ')'.
8+
3. Start with empty string and push it on stack along with the counts of '(' and ')'.
99
4. While the stack is not empty:
1010
a. Pop a partial combination and its open and close counts from the stack.
1111
b. If the combination length is equal to 2*length, add it to the result.
@@ -17,19 +17,13 @@ def generate_parentheses_iterative(length: int) -> list[str]:
1717
length: The desired length of the parentheses combinations
1818
1919
Returns:
20-
A list of strings representing valid combinations of parentheses
21-
22-
Raises:
23-
ValueError: If length is negative
24-
TypeError: If length is not an integer
25-
20+
A list of strings representing valid combinations of parentheses.
21+
2622
Time Complexity:
27-
O(4^n / sqrt(n)) - Catalan number growth
28-
23+
O(2^(2*length))
2924
Space Complexity:
30-
O(4^n / sqrt(n)) - Storage for all valid combinations
31-
32-
25+
O(2^(2*length))
26+
3327
>>> generate_parentheses_iterative(3)
3428
['()()()', '()(())', '(())()', '(()())', '((()))']
3529
>>> generate_parentheses_iterative(2)
@@ -38,22 +32,7 @@ def generate_parentheses_iterative(length: int) -> list[str]:
3832
['()']
3933
>>> generate_parentheses_iterative(0)
4034
['']
41-
>>> generate_parentheses_iterative(-1)
42-
Traceback (most recent call last):
43-
...
44-
ValueError: length must be non-negative
45-
>>> generate_parentheses_iterative(2.5)
46-
Traceback (most recent call last):
47-
...
48-
TypeError: length must be an integer
4935
"""
50-
# Input validation
51-
if not isinstance(length, int):
52-
raise TypeError("length must be an integer")
53-
if length < 0:
54-
raise ValueError("length must be non-negative")
55-
56-
# Handle edge case
5736
if length == 0:
5837
return [""]
5938

@@ -65,22 +44,20 @@ def generate_parentheses_iterative(length: int) -> list[str]:
6544
while stack:
6645
current_combination, open_count, close_count = stack.pop()
6746

68-
# If we've used all pairs, add to result
6947
if len(current_combination) == 2 * length:
7048
result.append(current_combination)
7149
continue
7250

73-
# Add '(' if we haven't used all open parentheses
7451
if open_count < length:
7552
stack.append((current_combination + "(", open_count + 1, close_count))
7653

77-
# Add ')' if it maintains validity
7854
if close_count < open_count:
7955
stack.append((current_combination + ")", open_count, close_count + 1))
8056

8157
return result
8258

8359

60+
8461
if __name__ == "__main__":
8562
import doctest
8663

0 commit comments

Comments
 (0)