Balanced parentheses
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.
Input format :
The first line of input contains an integer ‘T’, which denotes the number of test cases. Then each test case follows.
Each line of the test case contains an integer ‘N’ denoting the pair of parentheses.
Output format :
For each test case print, all the combinations of balanced parentheses separated by a single space.
The output of each test case will be printed on a separate line.
Note:
1. You don't need to print anything, it has already been taken care of. Just implement the given function.
2. You can return strings in any order.
Constraints:
1 <= T <= 5
1 <= N <= 10
Time Limit : 1 sec.
CodingNinjas
author
2y
Approach :
1) First make a recursive function, say ‘solve’ taking the number of opening brackets ‘opening’, number of closing brackets ‘closing’ output string ‘output’, and an array of strings ‘ans’ a...read more
CodingNinjas
author
2y
Recursion
The idea is to generate all possible combinations and check whether the combination is the combination of balanced parentheses or not. We have two choices whether to consider ‘(‘ or ‘)’.
When ...read more
Help your peers!
Add answer anonymously...
Top Salesforce Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Salesforce Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app