Generate Parenthesis

August 29, 2023

DSA & Leetcodes

Generate Parenthesis

Thuta Sann

Thuta Sann

solution to generate Parenthesis for the given number.


Share This Snippet To :

Generate Parenthesis

Give `n` paris of parentheses, write a function to generate all combinations of well-formed parentheses.

/** Generate Parenthesis * @example * if n === 3 => ((( ))) * leftPCount would be 3 * rightPCount would be 3 * partial = '' * @recurisve * leftPCount = 2 * rightPCount = 3 * partial^1 = '(' * partial^1 = ')' */ function generateParenthesis(n: number): Array<string> { const solution: Array<string> = []; /** * Build Combo Recurive function * @param { number } leftPCount - Left Parenthesis Count * @param { number } rightPCount - Right Parenthesis Count * @param { number } partial - Partial string */ const buildCombo = ( leftPCount: number, rightPCount: number, partial: string, ) => { if (leftPCount > rightPCount) return; if (!leftPCount && !rightPCount) solution.push(partial); if (leftPCount > 0) buildCombo(leftPCount - 1, rightPCount, partial + '('); if (rightPCount > 0) buildCombo(leftPCount, rightPCount - 1, partial + ')'); }; buildCombo(n, n, ''); return solution; } const ParenthesisAnswer = generateParenthesis(3); console.log('ParenthesisAnswer', ParenthesisAnswer);

Cookie

I baked some cookies that you have to accept, if you want to enjoy my portfolio.
In order to gather information and make improvements, I should use some third-party cookies too, Can I?