I encountered the problem of generating all combinations of valid parenthesis in a given length, when there is only one type of parenthesis - ( and ).
The solution I wrote is:
def generate(n, open, close, seq):
i = open + close
if i >= 2 * n:
print(seq)
return
if open < n:
generate(n, open + 1, close, seq + "(")
if close < open:
generate(n, open, close + 1, seq + ")")
I am trying to figure out it this can be extended to three different parenthesis, (), [], {}, when expression is valid when it is balanced and contains exactly n parenthesis of each type (and thus, the length of the expression is 6n).
I thought about passing more arguments (n, open1, close1, open2, close2, open3, close3), but the calls cannot keep track of the last open parenthesis. I tried adding another argument which stores the last type, but this allows me only to remember the last type.
Can it be done without using a data structure and only use recursion?