Generate Parentheses
Problem statement
Given an integer n, return every balanced string made from exactly n pairs of parentheses.
A string is balanced when each opening parenthesis has a later matching closing parenthesis and no prefix has more closing parentheses than opening parentheses.
Return each string once, in lexicographic order. The character ( comes before ).
Function
generateParenthesis(n: int) → List<String>Examples
Example 1
n = 1return = ["()"]The only balanced string with one pair is ().
Example 2
n = 3return = ["((()))","(()())","(())()","()(())","()()()"]There are five balanced strings with three pairs. Each prefix has at least as many opening parentheses as closing parentheses. The strings are listed in lexicographic order.
Constraints
1 <= n <= 8