How many words we can make from $0,1,2$? The restriction is we can't put the digit $2$ after the digit $2$.
My solution:
I tried to solve it with Inclusion-Exclusion Principle. Count the number of the words without restriction and then I decreased the options that I don't want. The number of ways to make words without restriction $3 \cdot 3 \cdot 3 \cdot 3 \cdot 3 \cdot \ldots \cdot 3$, so this is $3^n$. And then the number that I don't want to count is: $$\binom {n-1}{2} - \binom {n-2}{3} + \cdots + (-1)^n \cdot \frac{ (n-(n+1)}{(i+1)!}$$ so my solution is $$3^n - \sum_{i=1}^n-1 \frac{(-1)^n \cdot (n-i)}{(i+1)!}$$ This is not the answer. Can someone help me? What did I do wrong? Does the problem have any solution with a recurrence relation??
Thanks for your help!
This problem is best approached through a recurrence relation.
Let $a_n$ denote the number of permissible sequences of length $n$.
No sequence of length $1$ can have two consecutive $2$'s. Since there are three ways the first position can be filled, $a_1 = 3$.
The only sequence of length $2$ that has two consecutive $2$'s is $22$. Since each of the two positions can be filled in two ways, $a_2 = 3 \cdot 3 - 1 = 8$.
Notice that any permissible sequence of length $n$ can be extended to a sequence of length $n + 1$ by appending a $0$ or a $1$ to the end of the permissible sequence of length $n$.
We can only have a permissible sequence of length $n + 1$ that ends in a $2$ if the $n$th digit is a $0$ or a $1$. Hence, any permissible sequence of length $n - 1$ can be extended to a permissible sequence of length $n + 1$ by appending $02$ or $12$ to the end of the permissible sequence of length $n - 1$.
Hence, we have the recurrence relation \begin{align*} a_1 & = 3\\ a_2 & = 8\\ a_{n + 1} & = 2a_n + 2a_{n - 1}, n \geq 2 \end{align*}
If I am interpreting your attempt correctly, you are trying to eliminate those sequences with two or more consecutive $2$'s. However, you have only excluded those sequences in which all the $2$'s are consecutive. They can also appear in disjoint pairs. For instance, both the sequences $12220$ and $22122$ have two pairs of consecutive $2$'s. The consecutive pairs overlap in $12220$ and are disjoint in $22122$. Trying to count pairs of consecutive $2$'s becomes trickier as $n$ increases, which is why I approached the problem through a recurrence relation.