Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
cnt_r = [0]*N
cnt_g = [0]*N
cnt_b = [0]*N
for i in range(N-1,-1,-1):
    if S[i] == 'R':
        cnt_r[i] += 1
    if S[i] == 'G':
        cnt_g[i] += 1
    if S[i] == 'B':
        cnt_b[i] += 1
    if i != 0:
        cnt_r[i-1] += cnt_r[i]
        cnt_g[i-1] += cnt_g[i]
        cnt_b[i-1] += cnt_b[i]
# print(cnt_r)
# print(cnt_g)
# print(cnt_b)
ans = 0
for i in range(N):
    if S[i] == 'R':
        for j in range(i+1,N):
            if S[j] == 'G':
                if j+1 >= N:
                    continue
                ans += cnt_b[j+1]

                if j + (j-i) >= N:
                    continue
                if S[j+(j-i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'R':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_b[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'G':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = 0
cnt_r = [0]*N
cnt_g = [0]*N
cnt_b = [0]*N
for i in range(N-1,-1,-1):
    if S[i] == 'R':
        cnt_r[i] += 1
    if S[i] == 'G':
        cnt_g[i] += 1
    if S[i] == 'B':
        cnt_b[i] += 1
    if i != 0:
        cnt_r[i-1] += cnt_r[i]
        cnt_g[i-1] += cnt_g[i]
        cnt_b[i-1] += cnt_b[i]
# print(cnt_r)
# print(cnt_g)
# print(cnt_b)
ans = 0
for i in range(N):
    if S[i] == 'R':
        for j in range(i+1,N):
            if S[j] == 'G':
                if j+1 >= N:
                    continue
                ans += cnt_b[j+1]

                if j + (j-i) >= N:
                    continue
                if S[j+(j-i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'R':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_b[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'G':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1
print(ans)