Update appNew update is available. Click here to update.

Balanced parentheses

profile
Contributed by
Deepanshu_1780
Medium
yellow-spark
0/80
10 mins
90 %
11 upvotes
IntuitGoldman SachsFlipkart
+54 more

Problem Statement

Given an integer β€˜N’ representing the number of pairs of parentheses, Find all the possible combinations of balanced parentheses with the given number of pairs of parentheses.

Note :

Conditions for valid parentheses:
1. All open brackets must be closed by the closing brackets.

2. Open brackets must be closed in the correct order.

For Example :

()()()() is a valid parentheses.
)()()( is not a valid parentheses.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 5
1 <= N <= 10
Time Limit : 1 sec.
Sample Input 1 :
2
2
3
Sample Output 1 :
(()) 
()()
((())) 
(()()) 
(())()
()(())
()()()
Explanation for Sample Input 1 :
Test Case 1: 

For 2 pair of parentheses the combinations of balanced parentheses will be (()) ()()

Test Case 2:

For 3 pair of parentheses the combinations of balanced parentheses will be 
((())) (()()) (())() ()(()) ()()()
Sample Input 2 :
2
1
4
Sample Output 2 :
()
(((()))) 
((()())) 
((())()) 
((()))() 
(()(())) 
(()()()) 
(()())() 
(())(()) 
(())()() 
()((())) 
()(()()) 
()(())() 
()()(()) 
()()()()
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console