Generate Parentheses
Application of Backtracking

Customer Support: admin@thealgorists.com

Feedback:
We are listening to your every feedback,
and taking action to constantly improve your learning experience.
If you have any feedback, please use this form:
https://thealgorists.com/Feedback.
 If you are a student, email to admin@thealgorists.com to get special student discount.
Problem Statement:
Given n pairs of parentheses, write a function to generate all combinations of wellformed parentheses.
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
Solution:
 NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rocksolid understanding.
If you are familiar with our backtracking template then you would be easily come up with a quick solution for this problem just by making the below two observations:
We need to ask ourselves two questions:
 When can we add an opening brace "(" to a given empty or nonempty partial solution ?
 When can we add a closing brace ")" to a given empty or nonempty partial solution ?
Looking at the patterns carefully, we can conclude:

We always have room for appending one more opening brace "("
to a given partial solution
as long as partial solution contains less than n opening braces.
"(" can be added to an empty partial solution.
Keep in mind we cannot have more than n opening braces in our solution.

We can append closing brace ")" to our partial solution
only when we have less number of closing braces than opening braces in our current partial solution.
")" cannot be added to an empty partial solution.
This is a Premium content. Please subscribe to access the code.
After subscribing please come back and refresh this page.
Time Complexity:
From
backtrack( ... )
method we recursively call itself O(2) times. We start from
partialSolution.length() == 1
and the recursion call returns when partialSolution.length() == 2 * n
.
So we have O(2 * n)
recursion calls. Therefore, overall time complexity = O(2^{(2 * n)}) = O(4^{n}).
n = value of the parameter n passed in generateParenthesis(int n).
Space Complexity:
From
backtrack( ... )
method we recursively call itself O(2) times. We start from
partialSolution.length() == 1
and the recursion call returns when partialSolution.length() == 2 * n
.
So we have O(2 * n)
recursion calls.
So, at any point of time maximum number of recursion call present in the call stack = O(2^{(2 * n)}) = O(4^{n})
Space Complexity = O(4^{n})
n = value of the parameter n passed in generateParenthesis(int n).
Don't forget to take indepth look at the other backtracking problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:
 Letter Case Permutation
 Power Set
 All Paths Between Two Nodes
 Word Search
 Sudoku
 NQueens
 Word Square
The above content is written by:
Abhishek Dey
A Visionary Software Engineer With A Mission To Empower Every Person & Every Organization On The Planet To Achieve More
Microsoft  University of Florida
If you have any feedback, please use this form: https://thealgorists.com/Feedback.
Follow Us On LinkedIn