Balanced Parentheses Generator

Recursion Backtracking Balanced Parentheses

Problem: Balanced Parentheses Generator

Write a function generateParentheses(n) that returns all combinations of well-formed parentheses for a given number of pairs n. You must implement your solution using a recursive approach.

Instructions

  • Define a function generateParentheses(n) where n is the number of pairs of parentheses.
  • Use recursion (and optionally backtracking) to generate all valid combinations of balanced parentheses.
  • Return the result as a list (or array) of strings.

Example

For n = 3, one possible output is:

["((()))", "(()())", "(())()", "()(())", "()()()"]

Note

In celebration of the upcoming summer and the balance between work and leisure, enjoy this recursive challenge to balance your code as well!