Top 150 InterviewEasy

Generate Parentheses

Detailed guide and Python implementation for the 'Generate Parentheses' problem.

Problem Statement

Easy

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

Write a function generateParenthesis(n: int) -> List[str].

Constraints
  • 1 <= n <= 8

Examples

Example 1
Input
n = 3
Output
["((()))", "(()())", "(())()", "()(())", "()()()"]
Explanation

All 5 valid combinations of 3 pairs of parentheses.

Example 2
Input
n = 1
Output
["()"]
Explanation

Only one valid combination with 1 pair.

Need a Hint?
Consider using Stack-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.