Dr. Lizardo writes down a sequence of $n$ letters, where each letter is A, B, or C, and the letter A appears an even number of times. How many different sequences could Dr. Lizardo have written down?
What I have tried so far
The last letter is predefined in terms of "A or not" based on the first $n-1$ letters. If there are an odd number of As so far, the last one has to be A, otherwise, B or C. Therefore, there are $3^{n-1}$ ways to write the first $n-1$ letters. However, the last letter can either have 1 choice or 2 choices.
I am thinking maybe we need to do $S_n = 3^{n-1} + S_{n-1}$. However, I need a closed form, not a recurrance. How can I do this?
Thank you!
Note: $S_n = 3^{n-1} + S_{n-1}$ seems to be working for the first few values of $n$ (I tested through $n=4$), so finding a closed form of that recurrance would work as well.
Consider two cases.
Case 1. The sequence is all C's. There is just one such sequence, and it has an even number of A's, namely zero.
Case 2. The sequence is not all C's. There are $3^n-1$ such sequences, and exactly half of them, or $\frac{3^n-1}2$, have an even number of A's. To see this, look at the leftmost letter in the sequence that's not a C; toggling it between A and B, we see that there's a one-to-one correspondence between sequences with an even and an odd number of A's.
Combining the results from Case 1 and Case 2, we get a grand total of $$1+\frac{3^n-1}2=\frac{3^n+1}2.$$
More generally, over an alphabet of $k$ letters, the number of sequences of length $n$ in which a certain letter appears an even number of times is $$(k-2)^n+\frac{k^n-(k-2)^n}2=\frac{k^n+(k-2)^n}2;$$ see my answer to this question.