Generate Parentheses
Given n, the number of pairs of parentheses, return every string of n opening brackets "(" and n closing brackets ")" that is balanced.
A string is balanced when every ")" closes an earlier, still-open "(": reading left to right, the number of closing brackets never exceeds the number of opening ones, and at the end they are equal. Return each balanced string once, in any order.
Constraints
1 <= n <= 8
Examples
Example 1
Input: n = 3 Output: ["()()()","()(())","(())()","(()())","((()))"]
There are 5 balanced strings of 3 pairs. They may be returned in any order.
Example 2
Input: n = 1 Output: ["()"]
One pair can only be written one way.
Example 3
Input: n = 2 Output: ["()()","(())"]
"(())" nests the pairs, "()()" puts them side by side. ")(()" is not balanced: it closes before anything is open.
JavaScript you'll use: Recursion, the call stack and memoization Strings and characters
/**
* @param {number} n
* @return {string[]}
*/
function generateParenthesis(n) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.