|
| 1 | +/* |
| 2 | +https://leetcode.com/problems/generate-parentheses |
| 3 | +
|
| 4 | +Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. |
| 5 | +
|
| 6 | +For example, given n = 3, a solution set is: |
| 7 | +
|
| 8 | +[ |
| 9 | + "((()))", |
| 10 | + "(()())", |
| 11 | + "(())()", |
| 12 | + "()(())", |
| 13 | + "()()()" |
| 14 | +] |
| 15 | +*/ |
| 16 | + |
| 17 | +// ************************************************ Approach1 ************************************************ |
| 18 | +var generateParenthesesApproach1 = function(n) { |
| 19 | + if(n === 0) { return [] }; |
| 20 | + |
| 21 | + var str = "(".repeat(n); |
| 22 | + sol = []; |
| 23 | + |
| 24 | + genParAux(str, 0, 0, sol) |
| 25 | + return sol; |
| 26 | +}; |
| 27 | + |
| 28 | +/* |
| 29 | +@param {string} str contains the string generated. |
| 30 | +@param {number} position Current position of the string where new parenthesis would be added. |
| 31 | +@param {string} leftParenthesis Amount for parenthesis left to be added. |
| 32 | +@param {[string]} sol array that contains the solution found so far. |
| 33 | +*/ |
| 34 | + |
| 35 | +var genParAux = function(str, position, leftParentheses, sol) { |
| 36 | + if(position === str.length) { |
| 37 | + var ret = str + ")".repeat(leftParentheses); |
| 38 | + sol.push(ret); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything |
| 43 | + if(leftParentheses === 0) { return; } |
| 44 | + |
| 45 | + for(var i = 1; i <= leftParentheses; i++) { |
| 46 | + var parString = ")".repeat(i); |
| 47 | + var partSol = str.slice(0, position) + parString + str.slice(position); // Insert i parentheses in the position |
| 48 | + genParAux(partSol, position + i + 1, leftParentheses - i + 1, sol); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// ************************************************ Approach2 ************************************************ |
| 53 | +var generateParenthesesApproach2 = function(n) { |
| 54 | + if(n === 0) { return [] }; |
| 55 | + |
| 56 | + var sol = []; |
| 57 | + genParAuxApproach2("", 0, 0, 0, n * 2, sol) |
| 58 | + return sol; |
| 59 | +} |
| 60 | + |
| 61 | +var genParAuxApproach2 = function(str, leftPar, rightPar, index, totalCharCount, sol) { |
| 62 | + if(index === totalCharCount) { |
| 63 | + if(rightPar === leftPar) { |
| 64 | + sol.push(str); |
| 65 | + } |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + var strLeft = insertAt(str, index, "("); |
| 70 | + genParAuxApproach2(strLeft, leftPar + 1, rightPar, index + 1, totalCharCount, sol); |
| 71 | + |
| 72 | + if(rightPar === leftPar) { return; } |
| 73 | + |
| 74 | + var strRight = insertAt(str, index, ")"); |
| 75 | + genParAuxApproach2(strRight, leftPar, rightPar + 1, index + 1, totalCharCount, sol); |
| 76 | +} |
| 77 | + |
| 78 | +var insertAt = function(str, position, value) { |
| 79 | + return str.slice(0, position) + value + str.slice(position); |
| 80 | +} |
| 81 | + |
| 82 | +function main() { |
| 83 | + console.log("Approach 1"); |
| 84 | + [0, 1, 2, 3].forEach(function(elem) { |
| 85 | + console.log(`${elem}: ${generateParenthesesApproach2(elem)}`); |
| 86 | + }) |
| 87 | + |
| 88 | + console.log("-------------"); |
| 89 | + |
| 90 | + console.log("Approach 2"); |
| 91 | + [0, 1, 2, 3].forEach(function(elem) { |
| 92 | + console.log(`${elem}: ${generateParenthesesApproach2(elem)}`); |
| 93 | + }) |
| 94 | +} |
| 95 | +main(); |
| 96 | +module.exports.main = main |
0 commit comments