Pages

Wednesday 2 August 2023

LeetCode - 22 - Generate Parentheses

 /**

 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function(n) {
    var output = [];
    getAllParenthis(n, n, "", output);
    return output;
};

function getAllParenthesis(opening, closing, comb, output) {
if (opening == 0 && closing == 0) { output.push(comb); return } if (opening < closing) { getAllParenthesis(opening, closing -1, comb + ")", output);
} if (opening > 0) { getAllParenthesis(opening - 1, closing, comb + "(", output);
} }

No comments:

Post a Comment