From df55c95fe1d50f4f57998aa1d7305baf5efdaa67 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Sat, 27 Oct 2018 16:16:16 -0400 Subject: [PATCH] Update 22 Generate Parentheses.js --- 22 Generate Parentheses.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/22 Generate Parentheses.js b/22 Generate Parentheses.js index 9e4e028..c3b0254 100644 --- a/22 Generate Parentheses.js +++ b/22 Generate Parentheses.js @@ -22,4 +22,22 @@ var generate = function(result, output, depth, n, left, right){ if(left > right){ generate(result,output + ')',depth + 1, n, left, right+1); } -} \ No newline at end of file +} + +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); +}