11"""
2- Generate all valid combinations of parentheses (Iterative Approach).
2+ >>> generate_parentheses_iterative(1)
3+ ['()']
4+
5+ >>> generate_parentheses_iterative(0)
6+ ['']
7+
8+ Generate all valid combinations of parentheses (Iterative Approach).
39
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 ')'.
10+ The algorithm works as follows:
11+ 1. Initialize an empty list to store the combinations.
12+ 2. Initialize a stack to keep track of partial combinations.
13+ 3. Start with empty string and push it onstack along with the counts of '(' and ')'.
814
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+ 4. While the stack is not empty:
16+ a. Pop a partial combination and its open and close counts from the stack.
17+ b. If the combination length is equal to 2*n, add it to the result.
18+ c. If open count is < n, push new combination with added '(' onto the stack.
19+ d. If close count < open count, push new combination with added ')' on stack.
20+ 5. Return the result containing all valid combinations.
1521
16- Args:
17- n (int): The desired length of the parentheses combinations
22+ Args:
23+ n (int): The desired length of the parentheses combinations
1824
19- Returns:
20- list: A list of strings representing valid combinations of parentheses
25+ Returns:
26+ list: A list of strings representing valid combinations of parentheses
2127
22- Time Complexity:
23- O(2^(2n))
28+ Time Complexity:
29+ O(2^(2n))
2430
25- Space Complexity:
26- O(2^(2n))
31+ Space Complexity:
32+ O(2^(2n))
2733"""
2834
2935
30- def generate_parentheses ( n : int ) -> list :
36+ def generate_parentheses_iterative ( length : int = 0 ) -> list :
3137 """
32- >>> generate_parentheses (3)
38+ >>> generate_parentheses_iterative (3)
3339 ['()()()', '()(())', '(())()', '(()())', '((()))']
3440
35- >>> generate_parentheses (2)
41+ >>> generate_parentheses_iterative (2)
3642 ['()()', '(())']
3743
38- >>> generate_parentheses(1)
39- ['()']
40-
41- >>> generate_parentheses(0)
44+ >>> generate_parentheses_iterative()
4245 ['']
4346
4447 """
@@ -51,23 +54,19 @@ def generate_parentheses(n: int) -> list:
5154 while stack :
5255 current_combination , open_count , close_count = stack .pop ()
5356
54- if len (current_combination ) == 2 * n :
57+ if len (current_combination ) == 2 * length :
5558 result .append (current_combination )
5659 else :
57- if open_count < n :
60+ if open_count < length :
5861 stack .append ((current_combination + "(" , open_count + 1 , close_count ))
5962 if close_count < open_count :
6063 stack .append ((current_combination + ")" , open_count , close_count + 1 ))
6164
6265 return result
6366
6467
65- def main () -> None :
66- print (generate_parentheses (4 ))
67-
68-
6968if __name__ == "__main__" :
7069 import doctest
7170
7271 doctest .testmod ()
73- main ( )
72+ print ( generate_parentheses_iterative ( 3 ) )
0 commit comments