For example, if $n = 4$ and $k = 2$, then we have ${4 \choose 2} = 6$ strings:
$$ 1100 \rightarrow 1\\ 0011 \rightarrow 1\\ 1001 \rightarrow 2\\ 0110 \rightarrow 2\\ 1010 \rightarrow 3\\ 0101 \rightarrow 3 $$
The number $N$ of $01$ or $10$ substrings is shown beside each string. In this case, the average number is
$$\bar{N}(4,2) = \frac{12}{6}$$
So, what's the general formula for $\bar{N}(n,k)$, the average number of $01$ or $10$ substrings in a random $n$-bit string containing $k$ ones?
The probability of $01$ or $10$ appearing in each individual two-bit window of the $n$-bit string is the probability of drawing one $0$ and one $1$ from a bag of $k$ ones and $n-k$ zeros, given by the hypergeometric distribution: $$\frac{\binom k1\binom{n-k}1}{\binom n2}=\frac{2k(n-k)}{n(n-1)}$$ There are $n-1$ such two-bit windows, so linearity of expectation gives $\overline N(n,k)$ as $n-1$ times the previous probability or $\frac{2k(n-k)}n$.