Let $a(n)$ be the number of sequences with length $n$ which consists the digits $0,1,2$ such that between every two occurrences of $2$ there is an occurrence of $0$ (not necessarily next to the $2$'s). An example for good sequence is $0102102$. An example for bad sequence is $01212$.
A. Find a recursion formula for $a(n)$
B. Find an explicit expression for $a(n)$.
My try: let $x(n)$ be the number of sequences with length $n$ that if we add $2$ at the end of sequence it is still a good sequence with length $n+1$.
Now, if the last digit in the sequence was $0$, then we have to look on the number of good sequences with length $n-1$, hence $\displaystyle a(n-1)$.
If the last digit was $1$, then we now need to look on $x(n-1)$ and work with the same manipulation. This way we get the recursion $x(n)=a(n-1)+x(n-1)$. By induction I can get a recursion formula for $x(n)$, but I don't know how to find a formula for $a(n)$ or find an explicit expression for $a(n)$.
Any help will be appreciated, thank you!
$x(n) = x(n-1) + x(n-1) + \sum_{i=1}^{n-1}x(n-1 -i) + x(0), n\ge 2 $
$x(0)=1$ $, x(1)=3$.
Any valid string either start with $0$ (number of such strings are $x(n-1)$) or $1$ (number of such strings are $x(n-1))$ or $2$.If it starts with $2$ then it must have only $1's$ before first occurrence of $0$. Condition on the position of first $0$ after the $2$. It can be next to it, or one $1$ followed by $0$ an so on and the case that occurrence of $2$ is followed by only $1's$ (number of such strings are $x(0))$.
Edit
More Simpler Recurrence
Using the recurrence above subtract $x(n-1)$ from $x(n)$ to get this simpler recurrence, $$x(n)= 3x(n-1)-x(n-2), n \ge 2 $$
You can use this formula to compute the closed formula easily.