31 lines
767 B
JavaScript
31 lines
767 B
JavaScript
function generateParenthesis(n) {
|
|
const output = []
|
|
const rec = (input = [], left = 0, right = 0) => {
|
|
if (input.length === 2*n) {
|
|
output.push(input.join(''));
|
|
console.log('sol');
|
|
return
|
|
}
|
|
if (left < n) {
|
|
input.push('(');
|
|
console.log(input.join('') + ' l=' + left + ' r=' + right)
|
|
rec(input, left+1, right);
|
|
console.log('back left');
|
|
input.pop();
|
|
}
|
|
|
|
if (right < left) {
|
|
input.push(')');
|
|
console.log(input.join('') + ' l=' + left + ' r=' + right)
|
|
rec(input, left, right+1);
|
|
console.log('back right l=' + left);
|
|
input.pop();
|
|
console.log(input.join('') + ' l=' + left + ' r=' + right)
|
|
}
|
|
};
|
|
rec();
|
|
return output;
|
|
}
|
|
|
|
console.log(generateParenthesis(3));
|