Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
R = 0
G = 0
B = 0
for i in range(N):
    s = S[i]
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    elif s == "B":
        B += 1
ans = R * G * B
for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j - i
        if k <= N-1 and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
            ans -= 1

print(ans)

Fixed submission:
N = int(input())
S = input()

R = [0] * N
G = [0] * N
B = [0] * N
SR = [0] * N
SG = [0] * N
SB = [0] * N
pr = 0
pg = 0
pb = 0
for i in range(N):
    if S[i] == "R":
        pr += 1
        SR[i] = pr
        R[i] = 1
    elif S[i] == "G":
        pg += 1
        SG[i] = pg
        G[i] = 1
    elif S[i] == "B":
        pb += 1
        SB[i] = pb
        B[i] = 1
        
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if R[i]:
            if G[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
            if B[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
        if G[i]:
            if B[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
        if B[i]:
            if G[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = input()

from collections import defaultdict
d = defaultdict(int)

for i in range(N):
    s = S[i]
    d[s] += 1
cnt = 0
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j - i
        if k <= N-1 and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
            cnt += 1
if d["R"] and d["G"] and d["B"]:
    ans = d["R"] * d["G"] * d["B"] - cnt
    print(ans)
else:
    print(0)

Fixed submission:
N = int(input())
S = input()

R = [0] * N
G = [0] * N
B = [0] * N
SR = [0] * N
SG = [0] * N
SB = [0] * N
pr = 0
pg = 0
pb = 0
for i in range(N):
    if S[i] == "R":
        pr += 1
        SR[i] = pr
        R[i] = 1
    elif S[i] == "G":
        pg += 1
        SG[i] = pg
        G[i] = 1
    elif S[i] == "B":
        pb += 1
        SB[i] = pb
        B[i] = 1
        
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if R[i]:
            if G[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
            if B[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
        if G[i]:
            if B[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
        if B[i]:
            if G[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
print(ans)