Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 11225d7

Browse files
Added second approach to parentheses
1 parent fff5cb8 commit 11225d7

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

GenerateParentheses.js

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@ For example, given n = 3, a solution set is:
1414
]
1515
*/
1616

17-
var generateParentheses = function(n) {
18-
if(n == 0) { return [""] };
17+
// ************************************************ Approach1 ************************************************
18+
var generateParenthesesApproach1 = function(n) {
19+
if(n === 0) { return [] };
1920

2021
var str = "(".repeat(n);
21-
var sol = [];
22+
sol = [];
23+
2224
genParAux(str, 0, 0, sol)
2325
return sol;
2426
};
2527

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+
2635
var genParAux = function(str, position, leftParentheses, sol) {
2736
if(position === str.length) {
2837
var ret = str + ")".repeat(leftParentheses);
@@ -31,7 +40,7 @@ var genParAux = function(str, position, leftParentheses, sol) {
3140
}
3241

3342
genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything
34-
if(leftParentheses == 0) { return; }
43+
if(leftParentheses === 0) { return; }
3544

3645
for(var i = 1; i <= leftParentheses; i++) {
3746
var parString = ")".repeat(i);
@@ -40,15 +49,48 @@ var genParAux = function(str, position, leftParentheses, sol) {
4049
}
4150
}
4251

43-
function main() {
44-
console.log("0:");
45-
console.log(generateParentheses(0));
46-
console.log("1:");
47-
console.log(generateParentheses(1));
48-
console.log("2:");
49-
console.log(generateParentheses(2));
50-
console.log("3:");
51-
console.log(generateParentheses(3));
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);
5280
}
5381

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();
5496
module.exports.main = main

0 commit comments

Comments
 (0)