Skip to interview prep
Apexed

51. Generate Parentheses

MediumRecursion & backtracking

Return every string of n pairs of well-formed parentheses, sorted lexicographically ("(" before ")").

Example 1

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

Constraints

  • 1 ≤ n ≤ 8

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 3 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Generate Parentheses — coding interview practice — Apexed