Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8


def main():
    N = int(input())
    S = input()

    n_right_RGB = [None] * N

    n_r = 0
    n_g = 0
    n_b = 0
    for i in range(N):
        if S[i] == "R":
            n_r += 1
        elif S[i] == "G":
            n_g += 1
        else:
            n_b += 1
    
    for i in range(N):
        if S[i] == "R":
            n_r -= 1
        elif S[i] == "G":
            n_g -= 1
        else:
            n_b -= 1

        n_right_RGB[i] = [n_r, n_g, n_b]
    
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == "R" and S[j] == "G":
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == "B":
                    ans -= 1
            elif S[i] == "R" and S[j] == "B":
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == "G":
                    ans -= 1
            elif S[i] == "G" and S[j] == "B":
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == "R":
                    ans -= 1
            elif S[i] == "G" and S[j] == "R":
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == "B":
                    ans -= 1
            elif S[i] == "B" and S[j] == "R":
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == "G":
                    ans -= 1
            elif S[i] == "B" and S[j] == "G":
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == "R":
                    ans -= 1
    
    return ans


print(main())


Fixed submission:
# coding: utf-8


def main():
    N = int(input())
    S = input()
    S = [-1 if x == "R" else (0 if x == "G" else 1) for x in S]

    n_right_RGB = [None] * N

    n_r = 0
    n_g = 0
    n_b = 0
    for i in range(N):
        if S[i] == -1:
            n_r += 1
        elif S[i] == 0:
            n_g += 1
        else:
            n_b += 1
    
    for i in range(N):
        if S[i] == -1:
            n_r -= 1
        elif S[i] == 0:
            n_g -= 1
        else:
            n_b -= 1

        n_right_RGB[i] = [n_r, n_g, n_b]
    
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == -1 and S[j] == 0:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == -1 and S[j] == 1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 0 and S[j] == 1:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
            elif S[i] == 0 and S[j] == -1:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == 1 and S[j] == -1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 1 and S[j] == 0:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
    
    return ans


print(main())