Let $a_n$ be such sequence of $7$ letters, such that every letter is equal to previous or next letter in that sequence. My idea is to find recursive formula for $a_n$ and then to write generating function for $a_n$. From that I can find general formula for $a_n$. I have two cases:
– If $a_{n-1}$ ends with two same letters, then I can put any letter at $n$-th position.
– If $a_{n-1}$ ends with two different letters, then at $n$-th position I can put only one letter (same as at $(n-1)$-th position).
So I have recursive formula:
$a_n = 7 \cdot (\text{number of sequences $a_{n-1}$ such that two last letters are the same}) + (\text{number of sequences $a_{n-1}$ such that two last letters are different} )$
I don't where to go from here. Thanks in advance for any clues.
I think there is a slight problem with the recurrence, because the last letter always has to be equal to the previous one.
So to "fix" this we count a slightly different set of sequences that can be extended more naturally. Let $f_n$ be the number of sequences such that every letter except the last one must be equal to one of its neighbours.
We wish to find a recurrence for $f_n$.
The number of sequences that end in two consecutive equal ones is $f_{n-1}$.
The number of sequences that do not end in two consecutives is $ 6 \cdot f_{n-2}$.
To see this notice that every sequence counted in $f_{n-2}$ can be extended in exactly $6$ ways to a sequence of length $n$ where the last two are not equal, this is because the term $n-1$ must be equal to the term $n-2$.
Hence we get $f_n = f_{n-1} + 6f_{n-2}$.
We have $f_1 = 7$ and $f_2 = 7$.
It follows : $f_3 = 49,f_4 = 91, f_5 = 385, f_6 = 931$
Your answer is $f_6=931$.
C++ bruteforce code to check the answer to the original problem (which should equal $f_{n-1}$ for $n\geq 2$)