Skip to content
BASA DSA

Generate Parentheses

MediumBacktracking

Return every distinct balanced parentheses string containing n pairs, in any order.

Example 1

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

Explanation

Three pairs produce five strings covering nested, mixed and separate pairs.

Example 2

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

Explanation

Two pairs can nest as (()) or sit side by side as ()().

Example 3

Input
n = 1
Output
["()"]

Explanation

One pair has only the balanced string ().

Constraints

  • 1 <= n <= 8

Function contractgenerateParenthesisArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…