Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(0, N-3):
    for j in range(i+1, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            r = color[0]
            ans += S[j+1:].count(r)
            try:
                if S[j+(j-i)] == r:
                    ans -= 1
            except IndexError:
                pass
print(ans)


Fixed submission:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b

for i in range(N-2):
    for j in range(i+1, N):
        k = j+(j-i)
        if k > (N-1):
            break
        if S[i] != S[j] != S[k] != S[i]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
num = -2
for i in range(0, N-3):
    for j in range(i, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            r = color[0]
            if j > num:
                num = S[j+1:].find(r)
                su = S[j+1:].count(r)
                ans += su
            else:
                ans += su
            try:
                if S[j+(j-i)] == r:
                    ans -= 1
            except IndexError:
                pass
print(ans)


Fixed submission:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b

for i in range(N-2):
    for j in range(i+1, N):
        k = j+(j-i)
        if k > (N-1):
            break
        if S[i] != S[j] != S[k] != S[i]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
num = -2
for i in range(0, N-3):
    for j in range(i, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            r = color[0]
            if j > num:
                num = S[j+1:].find(r)
                su = S[j+1:].count(r)
                ans += su
            else:
                ans += su
            try:
                if S[j+(j-i)] == r:
                    ans -= 1
            except IndexError:
                pass
            num = S[j+1:].find(r)
print(ans)


Fixed submission:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b

for i in range(N-2):
    for j in range(i+1, N):
        k = j+(j-i)
        if k > (N-1):
            break
        if S[i] != S[j] != S[k] != S[i]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(0, N-3):
    for j in range(i, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            ans += S[j+1:].count(color[0])
            try:
                if S[j+(j-i)] == color[0]:
                    ans -= 1
            except IndexError:
                continue
print(ans)


Fixed submission:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b

for i in range(N-2):
    for j in range(i+1, N):
        k = j+(j-i)
        if k > (N-1):
            break
        if S[i] != S[j] != S[k] != S[i]:
            ans -= 1
print(ans)