Generate Parentheses

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

Example 1
Inputn = 3
Output["((()))","(()())","(())()","()(())","()()()"]
There are five distinct well-formed combinations using three pairs of parentheses.
Example 2
Inputn = 1
Output["()"]
With one pair of parentheses, the only valid combination is ().

Constraints

  • 1 <= n <= 8

Asked at 40 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate