dsa · medium

Generate Parentheses

Return every string of n pairs of correctly matched parentheses. Order of the strings does not matter.

Arguments

A string is correctly matched when every prefix has at least as many ( as ), and the totals are equal at the end.

Example

n = 3["((()))","(()())","(())()","()(())","()()()"] (any order).

Constraints

1 <= n <= 8

Examples

Example 1

Input:
3

Expected:
["((()))","(()())","(())()","()(())","()()()"]

Example 2

Input:
1

Expected:
["()"]

Open in the Dojo editor