Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())
S = input()

idxs = [i for i in range(N)]

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

ans = 0
for ri in ris:
    for gi in gis:
        for bi in bis:
            i, j, k = sorted([ri, gi, bi])
            if k - j != j - i:
                ans += 1



"""
for i in range(N):
    for j in range(i, N):
        for k in range(j, N):
            if j - i != k - j:
                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()

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if 0 <= k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1

ans = all - cnt


print(ans)

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

N = int(input())
S = input()

idxs = [i for i in range(N)]

ans = 0
for i in range(N):
    for j in range(i, N):
        for k in range(j, N):
            if j - i != k - j:
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    ans += 1


"""
data = deque()
for c in combinations(idxs, 3):
    i, j, k = c
    if j-i != k-j:
        data.append(c)

ans = 0
for c in data:
    # i, j, k = sorted(c)
    i, j, k = c
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1
"""

print(ans)


Fixed submission:

N = int(input())
S = input()

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if 0 <= k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1

ans = all - cnt


print(ans)

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

N = int(input())
S = input()

idxs = [i for i in range(N)]

ans = 0
for c in combinations(idxs, 3):
    # i, j, k = sorted(c)
    i, j, k = c
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if j - i != k - j and s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1

print(ans)


Fixed submission:

N = int(input())
S = input()

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if 0 <= k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1

ans = all - cnt


print(ans)

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

N = int(input())
S = input()

idxs = [i for i in range(N)]

ans = 0
for c in combinations(idxs, 3):
    i, j, k = sorted(c)
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if j - i != k - j and s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1

print(ans)


Fixed submission:

N = int(input())
S = input()

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if 0 <= k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1

ans = all - cnt


print(ans)