Number of possible strings of length $n$ without three consecutive repetitions

combinatorics

A sequence of $n$ characters, consisting of the characters $A$, $B$, $C$, $D$; how many sequences of length $n$ exist without three consecutive characters being identical?

This problem was part of a set of programming problems whose solutions were based on counting principles.
Input: N
Output: number of strings corresponding to above condition

To simplify the output, we were required to give the result modulo some specifc number as the output, but that's probably an irrelevant detail.

My first thought was inclusion-exclusion, but I couldn't figure out the solution. Can anyone provide any insight?

Example:
Input:
N = 3
Output:
Count = 60
Justification:
4^3 – {$AAA$, $BBB$, $CCC$, $DDD$}
= 64 – 4 = 60

Best Answer

Hint: If $T_n$ is the amount of such sequences of length $n$, $$T_n=3\left(T_{n-1}+T_{n-2}\right),$$ for $n\geq3$.

Related Question