#include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp> 
    #include <ext/pb_ds/detail/standard_policies.hpp>
    #include <ext/rope>
     
    using namespace std;
    using namespace __gnu_pbds;
    using namespace __gnu_cxx;
     
    #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define endl "\n"
    #define long long long 
     
    #define TRACE
    #ifdef TRACE
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
        template <typename Arg1>
        void __f(const char* name, Arg1&& arg1){
            cout << name << " : " << arg1 << endl;
            //use cerr if u want to display at the bottom
        }
        template <typename Arg1, typename... Args>
        void __f(const char* names, Arg1&& arg1, Args&&... args){
            const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
        }
    #else
    #define trace(...)
    #endif
     
    typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
    typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
    typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>, 
    hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;
     
     
    int main(){
       IOS;
     //    #ifndef ONLINE_JUDGE
     //        freopen("in.txt",  "r",  stdin);
     //        freopen("out.txt", "w", stdout);
     //    #endif
       
       long int n;
       string s;
     
       cin>>n>>s;
     
       long int ans = 0;
     
       vector<long int> red, green, blue;
       ordered_set os,os1,os2,os3;
     
     
       for(long int i = 0; i < n; i++){
        if(s[i] == 'R'){
          red.push_back(i);
          os1.insert(i);
        }
        else if(s[i] == 'B'){
          blue.push_back(i);
          os2.insert(i);
        }
        else{
          green.push_back(i);
          os3.insert(i);
        }
       }
     
       if(red.size() < blue.size() && red.size() < green.size()){
          auto copy = red;
          auto copy2 = green;
          green = copy;
          red = copy2;
          os = os1;
       }
     
       else if(blue.size() < red.size() && blue.size() < green.size()){
          auto copy = blue;
          auto copy2 = green;
          green = copy;
          blue = copy2;
          os = os2;
       }
       else{
        os = os3;
       }
     
       for(long int i = 0; i < red.size(); i++){
        for(long int j = 0; j < blue.size(); j++){
          long int totalGreen = green.size();
          long int dis = llabs(red[i]  - blue[j]);
          long int onePossibleValue = min(red[i],blue[j]) - dis;
          if(os.find(onePossibleValue) != os.end())
            totalGreen--;
     
          long int secondPossibleValue = max(red[i],blue[j]) + dis;      
          if(onePossibleValue != secondPossibleValue && os.find(secondPossibleValue) != os.end())
            totalGreen--;
     
          long int middleValue = (red[i] + blue[j]) / 2;
          if(middleValue != onePossibleValue && middleValue != secondPossibleValue &&  os.find(middleValue) != os.end() && (dis % 2 == 0))
            totalGreen--;
     
          ans += totalGreen;
        }
       }
     
       cout<<ans;
     
       return 0;
    }

Measured memory_bytes: 3960