Description

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Examples

Input:n = 3
Output:["((()))","(()())","(())()","()(())","()()()"]
Explanation:

All valid combinations of 3 pairs of parentheses.

Input:n = 1
Output:["()"]
Explanation:

Only one valid combination.

Input:n = 4
Output:["(((())))","((()()))","((())())","((()))()" ,"(()(()))","(()()())","(()())() ","(())(())","(())()() ","()((()))","()(()())","()(())() ","()()(())","()()()() "]
Explanation:

For 4 pairs of parentheses, there are 14 valid combinations. Each combination uses exactly 4 opening and 4 closing parentheses, with every opening parenthesis properly matched to a closing one, and no closing parenthesis appearing before its corresponding opening parenthesis.

Constraints

  • 1 ≤ n ≤ 8

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!