Generate Parentheses

interview_workbook/leetcode/stack /app/src/interview_workbook/leetcode/stack/generate_parentheses.py
View Source

Algorithm Notes

Summary: Generate Parentheses — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Generate Parentheses

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

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

Constraints:
    - 1 <= n <= 8
"""

from interview_workbook.leetcode._registry import register_problem
from interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def solve(self, n: int) -> list[str]:
        """Return all combinations of n pairs of valid parentheses."""
        res = []

        def backtrack(s, left, right):
            if len(s) == 2 * n:
                res.append(s)
                return
            if left < n:
                backtrack(s + "(", left + 1, right)
            if right < left:
                backtrack(s + ")", left, right + 1)

        backtrack("", 0, 0)
        return res


def demo():
    return str(Solution().solve(3))


register_problem(
    id=22,
    slug="generate_parentheses",
    title="Generate Parentheses",
    category=Category.STACK,
    difficulty=Difficulty.MEDIUM,
    tags=["stack", "backtracking"],
    url="https://leetcode.com/problems/generate-parentheses/",
    notes="Backtracking with stack constraints. Ensure left>=right always.",
)