Add type hints and improve generate_parentheses_iterative (#14324)

* Add type hints and improve code quality for generate_parentheses_iterative

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix line length issue

* Update generate_parentheses_iterative.py

* Update generate_parentheses_iterative.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
zain-cs
2026-03-09 07:40:39 +05:00
committed by GitHub
parent f527d43d6d
commit 8e70e2e77b

View File

@@ -1,11 +1,12 @@
def generate_parentheses_iterative(length: int) -> list:
def generate_parentheses_iterative(length: int) -> list[str]:
"""
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 ')'.
3. Start with empty string and push it on stack 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.
@@ -34,8 +35,11 @@ def generate_parentheses_iterative(length: int) -> list:
>>> generate_parentheses_iterative(0)
['']
"""
result = []
stack = []
if length == 0:
return [""]
result: list[str] = []
stack: list[tuple[str, int, int]] = []
# Each element in stack is a tuple (current_combination, open_count, close_count)
stack.append(("", 0, 0))
@@ -45,6 +49,7 @@ def generate_parentheses_iterative(length: int) -> list:
if len(current_combination) == 2 * length:
result.append(current_combination)
continue
if open_count < length:
stack.append((current_combination + "(", open_count + 1, close_count))