LeetcodeJun 18, 2025

Generate Parentheses

Hazrat Ali

Leetcode

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

 

Example 1:

Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]

Example 2:

Input: n = 1
Output: ["()"]


Solution
/**
 * @param {number} n
 * @return {string[]}
 */
const generateParenthesis = n => {
  const results = [];
  backtracking(n, 0, 0, '', results);
  return results;
};

const backtracking = (n, left, right, solution, results) => {
  if (left === n && right === n) {
    results.push(solution);
    return;
  }

  if (left < n) {
    backtracking(n, left + 1, right, solution + '(', results);
  }

  if (right < left) {
    backtracking(n, left, right + 1, solution + ')', results);
  }
};






Comments