For example, if the input is 3, then the possible combinations of 3 pairs of parenthesis, namely: ()()(), are ()()(), ()(()), (())(), ((())), and (()()). There are 5 total combinations when the input is 3, so your program should return 5. I try using Javascript like this :
function BracketCombinations(num) {
function calculatePOssibilities (open, closed) {
if (open === 0 && closed === 0)
return 1;
var res = 0;
if (open > 0)
res += calculatePOssibilities(open - 1, closed);
if (closed > open)
res += calculatePOssibilities(open, closed - 1);
return res;
}
// code goes here
return calculatePOssibilities(num, num);
}
I know, i can just transform that code to Go, but how to make it more efficient?