To start with an example of such a code can be: $34321210123212343210$
I have no clue how this property can be mathematically counted. I actually even have a short solution of this question which I could not make any sense of. I will type the solution at the bottom of this body, one may choose to solve it themselves first and then refer the solution.
I could notice some obvious properties that if there occurs the digit $4,$ it must have $3$ on both sides. And similarly the digit $0$ must have $1$ on both sides.
I am not able to think any way to solve this problem. Please help me with an explanatory solution. Thank you in advance.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
...............................................................................................................................................
SOLUTION: Let $A_n$ denote number of codes which end in $0$ or $4,$ $B_n$ denote number of codes which end in $1$ or $3$ and $C_n$ denote number of codes which ends with $2.$ $A_{n+1} = B_n = C_{n+1}, B_{n+1} = A_n + 2C_n$ Thus $N= 2^3\cdot 3^9$
Here's a way to frame the problem using linear algebra:
Let $A_{i,n}$ denote the number of valid codes that start with $i$ (which can be $0,1,2,3,$ or $4$) and have length $n$. We note that a valid code that begins with a $0$ of length $n$ must be a $0$ followed by a valid code that begins with $1$ of length $n-1$. Similarly, a valid code that begins with $1$ of length $n$ must be a $1$ followed by a valid code that begins with $2$, or a $1$ followed by a valid code that begins with a $0$.
Putting all these observations together, we find that the $A_{i,n}$ satisfy the following system of recurrences: $$ A_{0,n} = A_{1,n-1}\\ A_{1,n} = A_{0,n-1} + A_{2,n-1}\\ A_{2,n} = A_{1,n-1} + A_{3,n-1}\\ A_{3,n} = A_{2,n-1} + A_{4,n-1}\\ A_{4,n} = A_{3,n-1} $$ for any integer $n > 1$. That is: for integers $n>1$, we have $$ \pmatrix{A_{0,n}\\A_{1,n}\\A_{2,n}\\A_{3,n}\\A_{4,n}}= \pmatrix{0&1&0&0&0\\ 1&0&1&0&0\\ 0&1&0&1&0\\ 0&0&1&0&1\\ 0&0&0&1&0} \pmatrix{A_{0,n-1}\\A_{1,n-1}\\A_{2,n-1}\\A_{3,n-1}\\A_{4,n-1}} $$ With $A_{i,1} = 1$ for all $i$. With this recursive characterization, we see that we can calculate the desired quantity as $$ A_{0,n} + \cdots + A_{4,n} = \pmatrix{1&\cdots&1}\pmatrix{A_{0,n}\\A_{1,n}\\A_{2,n}\\A_{3,n}\\A_{4,n}}\\ = \pmatrix{1&1&1&1&1} \pmatrix{0&1&0&0&0\\ 1&0&1&0&0\\ 0&1&0&1&0\\ 0&0&1&0&1\\ 0&0&0&1&0}^{n-1} \pmatrix{1\\1\\1\\1\\1} $$
This leads us to the following answer: let $M$ denote the matrix $$ M = \pmatrix{0&1&0&0&0\\ 1&0&1&0&0\\ 0&1&0&1&0\\ 0&0&1&0&1\\ 0&0&0&1&0} $$ We note that $M$ is symmetric, tridiagonal and Toeplitz (German wiki link). Thus, we find that its eigenvalues and eigenvectors have the form $$ \lambda_k = -2 \cos \left( \frac{\pi k}{6}\right), \quad k = 1,\dots,5\\ v^{(k)} = \pmatrix{\sin\left(\frac{\pi k}{6}\right) & \cdots & \sin\left(\frac{5\pi k}{6}\right)}^T, \quad k = 1,\dots,5 $$ Let $Q$ denote the matrix $Q = \pmatrix{v^{(1)} & \cdots & v^{(5)}}$, and let $y$ denote the vector $y = \pmatrix{1&1&1&1&1}^T$. Let $x = \pmatrix{x_1 & \cdots & x_5}^T$ denote the solution to the equation $Qx = y$. Once we compute the solution $x$ to the above equation, we can answer our original question with the formula $$ N_{n} = y^TM^{n-1}y = \sum_{k=1}^n x_k^2 \lambda_k^{n-1} = \\ -2^{n-1} x_1^2 \cos^{n-1}\left( \frac{\pi}{6}\right) -2^{n-1} x_2^2 \cos^{n-1}\left( \frac{2\pi}{6}\right) - \cdots -2^{n-1} x_5^2 \cos^{n-1}\left( \frac{5\pi}{6}\right) $$ It is useful to note in the above that $\cos(3 \pi /6) = 0$.
It is notable that, because $M$ has characteristic polynomial $p(x) = x^5 - 4x^3 + 3x$, the sequence $N_n$ itself satisfies the linear recurrence $$ N_n = 4N_{n-2} - 3N_{n-4}, \qquad n \geq 6 $$
Computation with W|A indicates that our solution is $N_{20} = 157464$, which can indeed be factored as $N_{20} = 2^3 \cdot 3^9$.
Another approach: starting with the original recurrences and substituting the equations into themselves yields $$ A_{0,n} = A_{0,n-2} + A_{2,n-2}\\ A_{1,n} = 2A_{1,n-2} + A_{3,n-2}\\ A_{2,n} = A_{0,n-2} + 2A_{2,n-2} + A_{4,n-2}\\ A_{3,n} = A_{1,n-2} + 2A_{3,n-2}\\ A_{4,n} = A_{2,n-2} + A_{4,n-2} $$ If you prefer matrix calculation, this amount to the observation that $$ \mathbf{A}_n = M^2 \mathbf A_{n-2} $$
However, with the symmetry of the problem we observe that $A_{1,n} = A_{3,n}$ and $A_{0,n} = A_{4,n}$, which means that we can simplify the above system to $$ A_{0,n} = A_{0,n-2} + A_{2,n-2}\\ A_{1,n} = 3A_{1,n-2}\\ A_{2,n} = 2A_{0,n-2} + 2A_{2,n-2}\\ $$ It follows that $$ N_{2n} = 2A_{0,n} + 2A_{1,n} + A_{2,n}\\ = 2(A_{0,n-2} + A_{2,n-2}) + 2(3A_{1,n-2}) + (2A_{0,n-2} + 2A_{2,n-2})\\ = 3(2A_{0,n-2} + 2A_{2,n-2} + A_{2,n-2}) = 3 N_{n-2} $$ Calculating $N_2 = 8$ leads us to the conclusion that $$ N_{2n} = 8 \cdot 3^{n-1} $$ which allows us to easily calculate $N_{20}$.