Generate Parentheses

MediumRecursion & Backtracking
Next
BacktrackingStringRecursion

Given n pairs of parentheses, print every well-formed combination of them, one per line, in lexicographic order (where ( comes before )).

Input

A single integer n.

Output

One valid string of length 2n per line, in lexicographic order.

Constraints

1 <= n <= 8

Example 1
Input
3
Output
((()))
(()())
(())()
()(())
()()()

Five valid strings, from ((())) to ()()().

Hints

Generate Parentheses - Medium Recursion & Backtracking Problem | Skill Shiksha