Language: Java

/*****Author: Satyajeet Singh, Delhi Technological University************************************/
    import java.io.*;
    import java.util.*;
    import java.text.*; 
    import java.lang.*;
    import java.math.*;
public class Main{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));        
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static HashSet<Integer> graph[];
    static int pptr=0,pptrmax=0;
    static String st[];
/*****************************************Solutions Begins***************************************/

    static long input[]=new long[4005];
    long dp[][][]=new long[4005][4005][4];

    public static void main(String args[]) throws Exception{
        int n=pi();
        char arr[]=ps().toCharArray();
        for(int i=0;i<n;i++){
            if(arr[i]=='G')input[i]=1;
            else if(arr[i]=='B')input[i]=2;
        }
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(input[i]!=input[j]){
                    dp[i][j-i][input[j]]++;
                    dp[i][n][input[j]]++;
                    dp[i][n][3]++;
                }
            }
        }
        //debug(dp);
    //   debug(dp[2][1][2]);
    //   debug(dp[2][n][3]);

        long ans=0;
        int xx=1^2;

        for(int i=0;i<n;i++){
           // debug("hi");

            for(int j=i+1;j<n;j++){
                if(input[i]!=input[j]){
                    ans+=dp[j][n][3]-dp[j][n][input[i]];
                    int k=xx^input[i]^input[j];
                        ans-=dp[j][j-i][k];

                }
               // debug(ans);
            }
           // System.exit(0);

            //debug("hi");
            

        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }
/****************************************Template Begins************************************************/
    static void nl() throws Exception{
        pptr=0;
        st=br.readLine().split(" ");
        pptrmax=st.length;
    }
    static void nls() throws Exception{
        pptr=0;
        st=br.readLine().split("");
        pptrmax=st.length;
    }
    static int pi() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Integer.parseInt(st[pptr++]);
    }
    static long pl() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Long.parseLong(st[pptr++]);
    }
    static double pd() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Double.parseDouble(st[pptr++]);
    }
    static String ps() throws Exception{
        if(pptr==pptrmax)
            nl();
        return st[pptr++];
    }
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.000000000000"); 
        out.print(ft.format(d));
    }
/**************************************Bit Manipulation**************************************************/
    static void printMask(long mask){
        System.out.println(Long.toBinaryString(mask));
    }
    static int countBit(long mask){
        int ans=0;
        while(mask!=0){
            mask&=(mask-1);
            ans++;
        }
        return ans;
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
    // static void addEdge(int a,int b,int c){
    //     graph[a].add(new Pair(b,c));
    // }    
/*********************************************PAIR********************************************************/
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }
        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }
        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }
        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }
        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/******************************************Long Pair*******************************************************/
    static class Pairl implements Comparable<Pairl> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }
            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }
            public boolean equals(Object o) {
                Pairl other = (Pairl) o;
                return u == other.u && v == other.v;
            }
            public int compareTo(Pairl other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }
            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o){
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c){
        long x=1;
        long y=a%c;
        while(b > 0){
            if(b%2 == 1)
                x=(x*y)%c;
            y = (y*y)%c;
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y){
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; 
        b = (x < y) ? x : y;
        r = b;
        while(a % b != 0){
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/********************************************End***********************************************************/
}

Compiler output:
Main.java:31: error: non-static variable dp cannot be referenced from a static context
                    dp[i][j-i][input[j]]++;
                    ^
Main.java:31: error: incompatible types: possible lossy conversion from long to int
                    dp[i][j-i][input[j]]++;
                                    ^
Main.java:32: error: non-static variable dp cannot be referenced from a static context
                    dp[i][n][input[j]]++;
                    ^
Main.java:32: error: incompatible types: possible lossy conversion from long to int
                    dp[i][n][input[j]]++;
                                  ^
Main.java:33: error: non-static variable dp cannot be referenced from a static context
                    dp[i][n][3]++;
                    ^
Main.java:49: error: non-static variable dp cannot be referenced from a static context
                    ans+=dp[j][n][3]-dp[j][n][input[i]];
                         ^
Main.java:49: error: non-static variable dp cannot be referenced from a static context
                    ans+=dp[j][n][3]-dp[j][n][input[i]];
                                     ^
Main.java:49: error: incompatible types: possible lossy conversion from long to int
                    ans+=dp[j][n][3]-dp[j][n][input[i]];
                                                   ^
Main.java:50: error: incompatible types: possible lossy conversion from long to int
                    int k=xx^input[i]^input[j];
                                     ^
Main.java:51: error: non-static variable dp cannot be referenced from a static context
                        ans-=dp[j][j-i][k];
                             ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors