#include <bits/stdc++.h>

//#define debug_mode

#ifdef debug_mode
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
using std::endl;
#endif

#ifndef debug_mode
using namespace std;
#endif

void func_to_be_create();

int main(){

    #ifdef debug_mode
    int TEST_NUM = 2;
    while( TEST_NUM --){
    #endif

        func_to_be_create();

    #ifdef debug_mode
    }
    #endif
    return 0;

}

int naive_count(std::string& S){
    int count = 0;
    for(int i = 0 ; i < S.size() ; i++){
        for(int j = i + 1 ; j < S.size(); j++){
            for(int k = j + 1 ; k < S.size() ; k++){
                if( j - i != k - j){
                    std::array<char,3> arr = {S[i], S[j], S[k]};
                    std::sort(arr.begin(), arr.end());
                    if( arr[0] == 'B' && arr[1] == 'G' && arr[2] == 'R'){
                        count++;
                    }
                }
            }
        }
    }

    return count;
}

unsigned long smart_count(std::string& S){
    std::vector<std::array<unsigned long,3>> count;
    if( S[0] == 'R' ){
            count.push_back({1, 0, 0});
    }else if( S[0] == 'G'){
            count.push_back({0, 1, 0});
    }else{
        count.push_back({0, 0, 2});
    }

    for(unsigned int i = 1 ; i < S.size() ; i++){
        if( S[i] == 'R' ){
            count.push_back({count[i-1][0] + 1, count[i-1][1], count[i-1][2]});
        }else if( S[i] == 'G'){
            count.push_back({count[i-1][0], count[i-1][1] + 1, count[i-1][2]});
        }else{
            count.push_back({count[i-1][0], count[i-1][1], count[i-1][2] + 1});
        }
    }

    unsigned long sum = 0;
    std::map<char, unsigned int> char_map = {{'R', 0}, {'G', 1}, {'B', 2}};

    for(unsigned int i = 0 ; i < S.size(); i++){
        for(unsigned int j = i + 2 ; j < S.size(); j++){
            if( S[j] == S[i] ){
                continue;
            }else if ( (i + j) % 2 == 0){
                //cout << "testing i " << i << " " << j << endl;
                unsigned int k = (i + j) / 2;
                int discount = 0;
                std::array<char,3> arr = {S[i], S[j], S[k]};
                std::sort(arr.begin(), arr.end());
                
                if( arr[0] == 'B' && arr[1] == 'G' && arr[2] == 'R'){
                       discount = 1;
                }
                //cout << "discount " << discount << endl;
                unsigned int target_key = (char_map[S[i]] + 1) % 3;
                if ( target_key == char_map[S[j]]){
                    target_key = (target_key + 1) % 3;
                }
                //cout << " got " << count[j][target_key] - count[i][target_key] << endl;
                sum += count[j][target_key] - count[i][target_key] - discount;
            }else{
                //cout << "testing i " << i << " " << j << endl;
                unsigned int target_key = (char_map[S[i]] + 1) % 3;
                if ( target_key == char_map[S[j]]){
                    target_key = (target_key + 1) % 3;
                }
                //cout << " got " << count[j][target_key] - count[i][target_key] << endl;
                sum += count[j][target_key] - count[i][target_key];
            }
        }
    }
    return sum;
}

void func_to_be_create(){
    unsigned int N;
    std::
    string S;
    cin >> N;
    cin >> S;
    unsigned long  sum = 0;
    sum = smart_count(S);
    cout << sum << endl;
}

Measured memory_bytes: 3748