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>
using namespace std;

#define ll long long
#define tc(t) int t;cin >> t; while(t--)
#define pb push_back
#define fi first
#define se second
#define debug1(x) cerr << #x << " = " << x << '\n';
#define debug2(x, y) cerr << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define _ ios_base::sync_with_stdio(0);cin.tie(NULL);

void solve() {
    int n;
    cin >> n;
    ll ans = 0;
    string s;
    cin >> s;
    map<char, ll> mp;
    for(auto ch: s) {
        mp[ch] ++;
    }
    mp[s[0]] --;
    for(int j = 1; j<n-1; j++) {
        mp[s[j]] --;
        for(int i = j-1; i>=0; i--) {
            if(s[j] != s[i]) {
                int r = 0, b = 0, g = 0;
                if(s[i] == 'R' || s[j] == 'R') r = 1;
                if(s[i] == 'G' || s[j] == 'G') g = 1;
                if(s[i] == 'B' || s[j] == 'B') b = 1;
                if(b == 0) {
                    ans += mp['B'];
                    int idx = (j-i);
                    if(j+idx < n && s[j + idx] == 'B') ans --;
                }
                else if(g == 0) {
                    ans += mp['G'];
                    int idx = (j-i);
                    if(j+idx < n && s[j + idx] == 'G') ans --;
                }
                else if(r == 0) {
                    ans += mp['R'];
                    int idx = (j-i);
                    if(j+idx < n && s[j + idx] == 'R') ans --;
                }
            }
        }
    }
    cout << ans;
}

int main() {_
    int t = 1;
    //cin >> t;
    // Please remember to uncomment for taking test cases
    for(int i = 1; i<=t; i++) {
        solve();
    }
    return 0;
}