From c954633aeee2d727628dff56ca3d5080019fa0a8 Mon Sep 17 00:00:00 2001 From: zain-cs Date: Mon, 2 Mar 2026 23:09:32 +0500 Subject: [PATCH 1/3] Add type hints and improve code quality for generate_parentheses_iterative --- backtracking/generate_parentheses_iterative.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index 175941c7ae95..e7b06ce4d5ac 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -1,11 +1,11 @@ -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 +34,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 +48,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)) From 6753f91ef2dd1cc75184f230422022ed3bf69ae4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Mar 2026 18:15:29 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/generate_parentheses_iterative.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index e7b06ce4d5ac..b000605a2950 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -36,9 +36,9 @@ def generate_parentheses_iterative(length: int) -> list[str]: """ if length == 0: return [""] - - result : list[str] = [] - stack : list[tuple[str, int, int]] = [] + + 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)) From 6fdefcfcbbab1b52cdfa83b98d64b0617d4df216 Mon Sep 17 00:00:00 2001 From: zain-cs Date: Mon, 2 Mar 2026 23:29:36 +0500 Subject: [PATCH 3/3] Fix line length issue --- backtracking/generate_parentheses_iterative.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index e7b06ce4d5ac..659ad4ff74b9 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -5,7 +5,7 @@ def generate_parentheses_iterative(length: int) -> list[str]: 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 on stack along with the counts of '(' and ')'. + 3. Start with empty string and push it on stack 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.