Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    Cumsum = [[0] * 3 for i in range(N + 1)]
    for i in range(N):
        Cumsum[i + 1][0] = Cumsum[i][0] + (1 if S[i] == 'R' else 0)
        Cumsum[i + 1][1] = Cumsum[i][1] + (1 if S[i] == 'G' else 0)
        Cumsum[i + 1][2] = Cumsum[i][2] + (1 if S[i] == 'B' else 0)

    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                cnt = Cumsum[N][3 - RGB.index(S[i]) - RGB.index(S[j])] - Cumsum[j][3 - RGB.index(S[i]) - RGB.index(S[j])]
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        cnt -= 1
                ans += cnt
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
import collections


def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    cnt = collections.Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                cnt = S[j + 1:].count(RGB[3 - RGB.index(S[i]) - RGB.index(S[j])])
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        cnt -= 1
                ans += cnt
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
import collections


def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    cnt = collections.Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                for k in range(j + 1, N):
                    if S[j] != S[k] and S[i] != S[k]:
                        if j - i != k - j:
                            ans += 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
import collections


def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    cnt = collections.Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    main()