Skip to content
Decoder

Generate Parentheses

Medium

Problem

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.

Example 1

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

Example 2

Input
n = 1
Output
["()"]

Constraints

  • 1 <= n <= 8

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.