Older solution that doesn't work quite right
spoiler
This time I did it in JavaScript. Overall, it solves it in a reasonable amount of time up to n = 16, but won't at n = 18 and up.
const BRACKETS = [['(', ')'], ['[', ']'], ['{', '}']];
function brackets(n) {
if (n === 1) return ['()', '[]', '{}'];
let o = {};
function addBracket(s) {
BRACKETS.forEach(b => {
o[b[0] + b[1] + s] = true;
o[b[0] + s + b[1]] = true;
o[s + b[0] + b[1]] = true;
});
}
brackets(n - 1).forEach(addBracket);
return Object.keys(o);
}
brackets(Number(process.argv[2]) / 2).forEach(v => console.log(v));
I don't feel experienced enough with data structures and algorithms to make this more efficient. I really just started learning this stuff and don't have a great grasp of it yet. I could of probably used a set to save some lines instead of a hashmap, but eh, its probably slightly faster because I went the hashmap route to get rid of duplicates.
I revised it because I was pointed out I missed an aspect of the problem. This is in JavaScript
const BRACKETS = ['()', '[]', '{}'];
function brackets(n) {
if (n === 0) return [''];
let strings = brackets(n - 1);
let o = {};
for (let s of strings) {
for (let i = 0; brackets.length >= i; i++) {
for (let b of BRACKETS) {
o[s.slice(0, i) + b + s.slice(i)] = true;
}
}
}
return Object.keys(o);
}
brackets(Number(process.argv[2]) / 2).forEach(v => console.log(v));