I'm not sure if this question is suited for MO, but it does seem quite challenging to me, and is required for a research problem in chemistry I'm working on. I did try getting help from elsewhere ([Cross-post on MSE](https://math.stackexchange.com/questions/1126285/how-to-count-the-number-of-substrings-in-this-combinatorics-problem)), but no luck so far. So I apologize if this question is unwelcome here. 


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?



Combinatorics problem involving counting the number of certain substrings
The number of sequences with ABA or BAB at one specific place is
$$\binom{a+b-3}{a-2}+\binom{a+b-3}{a-1}=\binom{a+b-2}{a-1}.$$
Hence the total number of ABA and BAB is
$$(a+b-2)\binom{a+b-2}{a-1}.$$


