Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

r = []
g = []
b = []



for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)

cr = Counter(r)
cg = Counter(g)
cb = Counter(b)

import bisect
from copy import deepcopy
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        ng = j + (j - i)
        if s[i] == "R" and s[j] == "G":
            l = deepcopy(b)
            c = cb
        elif s[i] == "R" and s[j] == "B":
            l = deepcopy(g)
            c = cg
        elif s[i] == "G" and s[j] == "R":
            l = deepcopy(b)
            c = cb
        elif s[i] == "G" and s[j] == "B":
            l = deepcopy(r)
            c = cr
        elif s[i] == "B" and s[j] == "R":
            l = deepcopy(g)
            c = cg
        elif s[i] == "B" and s[j] == "G":
            l = deepcopy(r)
            c = cr
        ans += len(l) - bisect.bisect_right(l, j)
        ans -= c[j + (j - i)]

print(ans)






Fixed submission:
import sys
input = sys.stdin.readline

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)



for i in range(1, n + 1):
    if s[i - 1] == "R":
        r[i] = r[i - 1] + 1
    else:
        r[i] = r[i - 1]
    if s[i - 1] == "G":
        g[i] = g[i - 1] + 1
    else:
        g[i] = g[i - 1]
    if s[i - 1] == "B":
        b[i] = b[i - 1] + 1
    else:
        b[i] = b[i - 1]

ans = 0
for i in range(n):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ng = j + (j - i) + 1
        if s[i] == "R" and s[j] == "G":
            l = b
        elif s[i] == "R" and s[j] == "B":
            l = g
            #c = cg
        elif s[i] == "G" and s[j] == "R":
            l = b
            #c = cb
        elif s[i] == "G" and s[j] == "B":
            l = r
            #c = cr
        elif s[i] == "B" and s[j] == "R":
            l = g
            #c = cg
        elif s[i] == "B" and s[j] == "G":
            l = r
            #c = cr
        ans += l[n] - l[j + 1]
        if ng < n + 1:
            ans -= l[ng] - l[ng - 1]
        #print(ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

r = []
g = []
b = []

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)

import bisect
from copy import deepcopy
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        ng = j + (j - i)
        if s[i] == "R" and s[j] == "G":
            l = deepcopy(b)
        elif s[i] == "R" and s[j] == "B":
            l = deepcopy(g)
        elif s[i] == "G" and s[j] == "R":
            l = deepcopy(b)
        elif s[i] == "G" and s[j] == "B":
            l = deepcopy(r)
        elif s[i] == "B" and s[j] == "R":
            l = deepcopy(g)
        elif s[i] == "B" and s[j] == "G":
            l = deepcopy(r)
        ans += bisect.bisect_left(l, j + (j - i)) - bisect.bisect_right(l, j)
        ans += len(l) - bisect.bisect_right(l, j + (j - i))

print(ans)






Fixed submission:
import sys
input = sys.stdin.readline

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)



for i in range(1, n + 1):
    if s[i - 1] == "R":
        r[i] = r[i - 1] + 1
    else:
        r[i] = r[i - 1]
    if s[i - 1] == "G":
        g[i] = g[i - 1] + 1
    else:
        g[i] = g[i - 1]
    if s[i - 1] == "B":
        b[i] = b[i - 1] + 1
    else:
        b[i] = b[i - 1]

ans = 0
for i in range(n):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ng = j + (j - i) + 1
        if s[i] == "R" and s[j] == "G":
            l = b
        elif s[i] == "R" and s[j] == "B":
            l = g
            #c = cg
        elif s[i] == "G" and s[j] == "R":
            l = b
            #c = cb
        elif s[i] == "G" and s[j] == "B":
            l = r
            #c = cr
        elif s[i] == "B" and s[j] == "R":
            l = g
            #c = cg
        elif s[i] == "B" and s[j] == "G":
            l = r
            #c = cr
        ans += l[n] - l[j + 1]
        if ng < n + 1:
            ans -= l[ng] - l[ng - 1]
        #print(ans)

print(ans)