-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added generate parenthesis algorithm #10068
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
Closed
Closed
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
faf2975
Added new file Generate Parenthesis
Jibesh10101011 60fc394
Update generate_parenthesis.py
Jibesh10101011 98d7c27
Update generate_parenthesis.py
Jibesh10101011 b0a0f67
Update generate_parenthesis.py
Jibesh10101011 a69d5eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5ecc461
Update generate_parenthesis.py
Jibesh10101011 45eea59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 76a64a0
Update generate_parenthesis.py
Jibesh10101011 d6dd070
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a3efa4f
Update generate_parenthesis.py
Jibesh10101011 4aaeba7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e0c1e18
Update generate_parenthesis.py
Jibesh10101011 43cb929
Update generate_parenthesis.py
Jibesh10101011 5cacd54
Update generate_parenthesis.py
Jibesh10101011 1fa5f05
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 78d2503
Update generate_parenthesis.py
Jibesh10101011 e5e0efa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
def solve(op: str, open: int, close: int, ans: list[str]) -> None: | ||
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if open == 0 and close == 0: | ||
ans.append(op) | ||
return | ||
if open == close: | ||
op1: str = op | ||
op1 += "(" | ||
solve(op1, open - 1, close, ans) | ||
elif open == 0: | ||
op1: str = op | ||
op1 += ")" | ||
solve(op1, open, close - 1, ans) | ||
elif close == 0: | ||
op1: str = op | ||
op1 += "(" | ||
solve(op1, open - 1, close, ans) | ||
else: | ||
op1: str = op | ||
op2: str = op | ||
op1 += "(" | ||
op2 += ")" | ||
solve(op1, open - 1, close, ans) | ||
solve(op2, open, close - 1, ans) | ||
|
||
|
||
def generate_parenthesis(n: int) -> list[str]: # n = no. of pairs of parenthesis > 0 | ||
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
Jibesh10101011 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> generate_parenthesis(3) | ||
['((()))', '(()())', '(())()', '()(())', '()()()'] | ||
>>> generate_parenthesis(4) | ||
['(((())))', '((()()))', '((())())', '((()))()', '(()(()))', '(()()())', '(()())()', '(())(())', '(())()()', '()((()))', '()(()())', '()(())()', '()()(())', '()()()()'] | ||
>>> generate_parenthesis(5) | ||
['((((()))))', '(((()())))', '(((())()))', '(((()))())', '(((())))()', '((()(())))', '((()()()))', '((()())())', '((()()))()', '((())(()))', '((())()())', '((())())()', '((()))(())', '((()))()()', '(()((())))', '(()(()()))', '(()(())())', '(()(()))()', '(()()(()))', '(()()()())', '(()()())()', '(()())(())', '(()())()()', '(())((()))', '(())(()())', '(())(())()', '(())()(())', '(())()()()', '()(((())))', '()((()()))', '()((())())', '()((()))()', '()(()(()))', '()(()()())', '()(()())()', '()(())(())', '()(())()()', '()()((()))', '()()(()())', '()()(())()', '()()()(())', '()()()()()'] | ||
""" | ||
open: int = n | ||
close: int = n | ||
ans: list[str] = [] | ||
op: str = "" | ||
solve(op, open, close, ans) | ||
if n > 0: | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.