Back to all solutions

#22 - Generate Parentheses

Problem Description

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Solution

/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function(n) {
  const result = [];
  backtrack(result, '', 0, 0, n);
  return result;
};

function backtrack(result, str, open, close, max) {
  if (str.length === max * 2) {
    result.push(str);
    return;
  }

  if (open < max) backtrack(result, `${str}(`, open + 1, close, max);
  if (close < open) backtrack(result, `${str})`, open, close + 1, max);
}