Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)

Fixed submission:
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N = int(input())
S = input()
counter = {'R': [0], 'G': [0], 'B': [0]}
for s in S:
    for c in 'RGB':
        counter[c].append(counter[c][-1] + (1 if s == c else 0))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = set([S[i], S[j]])
    if 'R' not in temp: key = 'R'
    elif 'G' not in temp: key = 'G'
    else: key = 'B'
    cnt = counter[key]
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)

Fixed submission:
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
from collections import Counter

N = int(input())
S = input()
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = set([S[i], S[j]])
    if 'R' not in temp: key = 'R'
    elif 'G' not in temp: key = 'G'
    else: key = 'B'
    counter = Counter(S[j+1:j+l] + S[j + l + 1:])
    ans += counter[key]
print(ans)

Fixed submission:
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)