Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Generate Parentheses

Medium
backtrackingstringrecursion

Given n, generate all combinations of n pairs of well-formed parentheses.

Input: one line, the integer n. Output: one combination per line. Any order — the template prints canonically.

Example 1
Input:
3
Output:
((()))
(()())
(())()
()(())
()()()
Example 2
Input:
1
Output:
()

Constraints

  • 1 ≤ n ≤ 8
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.