Problem:
Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define ll          long long int
#define lld          long double
#define pb          push_back
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define vec         vector<ll>
#define all(a)     (a).begin(),(a).end()
#define F           first
#define S           second
#define mod         1000000007
#define rep(i,a,b)	for(ll  i=a;i<b;i++)
#define repi(i,a,b)	for(ll  i=a;i<=b;i++)
#define per(i,a,b)  for(ll i=a;i>=b;i--)
#define mp          make_pair
#define mit         map<ll,ll>::iterator
#define sit         set<ll>::iterator
#define xy          exit(0);
#define pit         pair<ll,ll>::iterator
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y) 
{ 
   return (x | y) & (~x | ~y); 
}  
ll bin_Expo(ll x,ll n)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return bin_Expo(x*x,n/2);
    else                             //n is odd
        return x*bin_Expo(x*x,(n-1)/2);
}
ll mod_Expo(ll x,ll n,ll M)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return mod_Expo((x*x)%M,n/2,M);
    else                             //n is odd
        return (x*mod_Expo((x*x)%M,(n-1)/2,M))%M;
 
}
ll NcR(int n, int r) 
{ 
    ll p = 1, k = 1; 
    if (n - r < r) 
        r = n - r; 
  
    if (r != 0) { 
        while (r) { 
            p *= n; 
            k *= r; 
            ll m=__gcd(p, k); 
            p /= m; 
            k /= m; 
            n--; 
            r--; 
        } 
    } 
    else
        p = 1; 
    return p;
}
bool prime_check(ll x)
{
    bool prime = (x >= 2);
    for (ll i = 2; i * i <= x; i++) 
    {
        if (x % i == 0) 
        {
            prime = false;
            break;
        }
    }
    return prime;
}
ll logg(ll base,ll x)
{
    return (ll)(log(x)/log(base));
}
ll gc(ll a,ll b,ll c)
{
	ll x=__gcd(a,b);
	return __gcd(x,c);
}
char cc(char a,char b)
{
	if(a!='R' && b!='R')
	return 'R';
	if(a!='G' && b!='G')
	return 'G';
	if(a!='B' && b!='B')
	return 'B';
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
    while(TESTS--)
    {
    	ll i,j,m,d,x,h,l,n,p,q,k,ans;
    	cin>>n;
    	char s[n+1];
    	repi(i,1,n)
    	cin>>s[i];
    	ll r[n+1],g[n+1],b[n+1];
    	r[0]=0;g[0]=0;b[0]=0; ans=0;
    	repi(i,1,n)
    	{
    		r[i]=0;g[i]=0;b[i]=0;
    		if(s[i]=='R')
			r[i]=1;
    		else if(s[i]=='G') 
			g[i]=1;
    		else 
			b[i]=1;
		}
    	repi(i,1,n)
    	{
    		r[i]+=r[i-1];
    		g[i]+=g[i-1];
    		b[i]+=b[i-1];
		}
    	repi(i,1,n-2)
    	{
    		repi(j,i+1,n-1)
    		{
    			k=2*j-i;
    			if(s[i]!=s[j] && k<=n)
    			{
    				char c=cc(s[i],s[j]);
    				if(c=='R')
    				{
    					ans+=(r[k-1]-r[j]);
    					ans+=(r[n]-r[k]);
					}
					else if(c=='G')
					{
						ans+=(g[k-1]-g[j]);
    					ans+=(g[n]-g[k]);
					}
					else
					{
						ans+=(b[k-1]-b[j]);
    					ans+=(b[n]-b[k]);
					}
				}
				else if(s[i]!=s[j] && k>n)
    			{
    				char c=cc(s[i],s[j]);
    				if(c=='R')
    				{
    					ans+=(r[n]-r[j]);
					}
					else if(c=='G')
					{
    					ans+=(g[n]-g[j]);
					}
					else
					{
    					ans+=(b[n]-b[j]);
					}
				}
			}
		}
    	cout<<ans;
    }
	return 0;
}