Skip to content
Onsite

Generate All Parentheses

MediumP1Coding

Given n pairs of parentheses, return all strings of length 2 * n that are well-formed: every ( is closed by a matching ) in the correct order. Return the strings in any order.

Example 1

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

Example 2

Input
n = 1
Output
["()"]

Constraints

  • 1 <= n <= 8

Hints

Run checks the 3 visible tests, Submit all 7.