Last active
October 31, 2018 06:55
-
-
Save connecttobn/7610f0810857f39c44de162573824e0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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