Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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 />");
- Array.prototype.push.apply(results, getBalancedParens(pairs, openRem, closeRem - 1, current + ')', indent + 1));
- }
- // BASE CASE
- document.write(".".repeat(indent) + "2nd base case. Returning " + results + "<br />");
- return results;
- }
- document.write("All combinations of 3 balanced parentheses:<br />");
- document.write("Results: ", getBalancedParens(3));
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement