Dodomeki
Sign in

96. Generate All Parentheses

Medium

Given n, generate all valid sequences with exactly n pairs of parentheses. Return the sequences in increasing lexicographic order.

Example 1
Input: n = 2
Output: ["(())", "()()"]
Example 2
Input: n = 1
Output: ["()"]
Constraints
  • 1 <= n <= 8
BacktrackingRecursion
Caso 1
n =
2
Expected =
["(())","()()"]
Caso 2
n =
1
Expected =
["()"]

Run checks these 2 cases. Submit runs all 6, including hidden ones.