#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 7;
int f[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  s = " " + s;
  for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + (s[i] == 'A');
  long long ans = 1LL * n * (n + 1) / 2;
  for (int i = 1; i <= n; ++i) {
    --ans;
    if (i - 1) ans += s[i] != s[i - 1];
    int l = 1, r = i - 1, p = i;
    while (l <= r) {
      int mid = (l + r) >> 1, t = f[i - 1] - f[mid - 1];
      if (s[i] == 'A') t = i - mid - t;
      if (t == i - mid)
        p = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    ans -= 1LL * (i - p);
    l = i + 1, r = n, p = i;
    while (l <= r) {
      int mid = (l + r) >> 1, t = f[mid] - f[i];
      if (s[i] == 'A') t = mid - i - t;
      if (t == mid - i)
        p = mid, l = mid + 1;
      else
        r = mid - 1;
    }
    ans -= 1LL * (p - i);
  }
  cout << ans;
  return 0;
}
