How to count the number of substrings in this combinatorics problem? Let's say I'm making a string of $A$s and $B$s, where the number of $A$s and $B$s are $a$ and $b$ respectively. A total of $a+b \choose a$ such strings are possible. Now, I wish to know the total number of '$ABA$' and '$BAB$' substrings that occur in all such strings. How do I count this?

Community wiki answer so the question can be marked as answered:
The cross-posted question at MO has been answered; the result is
$$
(a+b−2)\binom{a+b−2}{a−1}\;.
$$
