Generate Parentheses

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

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

A parentheses string is well-formed if every opening parenthesis ( has a corresponding closing parenthesis ) and the pairs are properly nested.

Examples

Example 1:

Input: n = 3

Output: ["((()))","(()())","(())()","()(())","()()()"]

Explanation: All 5 valid arrangements of 3 pairs of parentheses.

Example 2:

Input: n = 1

Output: ["()"]

Explanation: The only valid arrangement of 1 pair of parentheses.

Example 3:

Input: n = 2

Output: ["(())","()()"]

Explanation: Both valid arrangements of 2 pairs of parentheses.

Constraints

  • 1 <= n <= 8
Source: Subsets pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle