Let $a_n$ (for $n \geq 2$) denote the number of words of length $n$ over a 7-letter alphabet in which each letter is the same as the previous or next one. Find a compact formula for $a_n$.
The conclusion is that each letter must appear in a "group" of the same letters of size 2 or more.
My first approach was by recursion and I got:
$$a_n = 7a_{n-2} + 7a_{n-3}, a_0 = 1, a_1 = 0, a_2 = 7$$
since every string of length $n$ can be created from a string of length $n-2$ by adding two identical letters, or from a string of length $n-3$ by adding three identical letters. We can achieve a group of any length in this way. Each time, we choose the color of the added letters in 7 ways.
The second way is to use stars and bars, dividing into elements $\geq 2$. At the start, we can assign 2 elements to each of the $k$ groups, so we have the standard stars and bars but for $n-2k$ elements:
$$\binom{n-2k+k-1}{k-1} = \binom{n-k-1}{k-1}.$$
Then we sum over all possible numbers of groups and assign a color to each group in 7 ways:
$$a_n = \sum_{k=1}^{\left\lfloor\frac{n}{2}\right\rfloor}\binom{n-k-1}{k-1} \cdot 7^k.$$
There are at most $\left\lfloor\frac{n}{2}\right\rfloor$ groups.
However, first of all, these two solutions give different values, and secondly, I suspect that they count some things more than once, because numbers they give are slightly larger than when manually counting the possibilities.
Is any of these formulas good? If not, what could be the way to find the number of these sequences? Thanks.
The recurrence $a_n=7a_{n-2}+7a_{n-3}$ is incorrect, because it overcounts. For example, $\mathsf{AAAA}$ can be obtained in two ways by your method; either by starting with $\mathsf{AA}$ and appending $\mathsf{AA}$, or starting with $\mathsf{A}$ and appending $\mathsf{AAA}$.
A correct recurrence (which I got from Henry's comment) is $$ a_n=a_{n-1}+6a_{n-2}. \tag{$n\ge 4$}, $$ with the base cases $a_2=a_3=7$. This is because any sequence of length $n$ can be obtained in exactly one of two ways.
You can start with a valid sequence of length $n-1$, and append a copy of the last character. These sequences will all end with a group of $3$ or more, and every sequence ending in a group of $3$ or more is obtained this way.
You can start with a valid sequence of length $n-2$, whose last letter is $x$, and append a pair of identical letters which are unequal to $x$. These sequence end with a group of $2$, and all such sequences can be obtained this way.
For the stars-and-bars method, you are also overcounting. You are correct that the number of ways to partition the spots into $k$ contiguous groups is $\binom{n-k-1}{k-1}$. However, the number of ways to assign numbers to these groups is not $7^k$, but is instead $7\cdot 6^{k-1}$. This is because adjacent groups must receive different numbers (else they would be in the same group). The leftmost group has $7$ choices, but then each subsequent group has only $6$ choices, because each group cannot be assigned the same number as the group on its left. Therefore, the correct expression is $$ 7\sum_{k=1}^{\lfloor n/2\rfloor } \binom{n-k-1}{k-1}6^{k-1} $$
This summation agrees with the recurrence $a_n=a_{n-1}+6a_{n-2}$.