Determine the number of sequences $(a_1, a_2, . . . , a_n)$, with elements from the set $\{ 0, 1, 2 \}$, satisfying the condition $|a_k - a_{k-1}| \leq 1$ for $k = 2, 3, . . ., n$.
We know that if I define the number of sequences for a given $n$ as $a_n$, then: $$a_n = 3a_{n-1} + 2a_{n-1} + 2a_{n-1} = a_n = 7a_{n-1}$$
The equality holds because if we know that:
- the last element was equal to $1$, then semi-last element could be equal to: $0$, $1$ or $2$, so we have $3$ options
- the last element was equal to $0$, then semi-last element could be equal to: $0$ or $1$, so we have $2$ options
- the last element was equal to $2$, then semi-last element could be equal to: $1$ or $2$, so we have $2$ options
From that we have: $$a_n = k \cdot 7^n$$
We know that $a_1 = 3$, because that one element can be equal to: $0$, $1$ or $2$, so we have $3$ options. Therefore:
$$a_n = \frac{3}{7} \cdot 7^n = 3 \cdot 7^{n-1}$$
Is that correct?
Another solution which is simpler.
Denote $(A_n,B_n,C_n)$ the number of sequences for a given $n$ that begins with $0$, $1$ or $2$. We observe that: $$\begin{align} &A_{n+1} = A_n + B_n\\ &B_{n+1} = A_n + B_n+C_n\tag{1}\\ &C_{n+1} = B_n +C_n\\ \end{align}$$
Denote $P(n)$ the total number of sequences of length $n$, then $P(n) = A_n + B_n +C_n$. From $(1)$, we have:
$$P(n+1) = A_{n+1}+B_{n+1} +C_{n+1} = 2\underbrace{(A_n + B_n+C_n)}_{= P(n)} + \underbrace{B_n}_{= A_{n-1}+B_{n-1} +C_{n-1} = P(n-1)}$$ $$\implies \color{red}{P(n+1) = 2P(n) +P(n-1)} \tag{2}$$ It's easy to solve the quadratic sequence $(2)$: if $x_{1,2}= (1\pm \sqrt{2})/2$ the two solutions of the quadratic equation $x^2 - 2x -1 =0$ then $$P(n) = p_1\cdot x_1^{n}+p_2\cdot x_2 ^n \tag{3}$$
Determine $(p_1,p_2)$ from $(3)$ given that $P(1) = 3$ and $P(2) = 7$, we can show that $$\color{red}{P(n) = \frac{(\sqrt{2}+1)^{n+1}+(1-\sqrt{2})^{n+1}}{2}}$$