def generateParenthesis(self, n: int) -> List[str]:
def helper(current_string, number_open, number_close, n):
# if all parenthesis are opened and closed then nothing to do, return the current string
if number_open == n and number_close == n:
return([current_string])
# if every parenthesis are already opened we only need to close them
elif number_open == n:
return(helper(current_string + ")", number_open, number_close+1, n))
# if the same number of parenthesis are opened and closed we can't close a new one and need to open one
elif number_open == number_close:
return(helper(current_string + "(", number_open+1, number_close, n))
# otherwise we can open a new parenthesis or close an opened parenthesis
else:
return(helper(current_string + "(", number_open+1, number_close, n) +
helper(current_string + ")", number_open, number_close+1, n))
return(helper("", 0, 0, n))