Question
Counting Green Substrings
According to basic color theory, mixing blue and yellow in equal amounts results in green.

You are given a string KaTeX can only parse string typed expression consisting only of the characters 'B' (blue) and 'Y' (yellow).

Your task is to count the number of substrings such that the number of blue characters is equal to the number of yellow characters. Such substrings are considered to become green.
Input
The first line contains a single integer KaTeX can only parse string typed expression — the length of the string.
The second line contains the given string KaTeX can only parse string typed expression of length KaTeX can only parse string typed expression, consisting only of the characters KaTeX can only parse string typed expression and KaTeX can only parse string typed expression.
Output
Print a single integer — the number of substrings in which the count of KaTeX can only parse string typed expression is equal to the count of KaTeX can only parse string typed expression.
Example
Input
6
BYBYBB
Output
6
Explanation
Valid substrings with equal numbers of blue and yellow:
(0, 1) → "BY"
(1, 2) → "YB"
(2, 3) → "BY"
(3, 4) → "YB"
(0, 3) → "BYBY"
(1, 4) → "YBYB"
Total = 6 substrings.

Online