Skip to content
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

Merged
merged 69 commits into from
Oct 29, 2023
Merged
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
35c6c7e
Add: Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
a8603ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b21b10d
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
322e45a
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
89d32bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
2cee13c
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
4e2e56e
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
de886cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
336adf2
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
28d9cfd
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
b43f475
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
37718c7
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
e5ee235
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
e9403b2
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
7d7e2e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
f987823
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
c0e18be
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
4ed2d72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
157c744
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
6e5c84c
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
5c3a01f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
6b718c5
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
2c78030
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
11aa213
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
3ab1ea6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
97d1414
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
14000b3
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
0853c35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b01f637
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
13d806e
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
778df87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b568f9d
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
b03426e
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
aba2ef2
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
6280be4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
896f3a9
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
abe5fb0
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
01e6e1c
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
27022eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
0d8007d
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
bc5276d
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
fbc4fa7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b9311e9
Changes made in Matrix Prefix Sum
aayushsoni4 Oct 23, 2023
14e51db
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
9da4957
Add: Distinct Subsequences
aayushsoni4 Oct 23, 2023
b2f0756
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
61a2824
Changes made in Distinct Subsequences
aayushsoni4 Oct 23, 2023
ff23cb8
Changes made in Distinct Subsequences
aayushsoni4 Oct 23, 2023
3329261
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
fb935b2
Changes made in Distinct Subsequences
aayushsoni4 Oct 23, 2023
b7e51c9
Merge branch 'master' of https://github.com/aayushsoni4/Python
aayushsoni4 Oct 23, 2023
aaf1c3b
Changes made in Distinct Subsequences
aayushsoni4 Oct 23, 2023
e268227
Changes made in Distinct Subsequences
aayushsoni4 Oct 23, 2023
6ce845a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
7cb9ca2
Removed Distinct Subsequences
aayushsoni4 Oct 23, 2023
2eda0a9
Removed Distinct Subsequences
aayushsoni4 Oct 23, 2023
e01c863
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
c9d1d70
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
f037e2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
e9ac8f6
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
0d227a3
Merge branch 'Generate_Parentheses' of https://github.com/aayushsoni4…
aayushsoni4 Oct 24, 2023
26643c1
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
c93ee0c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
a1c3658
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
09aa37f
Merge branch 'Generate_Parentheses' of https://github.com/aayushsoni4…
aayushsoni4 Oct 24, 2023
4042202
Add: Generate Parentheses
aayushsoni4 Oct 24, 2023
b9a7acc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
11dc232
Update backtracking/generate_parentheses.py
cclauss Oct 29, 2023
380f4e4
Delete matrix/matrix_prefix_sum.py
cclauss Oct 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions backtracking/generate_parentheses.py
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]

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

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

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

) -> 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]:

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

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

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

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

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

"""
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()