You will be given a (possibly empty) string containing brackets ([{()}]
) and any other characters (A
-Z
, a
-z
, 0
-9
, punctuation). You need to check if it adheres to the following rules:
- Non-bracket characters are ignored.
- Every open bracket
[{(
has a closing bracket)}]
. So[](
is not allowed. - Brackets are nested properly.
[(])
is not allowed. - Curly brackets cannot contain square brackets within them. Simple brackets cannot contain either curly or square brackets within them. So
[({})]
,[{[]}]
and({})
are not allowed. Brackets can be nested with similar brackets, so[[{((()))}{{(())}}]()]{()}
is allowed.
Output is a single truthy/falsey value as per your choice.
Shortest code wins.
Test cases
b[[a{(/)}(())+={{}-}],]
-> Valid
([h][e][l][l][o])
-> Invalid
[///[{(\/(arg()))}1{{((-)-2)}}]()]{()}
-> Valid
hi
-> Valid