AfterAcademy Tech
•
18 Jul 2020

Difficulty: Medium
Asked in: Facebook, Microsoft
Problem Description
Given n pairs of parentheses, write a program to generate all combinations of balanced parentheses. You have to return a string array containing all possible cases.
Example:
Input: n = 3
Output: A solution set is
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
( and ) and check for valid ones.( and ) when adding them will guarantee a solution.You may try to solve this problem here.
We can generate all sequences of '(' and ')' characters. Then, we will check if the generated sequence is valid or not.
Through recursion, we can have all the possible sequences of ( and ) . To check if the sequence is valid or not, we can do that by finding the difference between the count of ( and ) . If the difference is not zero, then clearly the sequence becomes invalid and we will discard it else, it would be a valid sequence.
Solution Steps
generateAll and pass a current character array of size n*2 (Why?)current[pos] with ( and go ahead in the recursion tree. and set current[pos] with ) and then go ahead in the recursion tree.n*2 then check if the current character array represents a valid sequence or not. If the string is valid then add into the combinations string array.Pseudo Code
string[] generateParenthesis(int n) {
string[] combinations
char[2*n] current
generateAll(current, 0, combinations)
return combinations
}
// utility function
bool valid(char[] current) {
int balance = 0
for (char c: current) {
if (c == '(')
balance = balance + 1
else
balance = balance - 1
if (balance < 0)
return false
}
return (balance == 0)
}
void generateAll(char[] current, int pos, string[] result) {
if (pos == current.length) {
if (valid(current))
result.add(current);
} else {
current[pos] = '('
generateAll(current, pos+1, result)
current[pos] = ')'
generateAll(current, pos+1, result)
}
}
Complexity Analysis
Time Complexity: O(2^(2n)*n). For each 2^(2n) sequences, we have to validate in O(n) time.
Space Complexity: O(2^(2n)*n), including the recursion stack space.
Critical Ideas To Think
current character array by 2*n?current char array is valid or not?generateAll function, why did we make the recursive call whenever we updated current[pos]?In the previous case, you must have realized that we are adding ) in the current array even though the number of ( is less than the number of ) and one can also point that at any point the number of ) becomes greater than the number of ( then the sequence will be surely invalid.
In this case, we are trying to optimize that by keeping surety that adding ( or ) in the current string will result to be a valid sequence.
It could be simply tracked by keeping the number of ( and ) placed so far. We can start an opening bracket if we still have one (of n) left to place. And we can start a closing bracket if it would not exceed the number of opening brackets.
Solution Steps
current string if open_brackets are less than n or close_bracket are less than open_bracketcurrent string becomes equal to 2*n , add it to the combination result array.Pseudo Code
string[] generateParenthesis(int n) {
string[] combinations
open_bracket_so_far = 0
close_bracked_so_far = 0
backtrack(combinations, "", open_bracket_so_far, close_bracked_so_far, n)
return combinations
}
// utility function
void backtrack(string[] combination, String cur, int open_br_so_far, int close_br_so_far, int n){
if (cur.length() == n * 2) {
combination.add(cur)
return
}
if (open_br_so_far < n) {
backtrack(combination, cur + "(", open_br_so_far + 1, close_br_so_far, n)
}
if (close_br_so_far < open_br_so_far) {
backtrack(combination, cur + ")", open_br_so_far, close_br_so_far + 1, n)
}
}
Complexity Analysis
Time Complexity: O((4^n)/(√n)), in simple terms, the time complexity will be the nth Catalan number.
Space Complexity: O((4^n)/(√n)), including recursion stack space.
Critical Ideas To Think
( or ) to the end of the string and directly add 1 to open_br and close_br - we do not explicitly restore them after each recursion.) in the cur string in backtracking function only when close_br_so_far < open_br_so_far?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
This is a famous interview problem asked in companies like Amazon, Microsoft. Here the basic idea is to think of the data structure which can easily solve this problem.

AfterAcademy Tech
Given a chain <M1, M2...Mn> of n two-dimensional matrices, write a program to fully parenthesize the product M1×M2×⋯×Mn in a way that minimizes the number of multiplications. It's a famous dynamic programming problem.
