Generate Parentheses - aloalgo

Generate Parentheses

Medium

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

A well-formed parenthesis string satisfies two conditions:

  1. The total number of opening parentheses must be equal to the total number of closing parentheses.
  2. For any prefix of the string, the number of opening parentheses must be greater than or equal to the number of closing parentheses.

For example, ((())) is well-formed, but ()) is not.

Example 1

Input
1
Output
["()"]
Explanation:

With one pair, only one well-formed combination is possible.

Example 2

Input
3
Output
["((()))", "(()())", "(())()", "()(())", "()()()"]
Explanation:

All possible well-formed combinations for three pairs of parentheses.

Loading...
Input
1
Output
["()"]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!