Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from itertools import accumulate

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

    R, G, B = [0], [0], [0]
    for s in S:
        if s == 'R':
            R.append(1)
            G.append(0)
            B.append(0)

        elif s == 'G':
            R.append(0)
            G.append(1)
            B.append(0)

        elif s == 'B':
            R.append(0)
            G.append(0)
            B.append(1)

    R = list(accumulate(R))
    G = list(accumulate(G))
    B = list(accumulate(B))

    # print(R)
    # print(G)
    # print(B)
    ans = 0
    for j in range(1, N - 1):
        sj = S[j]
        for i in range(j):
            si = S[i]
            if si == sj:
                continue
            # print(sj, j, i)
            if set(['R', 'G', 'B']) - {sj} - {si} == {'R'}:
                ans += R[-1] - R[j + 1]
                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Rx', ans)

            if set(['R', 'G', 'B']) - {sj} - {si} == {'G'}:
                ans += G[-1] - G[j + 1]

                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Gx', ans)

            if set(['R', 'G', 'B']) - {sj} - {si} == {'B'}:
                ans += B[-1] - B[j + 1]
                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Bx', ans)

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    from itertools import accumulate

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

    R, G, B = [0], [0], [0]
    for s in S:
        if s == 'R':
            R.append(1)
            G.append(0)
            B.append(0)

        elif s == 'G':
            R.append(0)
            G.append(1)
            B.append(0)

        elif s == 'B':
            R.append(0)
            G.append(0)
            B.append(1)

    R = list(accumulate(R))
    G = list(accumulate(G))
    B = list(accumulate(B))

    # print(R)
    # print(G)
    # print(B)
    ans = 0
    for j in range(1, N - 1):
        sj = S[j]
        for i in range(j):
            si = S[i]
            sk = ''
            if j + j - i < N:
                sk = S[j + j - i]

            if si == sj:
                continue

            # if sj == sk:
            #     continue

            # print(sj, j, i)
            if (si == 'G' and sj == 'B') or (si == 'B' and sj == 'G'):
                ans += R[-1] - R[j + 1]
                if sk and si != sk and sj != sk:
                    ans -= 1
                # print('Rx', ans)

            if (si == 'R' and sj == 'B') or (si == 'B' and sj == 'R'):
                ans += G[-1] - G[j + 1]

                if sk and si != sk and sj != sk:
                    ans -= 1
                # print('Gx', ans)

            if (si == 'R' and sj == 'G') or (si == 'G' and sj == 'R'):
                ans += B[-1] - B[j + 1]
                if sk and si != sk and sj != sk:
                    ans -= 1
                # print('Bx', ans)

    print(ans)


if __name__ == '__main__':
    main()