Advertisement
imashutosh51

Generate all valid parenthesis of length k

Jul 23rd, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. def fun(open,close,cur,ans):
  2.     if open==0 and close==0:
  3.         ans.add(cur)
  4.     if open>0:
  5.         fun(open-1,close,cur+'(',ans)
  6.     if close>open:
  7.         fun(open,close-1,cur+')',ans)
  8.    
  9. class Solution:
  10.     def generateParenthesis(self, n: int) -> List[str]:
  11.         ans=set()
  12.         fun(n,n,'',ans)
  13.         return list(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement