Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

ss = [0] * n
ans = 0
for i in range(n):
    if s[i] == 'R':
        ss[i] = 1
    elif s[i] == 'G':
        ss[i] = 2
    else:
        ss[i] = 3

for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if (j - i) == (k - j):
                continue
            if ss[i] == ss[j] == ss[k] == 2:
                continue
            else:
                if ss[i] + ss[j] + ss[k] == 6:
                    ans += 1
print(ans)

Fixed submission:
n = int(input())
s = list(input())
s_reverse = s[::-1]
ans = 0
r_cnt = 0
b_cnt = 0
g_cnt = 0
r = [0] * n
b = [0] * n
g = [0] * n
for i in range(n):
    if s_reverse[i] == 'R':
        r_cnt += 1
    elif s_reverse[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r[n-i-1] = r_cnt
    g[n-i-1] = g_cnt
    b[n-i-1] = b_cnt
    
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == 'R' and s[j] == 'G':
            if (2*j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'R' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'G' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'G' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]
        elif s[i] == 'B' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'B' and s[j] == 'G':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

ss = [0] * n
ans = 0
for i in range(n):
    if s[i] == 'R':
        ss[i] = 1
    elif s[i] == 'G':
        ss[i] = 2
    else:
        ss[i] = 3

for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if (j - i) == (k - j):
                continue
            if ss[i] == ss[j] == ss[k] == 2:
                continue
            else:
                if ss[i] + ss[j] + ss[k] == 6:
                    ans += 1
print(ans)

Fixed submission:
n = int(input())
s = list(input())
s_reverse = s[::-1]
ans = 0
r_cnt = 0
b_cnt = 0
g_cnt = 0
r = [0] * n
b = [0] * n
g = [0] * n
for i in range(n):
    if s_reverse[i] == 'R':
        r_cnt += 1
    elif s_reverse[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r[n-i-1] = r_cnt
    g[n-i-1] = g_cnt
    b[n-i-1] = b_cnt
    
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == 'R' and s[j] == 'G':
            if (2*j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'R' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'G' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'G' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]
        elif s[i] == 'B' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'B' and s[j] == 'G':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]

print(ans)