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:
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
def gen(s="", open=0, close=0):
if len(s) is 2 * n:
res.append(s)
else:
if open < n:
gen(s + "(", open + 1, close)
if close < open:
gen(s + ")", open, close + 1)
gen()
return res