Talentd
FleetCode

22. Generate Parentheses

Medium79.6% acceptance40 companies

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

Example 1:

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

Example 2:

Input: n = 1
Output: ["()"]

Constraints:

  • 1 <= n <= 8
13:37
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def generateParenthesis(self, n: int) -> List[str]:
3
Starter codePython 3Spaces: 4
Examples
Input
3
Input
1
Your code is saved on this device.Solution