Skip to content

Commit 84f101c

Browse files
Pr0-C0dernightmare10123MaximSmolskiypre-commit-ci[bot]
authored
Add/generate parentheses iterative approach (#10024)
* Generate parantheses iterative * Generate parantheses iterative * Generating parantheses code using iterative approach * Update generate_parentheses_iterative.py * updating DIRECTORY.md * Update generate_parentheses_iterative.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update generate_parentheses_iterative.py * Update generate_parentheses_iterative.py * Update generate_parentheses_iterative.py --------- Co-authored-by: nightmare10123 <[email protected]> Co-authored-by: Maxim Smolskiy <[email protected]> Co-authored-by: MaximSmolskiy <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e6b5d26 commit 84f101c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [Combination Sum](backtracking/combination_sum.py)
1313
* [Crossword Puzzle Solver](backtracking/crossword_puzzle_solver.py)
1414
* [Generate Parentheses](backtracking/generate_parentheses.py)
15+
* [Generate Parentheses Iterative](backtracking/generate_parentheses_iterative.py)
1516
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
1617
* [Knight Tour](backtracking/knight_tour.py)
1718
* [Match Word Pattern](backtracking/match_word_pattern.py)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
def generate_parentheses_iterative(length: int) -> list:
2+
"""
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 < length, push new combination with added '(' on 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^(2*length))
24+
25+
Space Complexity:
26+
O(2^(2*length))
27+
28+
>>> generate_parentheses_iterative(3)
29+
['()()()', '()(())', '(())()', '(()())', '((()))']
30+
>>> generate_parentheses_iterative(2)
31+
['()()', '(())']
32+
>>> generate_parentheses_iterative(1)
33+
['()']
34+
>>> generate_parentheses_iterative(0)
35+
['']
36+
"""
37+
result = []
38+
stack = []
39+
40+
# Each element in stack is a tuple (current_combination, open_count, close_count)
41+
stack.append(("", 0, 0))
42+
43+
while stack:
44+
current_combination, open_count, close_count = stack.pop()
45+
46+
if len(current_combination) == 2 * length:
47+
result.append(current_combination)
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))
54+
55+
return result
56+
57+
58+
if __name__ == "__main__":
59+
import doctest
60+
61+
doctest.testmod()
62+
print(generate_parentheses_iterative(3))

0 commit comments

Comments
 (0)