TITLE: In how many ways can we permute the digits $2,3,4,5,2,3,4,5$ if identical digits must not be adjacent?
QUESTION [5 upvotes]: In how many ways can we permute the digits $2,3,4,5,2,3,4,5$ if identical digit must not be adjacent?

I tried this by first taking total permutation  as $\dfrac{8!}{2^4}$
Now $n_1$ as $22$ or $33$ or $44$ or $55$ occurs differently
$N_1 = \left(^7C_1\times \dfrac{7!}{8}\right)$
And $n_2 = \left(^4C_1 \times 4!\right)$
Using the inclusion-exclusion principle I got:
$\dfrac{8!}{16}-\left(^7C_1\times\dfrac{7!}{8}\right)+\left(^4C_1\times4!\right)$
But answer was wrong
Please help me solve the question
This question is from combinatorics and helpful for RMO

REPLY [5 votes]: Here is  a variation based upon generating functions of Smirnov words. These are words with no equal consecutive characters. (See example III.24 Smirnov words from Analytic Combinatorics by Philippe Flajolet and Robert Sedgewick for more information.) 

We encode the digits 
  \begin{align*}
2,3,4,5 \qquad\text{as}\qquad a,b,c,d
\end{align*}
  and look  for Smirnov words of length $8$ built from $a,b,c,d$ with each letter occurring exactly twice.

A generating function for the number of Smirnov words over a four letter alphabet $V=\{a,b,c,d\}$ is given by
\begin{align*}
\left(1-\frac{4z}{1+z}\right)^{-1}
\end{align*}
We  use      the coefficient of  operator  $[z^n]$     to  denote the coefficient of $z^n$ in a series $A(z)$. The number of all Smirnov words of length $8$ over a four letter alphabet is therefore 
\begin{align*}
[z^8]\left(1-\frac{4z}{1+z}\right)^{-1}
\end{align*}

Since we want to count the number of words of length $8$ with each character in $V$ occurring twice, we keep track of each character. We obtain with some help of Wolfram Alpha 
  \begin{align*}
[a^2b^2c^2d^2]\left(1-\frac{a}{1+a}-\frac{b}{1+b}-\frac{c}{1+c}-\frac{d}{1+d}\right)^{-1}=\color{blue}{864}
\end{align*}