Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. code example

Example 1: Given an integer 'n'. Print all the possible pairs of 'n' balanced parentheses. The output strings should be printed in the sorted order considering '(' has higher value than ')'.

def generateParentheses(openBr, closeBr, n, s = []):
	if closeBr == n:
		print(''.join(s))
		return 
	
	if closeBr < openBr:
		s.append(')')
		generateParentheses(openBr, closeBr+1, n, s)
		s.pop()
	if openBr < n:
		s.append('(')
		generateParentheses(openBr+1, closeBr, n, s)
		s.pop()
	return

n = int(input())
generateParentheses(0, 0, n)

Example 2: Given an integer A pairs of parentheses, write a function to generate all combinations of well-formed parentheses of length 2*A.

# Python3 program to
# Print all combinations
# of balanced parentheses

# Wrapper over _printParenthesis()
def printParenthesis(str, n):
	if(n > 0):
		_printParenthesis(str, 0,
						n, 0, 0);
	return;

def _printParenthesis(str, pos, n,
					open, close):
	
	if(close == n):
		for i in str:
			print(i, end = "");
		print();
		return;
	else:
		if(open > close):
			str[pos] = '}';
			_printParenthesis(str, pos + 1, n,
							open, close + 1);
		if(open < n):
			str[pos] = '{';
			_printParenthesis(str, pos + 1, n,
							open + 1, close);

# Driver Code
n = 3;
str = [""] * 2 * n;
printParenthesis(str, n);