JavaScript || How To Find All Combinations Of Well-Formed Brackets Using Vanilla JavaScript
The following is a module with functions which demonstrates how to find all combinations of well-formed brackets.
The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1…n. For example, Brackets(3), the output would be:
()
(()) ()()
((())) (()()) (())() ()(()) ()()()
The number of possible combinations is the Catalan number of N pairs C(n).
1. Find All Well-Formed Brackets
The example below demonstrates the use of ‘Utils.brackets‘ to find all the well-formed bracket combinations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Find All Well-Formed Brackets <script> (() => { let results = Utils.brackets(4); results.forEach((result) => { console.log(`Pair: ${result.pair}, Combination: ${result.combination}`); }); })(); </script> // expected output: /* Pair: 1, Combination: () Pair: 2, Combination: (()), ()() Pair: 3, Combination: ((())), (()()), (())(), ()(()), ()()() Pair: 4, Combination: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()() */ |
2. Utils Namespace
The following is the Utils.js Namespace. Include this in your project to start using!
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 6, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: brackets * USE: Returns all combinations of well-formed brackets from 1...n * @param pairs: The number of bracket combinations to generate. * @param open: Optional. The 'open bracket' symbol. * @param close: Optional. The 'close bracket' symbol. * @return: An array of bracket combination info. */ exposed.brackets = (pairs, open = '(', close = ')') => { let results = []; let symbols = { open: open, close: close, }; for (let pair = 1; pair <= pairs; ++pair) { results.push({ pair: pair, combination: buildBrackets('', 0, 0, pair, symbols), }); } return results; } let buildBrackets = (output, open, close, pair, symbols) => { if ((open === pair) && (close === pair)) { return output; } let result = ''; if (open < pair) { let openCombo = buildBrackets(output + symbols.open, open + 1, close, pair, symbols); if (openCombo.length > 0) { result += (result.length > 0 ? ', ' : '') + openCombo; } } if (close < open) { let closeCombo = buildBrackets(output + symbols.close, open, close + 1, pair, symbols); if (closeCombo.length > 0) { result += (result.length > 0 ? ', ' : '') + closeCombo; } } return result; } (function (factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply