mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00

* adding generate-parenthses algorithm * adding generateParenthses algorithm * adding generate parentheses algorithm * fixing comments according to the JDoc comments, cleaning code * fixing comments
32 lines
810 B
JavaScript
32 lines
810 B
JavaScript
/**
|
|
* Problem Statement: Given a number n pairs of parentheses, try to Generate all combinations of valid parentheses;
|
|
* @param {number} n - number of given parentheses
|
|
* @return {string[]} res - array that contains all valid parentheses
|
|
* @see https://leetcode.com/problems/generate-parentheses/
|
|
*/
|
|
|
|
const generateParentheses = (n) => {
|
|
const res = []
|
|
|
|
const solve = (chres, openParenthese, closedParenthese) => {
|
|
if (openParenthese === n && closedParenthese === n) {
|
|
res.push(chres)
|
|
return
|
|
}
|
|
|
|
if (openParenthese <= n) {
|
|
solve(chres + '(', openParenthese + 1, closedParenthese)
|
|
}
|
|
|
|
if (closedParenthese < openParenthese) {
|
|
solve(chres + ')', openParenthese, closedParenthese + 1)
|
|
}
|
|
}
|
|
|
|
solve('', 0, 0)
|
|
|
|
return res
|
|
}
|
|
|
|
export { generateParentheses }
|