Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()

RN = S.count('R')
GN = S.sount('G')
BN = S.count('B')

ans = RN * GN * BN

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

print(ans)

Fixed submission:
N = int(input())
S = input()

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            sb = 0
            sc = 0
            flgb = 0
            for a in range(len(L[i])):
                flgc = 0
                for b in range(sb, len(L[j])):
                    if L[i][a] <= L[j][b]:
                        if flgb == 0:
                            sb = b
                            flgb = 1
                        for c in range(sc, len(L[k])):
                            if L[j][b] <= L[k][c]:
                                if flgc == 0:
                                    sc = c
                                    flgc = 1
                                if L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                    ans += 1

print(ans)

Fixed submission:
N = int(input())
S = input()

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            sb = 0
            sc = 0
            flgb = 0
            for a in range(len(L[i])):
                flgc = 0
                for b in range(sb, len(L[j])):
                    if L[i][a] <= L[j][b]:
                        if flgb == 0:
                            sb = b
                            flgb = 1
                        for c in range(sc, len(L[k])):
                            if L[j][b] <= L[k][c]:
                                if flgc == 0:
                                    sc = c
                                    flgc = 1
                                if L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                    ans += 1

print(ans)

Fixed submission:
N = int(input())
S = input()

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            for a in range(len(L[i])):
                for b in range(len(L[j])):
                    if L[i][a] <= L[j][b]:
                        for c in range(len(L[k])):
                            if L[j][b] <= L[k][c] and L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                ans += 1

print(ans)

Fixed submission:
N = int(input())
S = input()

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

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

print(ans)