Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
seq = input().strip()
ans = 0
r = []
g = []
b = []
for i in range(len(seq)):
    if seq[i] == 'R':
        r.append(i)
    elif seq[i] == 'G':
        g.append(i)
    else:
        b.append(i)

for i in r:
    for j in g:
        for k in b:
            z,x,c = sorted([i,j,k])
            if x-z != c - x:
                ans += 1
print(ans)

Fixed submission:
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        if (i+j) % 2 == 0 and ((i + j) //2) in b:
            ans -= 1
        if max(i,j) + (max(i,j) - min(i,j)) in b:
            ans -= 1
        if min(i,j) - (max(i,j) - min(i,j)) in b:
            ans -= 1
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
seq = input().strip()
ans = 0
r = []
g = []
b = []
for i in range(len(seq)):
    if seq[i] == 'R':
        r.append(i)
    elif seq[i] == 'G':
        g.append(i)
    else:
        b.append(i)

for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        if seq[i] == seq[j]:
            continue
        if seq[i] in ('R', 'G') and seq[j] in ('R', 'G'):
            arr = b
        elif seq[i] in ('R', 'B') and seq[j] in ('R', 'B'):
            arr = g
        else:
            arr = r

        for k in arr:
            if k > j and j-i != k -j:
                ans += 1
        # for k, val in enumerate(seq[j+1:], j+1):
        #     if seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
        #         ans +=1
print(ans)

Fixed submission:
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        if (i+j) % 2 == 0 and ((i + j) //2) in b:
            ans -= 1
        if max(i,j) + (max(i,j) - min(i,j)) in b:
            ans -= 1
        if min(i,j) - (max(i,j) - min(i,j)) in b:
            ans -= 1
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
seq = input()
ans = 0
for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        if seq[i] == seq[j]:
            continue
        for k, val in enumerate(seq[j+1:], j+1):
            if seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
                ans +=1
print(ans)

Fixed submission:
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        if (i+j) % 2 == 0 and ((i + j) //2) in b:
            ans -= 1
        if max(i,j) + (max(i,j) - min(i,j)) in b:
            ans -= 1
        if min(i,j) - (max(i,j) - min(i,j)) in b:
            ans -= 1
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
seq = input()
ans = 0
for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        for k, val in enumerate(seq[j+1:], j+1):
            if seq[i] != seq[j] and seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
                ans +=1
print(ans)

Fixed submission:
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        if (i+j) % 2 == 0 and ((i + j) //2) in b:
            ans -= 1
        if max(i,j) + (max(i,j) - min(i,j)) in b:
            ans -= 1
        if min(i,j) - (max(i,j) - min(i,j)) in b:
            ans -= 1
        
print(ans)