Skip to content

Generate Parentheses

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Given n pairs of parentheses, return all well-formed strings of length 2n. A string is well-formed when every ( is closed by a later ) and no ) appears without an open ( before it. Return them in any order.

n = 3  →  ["((()))", "(()())", "(())()", "()(())", "()()()"]
n = 1  →  ["()"]

Constraints

  • 1 <= n <= 8