Question: How many words of length $ n $ are there consisting of letters $ A $, $ B $, $ C $ such that:
- At least one letter occurs an even (possibly zero) number of times
- At least one letter occurs an odd number of times
By writing a simple Python script verifying all possible $ \{A, B, C\}\text{*} $ words of length $ n $, I got the following sequence:
3, 6, 21, 60, 183, 546, 1641, 4920, 14763
Which led me to OEIS sequence A054878. In the formula section there is a neat recurrence relation:
$$ a_{n} = 2a_{n-1} + 3a_{n-2} $$
The $ 3a_{n-2} $ part is clear to me, as we count these words like so:
$$ \underbrace{AA\underbrace{XX...XX}_{\text{n - 2}}}_{\text{n}} + \underbrace{BB\underbrace{XX...XX}_{\text{n - 2}}}_{\text{n}} + \underbrace{CC\underbrace{XX...XX}_{\text{n - 2}}}_{\text{n}}$$
Since adding two of the same letter doesn't impact parity. But what about the $ 2a_{n-1} $ part? Or maybe there is an easier way to solve the problem?
There are only two possibilities for any such sequence length $n$:
If $a_{(o,o,e)}(n)$ is the number of sequences of the first type and $a_{(o,e,e)}(n)$ is the number of sequences of the second type then the number of desired sequences $a(n)$ is:
$$a(n)=a_{(o,o,e)}(n)+a_{(o,e,e)}(n)\tag{1}$$
Then each of these have individual recurrences. For the first term on the right hand side of $(1)$:
$$a_{(o,o,e)}(n)=2a_{(o,e,e)}(n-1)+3a_{(o,o,e)}(n-2)\tag{2i}$$
Since a sequence of type $(o,o,e)$ either ends with an "odd" letter $o$ ($2$ possibilities) then the remaining sequence is of type $(o,e,e)$, this accounts for the first term.
The sequences of type $(o,o,e)$ ends with an "even" letter in two ways: two "evens" $ee$ ($1$ possibility) then the remaining sequence is of type $(o,o,e)$, or ends with "odd" and "even" letters $oe$ ($2$ possibilities) then the remaining sequence is of type $(o,o,e)$, this accounts for the second term.
We can reason similarly for the second term on the right hand side of $(1)$:
$$a_{(o,e,e)}(n)=2a_{(o,o,e)}(n-1)+3a_{(o,e,e)}(n-2)\tag{2ii}$$
Since a sequence of type $(o,e,e)$ either ends with an "even" letter $e$ ($2$ possibilities) then the remaining sequence is of type $(o,o,e)$, this accounts for the first term.
The sequences of type $(o,e,e)$ ends with an odd letter in two ways: two "odds" $oo$ (1 possibility) then the remaining sequence is of type $(o,e,e)$, or ends with "even" and "odd" letters $eo$ ($2$ possibilities) then the remaining sequence is of type $(o,e,e)$, this accounts for the second term.
So, using $(2\text{i})$ and $(2\text{ii})$ in $(1)$ we get:
$$\begin{align}a(n)&=a_{(o,o,e)}(n)+a_{(o,e,e)}(n)\\ &=2(a_{(o,e,e)}(n-1)+a_{(o,o,e)}(n-1))+3(a_{(o,o,e)}(n-1)+a_{(o,e,e)}(n-2))\\ &=2a(n-1)+3a(n-2)\end{align}$$