Skip to content

Instantly share code, notes, and snippets.

@connecttobn
Last active October 31, 2018 06:55
Show Gist options
  • Save connecttobn/7610f0810857f39c44de162573824e0f to your computer and use it in GitHub Desktop.
Save connecttobn/7610f0810857f39c44de162573824e0f to your computer and use it in GitHub Desktop.
var cleverBrace = function(prefix, suffix, n, result) {
if (n % 2 !== 0) return;
if (n == 2) {
var val = prefix + "()" + suffix;
if (result.indexOf(val) === -1) {
result.push(val);
}
} else if (n > 2) {
cleverBrace(prefix + "(", ")" + suffix, n - 2, result);
cleverBrace(prefix + "()", suffix, n - 2, result);
cleverBrace(prefix, "()" + suffix, n - 2, result);
}
}
//Test cases
for (var i = 2; i < 10; i += 2) {
console.log("When n = " + i);
var res = [];
cleverBrace("", "", i, res);
console.log(res.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment