-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbalancedParentheses.html
46 lines (42 loc) · 1.48 KB
/
balancedParentheses.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<script type="text/javascript">
function getBalancedParens(pairs, openRem, closeRem, current, indent) {
if (openRem === undefined) {
openRem = pairs;
}
if (closeRem === undefined) {
closeRem = pairs;
}
if (current === undefined) {
current = "";
}
if (indent === undefined) {
indent = 0;
}
document.write(".".repeat(indent) + "Start of pairs=" +
pairs + ", openRem=" + openRem + ", closeRem=" +
closeRem + ", current=\"" + current + "\"<br />");
if (openRem === 0 && closeRem === 0) {
// BASE CASE
document.write(".".repeat(indent) +
"1st base case. Returning " + [current] + "<br />");
return [current];
}
// RECURSIVE CASE
let results = [];
if (openRem > 0) {
document.write(".".repeat(indent) + "Adding open parenthesis.<br />");
Array.prototype.push.apply(results, getBalancedParens(
pairs, openRem - 1, closeRem, current + '(', indent + 1));
}
if (closeRem > openRem) {
document.write(".".repeat(indent) + "Adding close parenthesis.<br />");
results = results.concat(getBalancedParens(
pairs, openRem, closeRem - 1, current + ')', indent + 1));
}
// BASE CASE
document.write(".".repeat(indent) + "2nd base case. Returning " + results + "<br />");
return results;
}
document.write("<pre>All combinations of 2 balanced parentheses:<br />");
document.write("Results: " + getBalancedParens(2) + "</pre>");
</script>