From ad4bbd66b7152e2b8a7bee1c31f9bb4993374a7c Mon Sep 17 00:00:00 2001 From: nightmare10123 Date: Sat, 7 Oct 2023 20:34:52 +0530 Subject: [PATCH 1/3] Generate parantheses iterative --- .../generate_parentheses_iterative.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 backtracking/generate_parentheses_iterative.py diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py new file mode 100644 index 000000000000..4f54e125b1da --- /dev/null +++ b/backtracking/generate_parentheses_iterative.py @@ -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() From eeb413133002a6e7045eab2818bafe81b57cd880 Mon Sep 17 00:00:00 2001 From: nightmare10123 Date: Sat, 7 Oct 2023 20:42:14 +0530 Subject: [PATCH 2/3] Generate parantheses iterative --- backtracking/generate_parentheses_iterative.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index 4f54e125b1da..4fdbb80ed117 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -14,16 +14,16 @@ 5. Return the result containing all valid combinations. Args: - n (int): The desired length of the parentheses combinations. + n (int): The desired length of the parentheses combinations Returns: - list: A list of strings representing valid combinations of parentheses. + list: A list of strings representing valid combinations of parentheses Time Complexity: - O(2^(2n)). + O(2^(2n)) Space Complexity: - O(2^(2n)). + O(2^(2n)) """ From 9428fa3d6ea7b6c9a3bb0b615312689596811410 Mon Sep 17 00:00:00 2001 From: nightmare10123 Date: Sat, 7 Oct 2023 21:19:17 +0530 Subject: [PATCH 3/3] Generating parantheses code using iterative approach --- .../generate_parentheses_iterative.py | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index 4fdbb80ed117..9c9b4313dbe7 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -1,44 +1,47 @@ """ - Generate all valid combinations of parentheses (Iterative Approach). + >>> generate_parentheses_iterative(1) + ['()'] + + >>> generate_parentheses_iterative(0) + [''] + +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 ')'. +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. +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 +Args: + n (int): The desired length of the parentheses combinations - Returns: - list: A list of strings representing valid combinations of parentheses +Returns: + list: A list of strings representing valid combinations of parentheses - Time Complexity: - O(2^(2n)) +Time Complexity: + O(2^(2n)) - Space Complexity: - O(2^(2n)) +Space Complexity: + O(2^(2n)) """ -def generate_parentheses(n: int) -> list: +def generate_parentheses_iterative(length: int = 0) -> list: """ - >>> generate_parentheses(3) + >>> generate_parentheses_iterative(3) ['()()()', '()(())', '(())()', '(()())', '((()))'] - >>> generate_parentheses(2) + >>> generate_parentheses_iterative(2) ['()()', '(())'] - >>> generate_parentheses(1) - ['()'] - - >>> generate_parentheses(0) + >>> generate_parentheses_iterative() [''] """ @@ -51,10 +54,10 @@ def generate_parentheses(n: int) -> list: while stack: current_combination, open_count, close_count = stack.pop() - if len(current_combination) == 2 * n: + if len(current_combination) == 2 * length: result.append(current_combination) else: - if open_count < n: + 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)) @@ -62,12 +65,8 @@ def generate_parentheses(n: int) -> list: return result -def main() -> None: - print(generate_parentheses(4)) - - if __name__ == "__main__": import doctest doctest.testmod() - main() + print(generate_parentheses_iterative(3))