Answers for "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 ')'."

2

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)
Posted by: Guest on May-31-2020

Code answers related to "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 ')'."

Python Answers by Framework

Browse Popular Code Answers by Language