A Rota-Baxter word, \$w\$, is a string made of the symbols a, (, and ) such that the following are satisfied:
- The
(s and)s are balanced. aa,)(and()do not occur as contiguous substrings.- \$w\$ does not begin with a
(and end with a). That is it either begins withaor ends witha. - If
((\$v\$))is a contiguous substring, then \$v\$ is not a balanced string.
For example the following is valid:
a(a)a(a(a)a)
However none of the following are:
((a) -- Parentheses are not balanced
aa(a) -- Illegal substring
a(a)(a) -- Illegal substring
a()a -- Illegal substring
(a)a(a) -- Begins and ends with parentheses
((a(a)))a -- The string "a(a)" is enclosed twice
Task
Given a positive number \$n\$ output the number of Rota-Baxter words containing exactly \$n\$ as. You do not have to output the words themselves.
You may use any default sequence input output methods.
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
Test cases
The terms of the sequence can be found on OEIS: A157418
I've explicitly calculated all the words for up to 4 as
a
(a)a
a(a)
((a)a)a
(a(a))a
a((a)a)
a(a(a))
a(a)a
(((a)a)a)a
((a(a))a)a
((a)a(a))a
(a((a)a))a
(a(a(a)))a
a(((a)a)a)
a((a(a))a)
a((a)a(a))
a(a((a)a))
a(a(a(a)))
a(a(a)a)
(a(a)a)a
(a)a(a)a
a(a)a(a)
a((a)a)a
a(a(a))a
a(a((a)a)a)a(a(a(a))a)avalid? \$\endgroup\$aais invalid substring, otherwise it maps ragged array without len=1(e.g.a(a)a(a(a)a)=>[[],[[],[]],[[],[[],[]],[]]]). What is such definition made for? \$\endgroup\$|aat the end there, otherwise this grammar cannot terminate. I don't think this can generate things of the forma(((a)a(a))a(a)).# -> a@ -> a((#)a@) -> a((#)a(#)) -> a((#)a(a)) -> ???\$\endgroup\$#=a|a@|@a|a@a, $=#|($)a@, @=($)|($)a@\$\endgroup\$