No Adjacency Combinatorics Problem via Generating Function

(The notations are slightly changed here)

We may use a directed graph for such pattern avoiding problems.

Write the matrix as

\begin{align*} A = \left(\begin{array}{r|rrr} & W & G & R \\ \hline W & w & g & r \\ G & w & g & 0 \\ R & w & 0 & r \\ \end{array}\right) \end{align*}

$W,G,R$ are the states to indicate the three colors and $w,g,r$ are the formal variables to indicate the colors.

To obtain the recurrence formula, find the characteristic polynomial,

$$x^{3} - \left(g + r + w\right) x^{2} + g r x + g r w = 0$$

If we indicate $F(a,b,c)$ as the number of ways to arrange the cards, where a,b,c are the number of white, green and red cards respectively, then $$F(a,b,c) = F(a-1,b,c)+F(a,b-1,c)+F(a,b,c-1)-F(a,b-1,c-1)-F(a-1,b-1,c-1)$$ and set the necessary boundary conditions

To obtain the generating function, find $$\left(I-x\, A\right)^{-1}$$ Each entry will have a g.f. for that entry, and we require the sum of the first row, which is

$$G(x) =\frac{1-g\, r\, x^{2}}{1\, -\, {\left(g\, +\, r\, +\, w\right)+\, g\, r\, x^{2}+g\, r\, w\, x^{3}}}$$

If there are $n=a+b+c$ cards in total, find $[x^n]$ and find the required $[w^a\, g^b\, r^c]$

Richard Stanley's "Enumerative combinatorics" discusses such methods (transfer matrices)