#include<bits/stdc++.h>
#define int long long 
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(a) a.begin(),a.end()
#define ld long double
#define pii pair<int,int>
using namespace std;
signed main(){
    fast;
    int n;
    cin>>n;
    string s;
    cin>>s;
    s = '#'+s;
    int ans = 0;
    vector<int> adj[3];
    for(int i = 1;i<=n;i++){
        if(s[i] == 'R')
            adj[0].pb(i);
        else if(s[i] == 'G')
            adj[1].pb(i);
        else adj[2].pb(i);
    }
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
    for(auto it:adj[0]){
        int i = it;
        for(auto it:adj[1]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[2]),j)-adj[2].begin();
            if( k == (int)adj[2].size())
                continue;
            ans+=((int)adj[2].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'B')
                ans--;
        }
    }
    //cout<<ans<<endl;
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
     for(auto it:adj[0]){
        int i = it;
        for(auto it:adj[2]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[1]),j)-adj[1].begin();
            if( k == (int)adj[1].size())
                continue;
            ans+=((int)adj[1].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'G')
                ans--;
        }
    }
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
     for(auto it:adj[1]){
        int i = it;
        for(auto it:adj[2]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[0]),j)-adj[0].begin();
            if( k == (int)adj[0].size())
                continue;
            ans+=((int)adj[0].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'R')
                ans--;
        }
    }
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
     for(auto it:adj[1]){
        int i = it;
        for(auto it:adj[0]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[2]),j)-adj[2].begin();
            if( k == (int)adj[2].size())
                continue;
            ans+=((int)adj[2].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'B')
                ans--;
        }
    }
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
     for(auto it:adj[2]){
        int i = it;
        for(auto it:adj[0]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[1]),j)-adj[1].begin();
            if( k == (int)adj[1].size())
                continue;
            ans+=((int)adj[1].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'G')
                ans--;
        }
    }
    //RGB 0 1 2
    //RBG 0 2 1
    //GBR 1 2 0
    //GRB 1 0 2
    //BRG 2 0 1
    //BGR 2 1 0
     for(auto it:adj[2]){
        int i = it;
        for(auto it:adj[1]){
            int j = it;
            if(j<i)
                continue;
            int d = j - i;
            int k = upper_bound(all(adj[0]),j)-adj[0].begin();
            if( k == (int)adj[0].size())
                continue;
            ans+=((int)adj[0].size() - k);
            int add = j + d;
            if(add<=n && s[add] == 'R')
                ans--;
        }
    }
    cout<<ans;
}

Measured memory_bytes: 3716