-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Generate parentheses #10903
Generate parentheses #10903
Changes from all commits
35c6c7e
a8603ea
b21b10d
322e45a
89d32bd
2cee13c
4e2e56e
de886cf
336adf2
28d9cfd
b43f475
37718c7
e5ee235
e9403b2
7d7e2e7
f987823
c0e18be
4ed2d72
157c744
6e5c84c
5c3a01f
6b718c5
2c78030
11aa213
3ab1ea6
97d1414
14000b3
0853c35
b01f637
13d806e
778df87
b568f9d
b03426e
aba2ef2
6280be4
896f3a9
abe5fb0
01e6e1c
27022eb
0d8007d
bc5276d
fbc4fa7
b9311e9
14e51db
9da4957
b2f0756
61a2824
ff23cb8
3329261
fb935b2
b7e51c9
aaf1c3b
e268227
6ce845a
7cb9ca2
2eda0a9
e01c863
c9d1d70
f037e2e
e9ac8f6
0d227a3
26643c1
c93ee0c
a1c3658
09aa37f
4042202
b9a7acc
11dc232
380f4e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
author: Aayush Soni | ||
Given n pairs of parentheses, write a function to generate all | ||
combinations of well-formed parentheses. | ||
Input: n = 2 | ||
Output: ["(())","()()"] | ||
Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ | ||
""" | ||
|
||
|
||
def backtrack( | ||
partial: str, open_count: int, close_count: int, n: int, result: list[str] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
) -> None: | ||
""" | ||
Generate valid combinations of balanced parentheses using recursion. | ||
|
||
:param partial: A string representing the current combination. | ||
:param open_count: An integer representing the count of open parentheses. | ||
:param close_count: An integer representing the count of close parentheses. | ||
:param n: An integer representing the total number of pairs. | ||
:param result: A list to store valid combinations. | ||
:return: None | ||
|
||
This function uses recursion to explore all possible combinations, | ||
ensuring that at each step, the parentheses remain balanced. | ||
|
||
Example: | ||
>>> result = [] | ||
>>> backtrack("", 0, 0, 2, result) | ||
>>> result | ||
['(())', '()()'] | ||
""" | ||
if len(partial) == 2 * n: | ||
# When the combination is complete, add it to the result. | ||
result.append(partial) | ||
return | ||
|
||
if open_count < n: | ||
# If we can add an open parenthesis, do so, and recurse. | ||
backtrack(partial + "(", open_count + 1, close_count, n, result) | ||
|
||
if close_count < open_count: | ||
# If we can add a close parenthesis (it won't make the combination invalid), | ||
# do so, and recurse. | ||
backtrack(partial + ")", open_count, close_count + 1, n, result) | ||
|
||
|
||
def generate_parenthesis(n: int) -> list[str]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter:
aayushsoni4 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Generate valid combinations of balanced parentheses for a given n. | ||
|
||
:param n: An integer representing the number of pairs of parentheses. | ||
:return: A list of strings with valid combinations. | ||
|
||
This function uses a recursive approach to generate the combinations. | ||
|
||
Time Complexity: O(2^(2n)) - In the worst case, we have 2^(2n) combinations. | ||
Space Complexity: O(n) - where 'n' is the number of pairs. | ||
|
||
Example 1: | ||
>>> generate_parenthesis(3) | ||
['((()))', '(()())', '(())()', '()(())', '()()()'] | ||
|
||
Example 2: | ||
>>> generate_parenthesis(1) | ||
['()'] | ||
""" | ||
|
||
result: list[str] = [] | ||
backtrack("", 0, 0, n, result) | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n