Perl 6, 36 bytes
{grep {try !.EVAL},[X~] <[ ]>xx$_*2}
Finds all lexographically sorted combinations of \$2n\$ []
s and filters the ones that EVAL
correctly. Note that all valid combinations (even stuff like [][]
) evaluate to []
(which is falsey, but we not
it (!
) to distinguish from try
returning Nil
)
Explanation:
{ } # Anonymous code block
<[ ]> # Create a list of ("[", "]")
xx$_*2 # Duplicate this 2n times
[X~] # Find all possible combinations
grep { }, # Filter from these
.EVAL # The EVAL'd strings
try ! # That do not throw an error