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

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate