A brace string is defined as a string consisting of the characters *()[]
in which braces match correctly:
[brace-string] ::= [unit] || [unit] [brace-string]
[unit] ::= "" || "*" || "(" [brace-string] ")" || "[" [brace-string] "]"
This is a valid brace-string:
((())***[]**)****[(())*]*
But these are not:
)(
**(**[*](**)
**([*)]**
Your task is to write a program (or function) that, given a positive integer n
, takes a number as input and outputs (or returns) all valid brace strings of length n
.
Specifications
- You may output the strings in any order.
- You may output as a list or a string separated by a different character.
- Your program must handle 0 correctly. There is 1 possible brace-string of length 0, which is the empty string
""
. - This is code-golf, so the shortest valid answer – measured in bytes – wins.
Test Cases
0.
1. *
2. ** () []
3. *** ()* []* (*) [*] *() *[]
4. **** ()** []** (*)* [*]* (**) **() **[] *(*) *[*] (()) ()() ()[] ([]) [**] [()] [[]] []() [][] *()* *[]*