Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def generateParenthesis(self, n: int) -> List[str]:
- res = []
- def func(Open, Close, result):
- # base case
- if Open == Close == n:
- res.append(result)
- # now we can add an open and close paran
- # to the resulting string under certain cond
- # add a open paran only the limit is not exceeedde
- if Open < n:
- func(Open + 1, Close, result + "(")
- # add a open paran only if the limit is not exceeded
- # and as long as open paran are greater than close
- if Close < n and Open > Close:
- func(Open, Close + 1, result + ")")
- return
- func(0, 0, "")
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement