Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution:
# @param an integer
# @return a list of string
def generateParenthesis(self, n):
res = []
if n==0 : return res
self.help(n,n,'',res)
return res
def help(self,l,r,item, res):
if l > r :
return
if l == 0 and r == 0 :
res.append(item)
if l > 0 :
self.help(l-1, r, item + '(', res)
if r > 0 :
self.help(l, r-1, item + ')', res)