Problem 50

Generate Parentheses Recursion Challenge

Given a non-negative integer n, return an array of all valid (balanced) combinations of n pairs of parentheses. Do not include duplicate combinations.

Function Signature

generateParentheses(n)

Parameters

  • n — a non-negative integer representing the number of parenthesis pairs.

Output

Return an array of strings, each being a valid arrangement of n pairs of parentheses.

Constraints

  • The function must use recursion.
  • Only balanced combinations should be included.
  • No duplicate combinations.
  • The function should accept exactly one argument.

Examples

generateParentheses(0) → ['']
generateParentheses(1) → ['()']
generateParentheses(2) → ['(())', '()()']
generateParentheses(3) → ['((()))', '(()())', '(())()', '()(())', '()()()']