#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n;
int pref[maxn][2];
string s;
int binary(int x, bool sto) {
  int lo = x + 1, hi = n, mid;
  while (lo < hi) {
    mid = (lo + hi) / 2;
    if (pref[mid][sto] - pref[x][sto]) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  return lo;
}
int rijesi(int x) {
  int pos1;
  bool trazi;
  if (x == n - 2) {
    if (s[x] != s[x + 1]) {
      return 0;
    } else {
      return 1;
    }
  } else {
    if (s[x] == s[x + 1]) {
      if (s[x] == 'A') {
        trazi = 1;
      } else {
        trazi = 0;
      }
      if (pref[n][trazi] - pref[x + 1][trazi]) {
        return n - x - 2;
      } else {
        return n - x - 1;
      }
    } else {
      if (s[x + 2] == s[x]) {
        return n - x - 2;
      } else {
        if (s[x] == 'A') {
          trazi = 0;
        } else {
          trazi = 1;
        }
        pos1 = binary(x + 2, trazi) - 1;
        if (s[pos1] == s[x]) {
          return n - pos1;
        }
        return 0;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;
  long long sol = 0;
  for (int i = 1; i <= n; i++) {
    pref[i][0] = pref[i - 1][0];
    pref[i][1] = pref[i - 1][1];
    if (s[i - 1] == 'A') {
      pref[i][0]++;
    } else {
      pref[i][1]++;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    sol += rijesi(i);
  }
  cout << sol << '\n';
  return 0;
}
