Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
ad4bbd6
Generate parantheses iterative
nightmare10123 Oct 7, 2023
eeb4131
Generate parantheses iterative
nightmare10123 Oct 7, 2023
9428fa3
Generating parantheses code using iterative approach
nightmare10123 Oct 7, 2023
f027cc1
Update generate_parentheses_iterative.py
MaximSmolskiy Aug 27, 2025
43a3fa7
Merge branch 'master' into fix/generate_parentheses_iterative_approach
MaximSmolskiy Aug 27, 2025
6c84846
updating DIRECTORY.md
MaximSmolskiy Aug 27, 2025
95e9aa1
Update generate_parentheses_iterative.py
MaximSmolskiy Aug 27, 2025
d2d83c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2025
4bee601
Update generate_parentheses_iterative.py
MaximSmolskiy Aug 27, 2025
8510873
Update generate_parentheses_iterative.py
MaximSmolskiy Aug 27, 2025
dda3067
Update generate_parentheses_iterative.py
MaximSmolskiy Aug 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Generate parantheses iterative
  • Loading branch information
nightmare10123 committed Oct 7, 2023
commit ad4bbd66b7152e2b8a7bee1c31f9bb4993374a7c
73 changes: 73 additions & 0 deletions backtracking/generate_parentheses_iterative.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
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*n, add it to the result.
c. If open count is < n, push new combination with added '(' onto the stack.
d. If close count < open count, push new combination with added ')' on stack.
5. Return the result containing all valid combinations.

Args:
n (int): The desired length of the parentheses combinations.

Returns:
list: A list of strings representing valid combinations of parentheses.

Time Complexity:
O(2^(2n)).

Space Complexity:
O(2^(2n)).
"""


def generate_parentheses(n: int) -> list:
"""
>>> generate_parentheses(3)
['()()()', '()(())', '(())()', '(()())', '((()))']

>>> generate_parentheses(2)
['()()', '(())']

>>> generate_parentheses(1)
['()']

>>> generate_parentheses(0)
['']

"""
result = []
stack = []

# Each element in stack has 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 * n:
result.append(current_combination)
else:
if open_count < n:
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


def main() -> None:
print(generate_parentheses(4))


if __name__ == "__main__":
import doctest

doctest.testmod()
main()

AltStyle によって変換されたページ (->オリジナル) /