For Example, there are four such substrings in $CABAAXBYA$.
The original brute force algorithm that I used was, Using an outer for loop, whenever I encounter an $A$, I go inside another for loop to check if there's a $B$ present or not. If a $B$ is found, I increment the count. Finally, the value stored in the count variable yields the required result.
I came across a point while reading about String matching algorithms, when you traverse right to left rather than left to right, your algorithm is more efficient but here the substring isn't given as a parameter to the function that you would be using to compute the required value.
My question is if I traverse the string from right to left instead of left to right, will it make my algorithm more efficient in any case?
Scan the string from left to right. Every time you encounter an $A$, increment a counter. Every time you encounter a $B$, the value of the counter tells you how many substrings starting with an $A$ end at that particular $B$, so add the value to a running total. By the end of the string, the running total gives you the answer.