-
-
Notifications
You must be signed in to change notification settings - Fork 47.7k
Add/generate parentheses iterative approach #10024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaximSmolskiy
merged 11 commits into
TheAlgorithms:master
from
Pr0-C0der:fix/generate_parentheses_iterative_approach
Aug 27, 2025
+63
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad4bbd6
Generate parantheses iterative
nightmare10123 eeb4131
Generate parantheses iterative
nightmare10123 9428fa3
Generating parantheses code using iterative approach
nightmare10123 f027cc1
Update generate_parentheses_iterative.py
MaximSmolskiy 43a3fa7
Merge branch 'master' into fix/generate_parentheses_iterative_approach
MaximSmolskiy 6c84846
updating DIRECTORY.md
MaximSmolskiy 95e9aa1
Update generate_parentheses_iterative.py
MaximSmolskiy d2d83c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4bee601
Update generate_parentheses_iterative.py
MaximSmolskiy 8510873
Update generate_parentheses_iterative.py
MaximSmolskiy dda3067
Update generate_parentheses_iterative.py
MaximSmolskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
backtracking/generate_parentheses_iterative.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
def generate_parentheses_iterative(length: int) -> list: | ||
""" | ||
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 | ||
|
||
Time Complexity: | ||
O(2^(2*length)) | ||
|
||
Space Complexity: | ||
O(2^(2*length)) | ||
|
||
>>> generate_parentheses_iterative(3) | ||
['()()()', '()(())', '(())()', '(()())', '((()))'] | ||
>>> generate_parentheses_iterative(2) | ||
['()()', '(())'] | ||
>>> generate_parentheses_iterative(1) | ||
['()'] | ||
>>> generate_parentheses_iterative(0) | ||
[''] | ||
""" | ||
result = [] | ||
stack = [] | ||
|
||
# Each element in stack is a tuple (current_combination, open_count, close_count) | ||
stack.append(("", 0, 0)) | ||
|
||
while stack: | ||
current_combination, open_count, close_count = stack.pop() | ||
|
||
if len(current_combination) == 2 * length: | ||
result.append(current_combination) | ||
|
||
if open_count < length: | ||
stack.append((current_combination + "(", open_count + 1, close_count)) | ||
|
||
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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.