Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    num[i+1][d[S[i]]] = num[i][d[S[i]]]+1
    for a in list(B-{S[i]}):
        num[i+1][d[a]] = num[i][d[a]]
num = num[1:]
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)

Fixed submission:
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        num[i+1][j] = num[i][j]
        if d[S[i]] == j:
            num[i+1][j] += 1
num = tuple(num[1:])
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
B = {'R','G','B'}
ans = 0
for i in range(N-1):
    s = S[i]
    for j in range(i+1,N):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = S[j+1:j+l]
            R = S[j+l+1:]
            ans += L.count(t)+R.count(t)

print(ans)

Fixed submission:
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        num[i+1][j] = num[i][j]
        if d[S[i]] == j:
            num[i+1][j] += 1
num = tuple(num[1:])
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
B = {'R','G','B'}
ans = 0
for i in range(N-1):
    s = S[i]
    for j in range(i+1,N):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = S[j+1:j+l]
            R = S[j+l+1:]
            ans += L.count(t)+R.count(t)

print(ans)

Fixed submission:
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        num[i+1][j] = num[i][j]
        if d[S[i]] == j:
            num[i+1][j] += 1
num = tuple(num[1:])
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)