Skip to content

Commit cc22d0b

Browse files
aayushsoni4pre-commit-ci[bot]cclauss
authored
Generate parentheses (TheAlgorithms#10903)
* Add: Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Matrix Prefix Sum * Add: Distinct Subsequences * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Distinct Subsequences * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes made in Distinct Subsequences * Changes made in Distinct Subsequences * Changes made in Distinct Subsequences * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed Distinct Subsequences * Add: Generate Parentheses * Add: Generate Parentheses * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add: Generate Parentheses * Add: Generate Parentheses * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add: Generate Parentheses * Add: Generate Parentheses * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update backtracking/generate_parentheses.py * Delete matrix/matrix_prefix_sum.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 403d3b8 commit cc22d0b

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

backtracking/generate_parentheses.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
author: Aayush Soni
3+
Given n pairs of parentheses, write a function to generate all
4+
combinations of well-formed parentheses.
5+
Input: n = 2
6+
Output: ["(())","()()"]
7+
Leetcode link: https://leetcode.com/problems/generate-parentheses/description/
8+
"""
9+
10+
11+
def backtrack(
12+
partial: str, open_count: int, close_count: int, n: int, result: list[str]
13+
) -> None:
14+
"""
15+
Generate valid combinations of balanced parentheses using recursion.
16+
17+
:param partial: A string representing the current combination.
18+
:param open_count: An integer representing the count of open parentheses.
19+
:param close_count: An integer representing the count of close parentheses.
20+
:param n: An integer representing the total number of pairs.
21+
:param result: A list to store valid combinations.
22+
:return: None
23+
24+
This function uses recursion to explore all possible combinations,
25+
ensuring that at each step, the parentheses remain balanced.
26+
27+
Example:
28+
>>> result = []
29+
>>> backtrack("", 0, 0, 2, result)
30+
>>> result
31+
['(())', '()()']
32+
"""
33+
if len(partial) == 2 * n:
34+
# When the combination is complete, add it to the result.
35+
result.append(partial)
36+
return
37+
38+
if open_count < n:
39+
# If we can add an open parenthesis, do so, and recurse.
40+
backtrack(partial + "(", open_count + 1, close_count, n, result)
41+
42+
if close_count < open_count:
43+
# If we can add a close parenthesis (it won't make the combination invalid),
44+
# do so, and recurse.
45+
backtrack(partial + ")", open_count, close_count + 1, n, result)
46+
47+
48+
def generate_parenthesis(n: int) -> list[str]:
49+
"""
50+
Generate valid combinations of balanced parentheses for a given n.
51+
52+
:param n: An integer representing the number of pairs of parentheses.
53+
:return: A list of strings with valid combinations.
54+
55+
This function uses a recursive approach to generate the combinations.
56+
57+
Time Complexity: O(2^(2n)) - In the worst case, we have 2^(2n) combinations.
58+
Space Complexity: O(n) - where 'n' is the number of pairs.
59+
60+
Example 1:
61+
>>> generate_parenthesis(3)
62+
['((()))', '(()())', '(())()', '()(())', '()()()']
63+
64+
Example 2:
65+
>>> generate_parenthesis(1)
66+
['()']
67+
"""
68+
69+
result: list[str] = []
70+
backtrack("", 0, 0, n, result)
71+
return result
72+
73+
74+
if __name__ == "__main__":
75+
import doctest
76+
77+
doctest.testmod()

0 commit comments

Comments
 (0)