Advertisement
smj007

Untitled

Feb 11th, 2024
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. class Solution:
  2.     def generateParenthesis(self, n: int) -> List[str]:
  3.  
  4.         res = []
  5.  
  6.         def func(Open, Close, result):
  7.             # base case
  8.             if Open == Close == n:
  9.                 res.append(result)
  10.  
  11.             # now we can add an open and close paran
  12.             # to the resulting string under certain cond
  13.  
  14.             # add a open paran only the limit is not exceeedde
  15.             if Open < n:
  16.                 func(Open + 1, Close, result + "(")
  17.  
  18.             # add a open paran only if the limit is not exceeded
  19.             # and as long as open paran are greater than close
  20.             if Close < n and Open > Close:
  21.                 func(Open, Close + 1, result + ")")
  22.  
  23.             return
  24.  
  25.         func(0, 0, "")
  26.  
  27.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement