Solution

Even Letters

Show the problem again

How many strings of length 10 over the alphabet {A, B, C, D} contain an even number of A's and an even number of B's? (Zero counts as even.) Give an integer.

Worked solution

The answer is 262656. Use a sign filter. For a string $w$ let $a(w)$ and $b(w)$ be its counts of A and B. The quantity $\frac{1}{4}(1 + (-1)^{a})(1 + (-1)^{b})$ is 1 when both are even and 0 otherwise. Summing over all $4^{10}$ strings, $\sum (-1)^{a(w)}$ is what you get by giving A weight $-1$ and the others weight $+1$, i.e. $(-1 + 1 + 1 + 1)^{10} = 2^{10}$; similarly for B; and $\sum (-1)^{a+b} = (-1 - 1 + 1 + 1)^{10} = 0$. So the count is $\frac{1}{4}(4^{10} + 2^{10} + 2^{10} + 0) = \frac{1048576 + 2048}{4} = 262656$. Equivalently, it is $10!$ times the coefficient of $x^{10}$ in the exponential generating function $\cosh^2(x)\,e^{2x}$.

Source: Classic exponential-generating-function exercise in wide circulation; no traceable original source. Statement written for AxiomIQ.