This question is published in multiple textbooks and already has an answer elsewhere on math.SE:
Find a recurrence relation for the number of ternary strings of length $n$ that do not contain two consecutive 0s or two consecutive 1s.
This question above has a recurrence relation $f_{n}=2f_{n-1}+f_{n-2}$.
But the same problem replacing or to and seems to be much more difficult:
Find a recurrence relation for the number of ternary strings of length $n$ that do not contain two consecutive 0s and two consecutive 1s.
I tried to solve it in the way below:
i) starts with 2 -> $a_{n-1}$
ii) starts with 12 or 02 -> $a_{n-2}$
iii) starts with 012 or 102 -> $a_{n-3}$
and so on .
So the relation :$$f_{n}=f_{n-1}+2f_{n-2}+2f_{n-3}+....+f_{0}+2$$ that is $$f_{n}=2f_{n-1}+f_{n-2}$$ I got a same relation as with the original question using or. Please help me to find out where did I go wrong?
So the problem, as I see it, starts shortly after point (iii) above: what about strings that begin with 002? To find those cases, you must find strings of length $n-3$ that do not have 11 in them. This is some amount less than $a_{n-3}/2$, but how much I don't know. (the 112 case is similar)
So to solve this, I'd divide the problem into several classes, find a recurrence relation for each class, and then combine that to obtain a recurrence for the sum.
So then, let's define a few cases:
Note that $a_n = b_n + c_n + d_n$.
Now, for $i = 0, 1, 2$, let $a^i_n$ be those strings be those strings of the kind we're looking to count to make $a_n$ that begin with the digit $i$. So for example, we have: $$a_n = a^0_n + a^1_n + a^2_n$$ Similarly, define $b^i_n$, $c^i_n$, and $d^i_n$.
Now, consider finding a recurrence relation for $b_n$; it's easiest to break that up into recurrence relations for $b^0_n$, $b^1_n$, and $b^2_n$.
Straight from the definitions we get: \begin{align} b^0_n &= b_{n-1} + d^0_{n-1} \\ b^1_n &= b^0_{n-1} + b^2_{n-1} \\ b^2_n &= b_{n-1} \end{align} Remember, we're looking for strings that do contain
00but do not contain11. For such a string to begin with0, that means that the rest of the string either already met our condition ($b_{n-1}$ cases) or was a string with no consecutive 0s or consecutive 1s that started with0($d^0_{n-1}$ cases). For such a string to begin with1, that means that the rest of the string already met our condition and began with a0or a2($b^0_{n-1} + b^2_{n-1}$ cases). For such a string to begin with2, it means only that the rest of the string matched our condition. ($b_{n-1}$ cases)To reduce the number of different series we're dealing with, let's try to eliminate the series $b^i_n$ and leave ourselves with only $b^i_n$ and $d^0_n$ on the right hand sides:
\begin{align} b^0_n &= b_{n-1} + d^0_{n-1} \\ b^1_n &= b_{n-2} + d^0_{n-2} + b_{n-2} \\ b^2_n &= b_{n-1} \end{align}
Therefore: $$b_n = b^0_n + b^1_n + b^2_n = 2b_{n-1} + 2b_{n-2} + d^0_{n-1} + d^0_{n-2}$$ By similar logic, we can derive a formula for $c_n$: $$c_n = 2c_{n-1} + 2c_{n-2} + d^1_{n-1} + d^1_{n-2}$$
Note that we already have a a recurrence relation for $d_n$ from the original problem: $$d_n = 2d_{n-1} + d_{n-2}$$
Also note that $d^2_n = d_{n-1}$ and therefore $$d^0_n + d^1_n = d_n - d^2_n = d_n - d_{n-1}$$ This will come in useful later.
Now then, for $a_n$: \begin{align} a_n &= b_n + c_n + d_n \\ &= 2b_{n-1} + 2b_{n-2} + d^0_{n-1} + d^0_{n-2} \\ & \qquad + 2c_{n-1} + 2c_{n-2} + d^1_{n-1} + d^1_{n-2} \\ & \qquad + 2d_{n-1} + d_{n-2} \\ &= 2a_{n-1} + a_{n-2} + (a_{n-2} - d_{n-2}) + d_{n-1} - d_{n-2} + d_{n-2} - d_{n-3} \\ &= 2a_{n-1} + 2a_{n-2} + d_{n-1} - d_{n-2} - d_{n-3} \\ &= 2a_{n-1} + 2a_{n-2} + (2d_{n-2} + d_{n-3}) - d_{n-2} - d_{n-3} \\ &= 2a_{n-1} + 2a_{n-2} + d_{n-2} \end{align}
Almost there. We can now work to eliminate the $d_{n-2}$ terms, but first notice that this last equation can be rewritten as: $$d_{n-2} = a_n - 2a_{n-1} - 2a_{n-2}$$ And therefore these two equations follow: \begin{align} d_{n-3} &= a_{n-1} - 2a_{n-2} - 2a_{n-3} \\ d_{n-4} &= a_{n-2} - 2a_{n-3} - 2a_{n-4} \end{align} Now, applying the recurrence relation for $d_n$ again: \begin{align} a_n &= 2a_{n-1} + 2a_{n-2} + d_{n-2} \\ &= 2a_{n-1} + 2a_{n-2} + 2d_{n-3} + d_{n-4} \\ &= 2a_{n-1} + 2a_{n-2} + 2a_{n-1} - 4a_{n-2} - 4a_{n-3} + a_{n-2} - 2a_{n-3} - 2a_{n-4} \\ &= 4a_{n-1} - a_{n-2} - 6a_{n-3} - 2a_{n-4} \end{align} So that's the final recurrence relation: $$a_n = 4a_{n-1} - a_{n-2} - 6a_{n-3} - 2a_{n-4}$$ This is something of a mess, but it checks out: \begin{align} a_0 &= 1 \\ a_1 &= 3 \\ a_2 &= 9 \\ a_3 &= 27 \\ a_4 &= 79 \\ a_5 &= 229 \\ a_6 &= 657 \\ a_7 &= 1871 \\ a_8 &= 5295 \end{align}
Those values were calculated with the recurrence relation and verified by the python program: