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), 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?