From fff5cb82d6ce1ce6933cee8b8c6f3a2eb2ebd7e6 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 14 Oct 2018 18:46:58 -0400 Subject: [PATCH 1/2] Added Generate Parentheses --- GenerateParentheses.js | 54 ++++++++++++++++++++++++++++++++++++++++++ Main.js | 4 +++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 GenerateParentheses.js diff --git a/GenerateParentheses.js b/GenerateParentheses.js new file mode 100644 index 0000000..733c81f --- /dev/null +++ b/GenerateParentheses.js @@ -0,0 +1,54 @@ +/* +https://leetcode.com/problems/generate-parentheses + +Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. + +For example, given n = 3, a solution set is: + +[ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()" +] +*/ + +var generateParentheses = function(n) { + if(n == 0) { return [""] }; + + var str = "(".repeat(n); + var sol = []; + genParAux(str, 0, 0, sol) + return sol; +}; + +var genParAux = function(str, position, leftParentheses, sol) { + if(position === str.length) { + var ret = str + ")".repeat(leftParentheses); + sol.push(ret); + return; + } + + genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything + if(leftParentheses == 0) { return; } + + for(var i = 1; i <= leftParentheses; i++) { + var parString = ")".repeat(i); + var partSol = str.slice(0, position) + parString + str.slice(position); // Insert i parentheses in the position + genParAux(partSol, position + i + 1, leftParentheses - i + 1, sol); + } +} + +function main() { + console.log("0:"); + console.log(generateParentheses(0)); + console.log("1:"); + console.log(generateParentheses(1)); + console.log("2:"); + console.log(generateParentheses(2)); + console.log("3:"); + console.log(generateParentheses(3)); +} + +module.exports.main = main diff --git a/Main.js b/Main.js index b9011fd..a7df501 100644 --- a/Main.js +++ b/Main.js @@ -2,9 +2,11 @@ var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js"); var permutationWithDuplicates = require("./PermutationsWithDuplicates.js"); var subsets = require("./Subsets.js"); +var generateParentheses = require("./GenerateParentheses.js") // Invocation // permutationWithoutDuplicates.main(); // permutationWithDuplicates.main(); -// subsets.main(); \ No newline at end of file +// subsets.main(); +// generateParentheses.main(); \ No newline at end of file From 11225d70f39619c08ee6e1dfbcf5b7d4811484e7 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 14 Oct 2018 19:15:54 -0400 Subject: [PATCH 2/2] Added second approach to parentheses --- GenerateParentheses.js | 68 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/GenerateParentheses.js b/GenerateParentheses.js index 733c81f..9e0c4f1 100644 --- a/GenerateParentheses.js +++ b/GenerateParentheses.js @@ -14,15 +14,24 @@ For example, given n = 3, a solution set is: ] */ -var generateParentheses = function(n) { - if(n == 0) { return [""] }; +// ************************************************ Approach1 ************************************************ +var generateParenthesesApproach1 = function(n) { + if(n === 0) { return [] }; var str = "(".repeat(n); - var sol = []; + sol = []; + genParAux(str, 0, 0, sol) return sol; }; +/* +@param {string} str contains the string generated. +@param {number} position Current position of the string where new parenthesis would be added. +@param {string} leftParenthesis Amount for parenthesis left to be added. +@param {[string]} sol array that contains the solution found so far. +*/ + var genParAux = function(str, position, leftParentheses, sol) { if(position === str.length) { var ret = str + ")".repeat(leftParentheses); @@ -31,7 +40,7 @@ var genParAux = function(str, position, leftParentheses, sol) { } genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything - if(leftParentheses == 0) { return; } + if(leftParentheses === 0) { return; } for(var i = 1; i <= leftParentheses; i++) { var parString = ")".repeat(i); @@ -40,15 +49,48 @@ var genParAux = function(str, position, leftParentheses, sol) { } } -function main() { - console.log("0:"); - console.log(generateParentheses(0)); - console.log("1:"); - console.log(generateParentheses(1)); - console.log("2:"); - console.log(generateParentheses(2)); - console.log("3:"); - console.log(generateParentheses(3)); +// ************************************************ Approach2 ************************************************ +var generateParenthesesApproach2 = function(n) { + if(n === 0) { return [] }; + + var sol = []; + genParAuxApproach2("", 0, 0, 0, n * 2, sol) + return sol; +} + +var genParAuxApproach2 = function(str, leftPar, rightPar, index, totalCharCount, sol) { + if(index === totalCharCount) { + if(rightPar === leftPar) { + sol.push(str); + } + return; + } + + var strLeft = insertAt(str, index, "("); + genParAuxApproach2(strLeft, leftPar + 1, rightPar, index + 1, totalCharCount, sol); + + if(rightPar === leftPar) { return; } + + var strRight = insertAt(str, index, ")"); + genParAuxApproach2(strRight, leftPar, rightPar + 1, index + 1, totalCharCount, sol); +} + +var insertAt = function(str, position, value) { + return str.slice(0, position) + value + str.slice(position); } +function main() { + console.log("Approach 1"); + [0, 1, 2, 3].forEach(function(elem) { + console.log(`${elem}: ${generateParenthesesApproach2(elem)}`); + }) + + console.log("-------------"); + + console.log("Approach 2"); + [0, 1, 2, 3].forEach(function(elem) { + console.log(`${elem}: ${generateParenthesesApproach2(elem)}`); + }) +} +main(); module.exports.main = main