Buggy python submission (verdict: Time Limit Exceeded):
# this is a good problem !

import sys
si = sys.stdin.readline


def main():
    d, ld, count, n, s = ['R', 'G', 'B'], [[0], [0], [0]], 0, int(si()), si()
    dc = d[:]
    # letter distribution
    # s = si()[:-1]

    for i in range(n-1, -1, -1):
        for j in range(3):
            if s[i] == dc[j]:
                ld[j] = [1+ld[j][0]]+ld[j]
            else:
                ld[j] = [ld[j][0]]+ld[j]

    for i in range(n-2):
        rgb1 = s[i]
        dc.remove(rgb1)
        for j in range(i+1, n-1):
            if s[j] in dc:
                rgb2 = s[j]
                dc.remove(rgb2)
                count += ld[d.index(dc[0])][j+1]
                k = j+j-i
                if k < n and s[k] in dc:
                    count -= 1
                dc += [rgb2]
        dc += [rgb1]
    print(count)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    cnt = s.count('R')*s.count('G')*s.count('B')

    for i in range(n-2):
        for j in range(i+1, n-1):
            k = j+j-i
            if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# this is a good problem !

import sys
si = sys.stdin.readline

d = ['R', 'G', 'B']
ld = [[0], [0], [0]]  # letter distribution


def main():
    count = 0
    n = int(si())
    s = si()[:-1]
    dc = d[:]

    for i in range(n-1, -1, -1):
        for j in range(3):
            if s[i] == dc[j]:
                ld[j] = [1+ld[j][0]]+ld[j]
            else:
                ld[j] = [ld[j][0]]+ld[j]

    for i in range(n-2):
        rgb1 = s[i]
        dc.remove(rgb1)
        for j in range(i+1, n-1):
            if s[j] in dc:
                rgb2 = s[j]
                dc.remove(rgb2)
                count += ld[d.index(dc[0])][j+1]
                k = j+j-i
                if k < n and s[k] in dc:
                    count -= 1
                dc += [rgb2]
        dc += [rgb1]
    print(count)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    cnt = s.count('R')*s.count('G')*s.count('B')

    for i in range(n-2):
        for j in range(i+1, n-1):
            k = j+j-i
            if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# this is a good problem !

import sys
si = sys.stdin.readline

d = ['R', 'G', 'B']
ld = [[], [], []]  # letter distribution


def main():
    count = 0
    n = int(si())
    l = list(si())
    l.pop()
    dc = d[:]

    for i in range(n-1, -1, -1):
        if i == n-1:
            if l[i] == 'R':
                ld[0].append(1)
                ld[1].append(0)
                ld[2].append(0)
            elif l[i] == 'G':
                ld[0].append(0)
                ld[1].append(1)
                ld[2].append(0)
            else:
                ld[0].append(0)
                ld[1].append(0)
                ld[2].append(1)
        else:
            if l[i] == 'R':
                ld[0] = [ld[0][0]+1]+ld[0]
                ld[1] = [ld[1][0]+0]+ld[1]
                ld[2] = [ld[2][0]+0]+ld[2]
            elif l[i] == 'G':
                ld[0] = [ld[0][0]+0]+ld[0]
                ld[1] = [ld[1][0]+1]+ld[1]
                ld[2] = [ld[2][0]+0]+ld[2]
            else:
                ld[0] = [ld[0][0]+0]+ld[0]
                ld[1] = [ld[1][0]+0]+ld[1]
                ld[2] = [ld[2][0]+1]+ld[2]

    for i in range(n-2):
        r1 = l[i]
        dc.remove(r1)
        for j in range(i+1, n-1):
            if l[j] in dc:
                r2 = l[j]
                dc.remove(r2)
                count += ld[d.index(dc[0])][j+1]
                if (j+j-i) < n and l[j+j-i] in dc:
                    count -= 1
                dc += [r2]
        dc += [r1]
    print(count)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    cnt = s.count('R')*s.count('G')*s.count('B')

    for i in range(n-2):
        for j in range(i+1, n-1):
            k = j+j-i
            if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
si = sys.stdin.readline

d = ['R', 'G', 'B']


def main():
    n = int(si())
    l = list(si())
    dc = d[:]
    count = 0
    for i in range(n):
        r1 = l[i]
        dc.remove(r1)
        for j in range(i+1, n):
            if l[j] in dc:
                r2 = l[j]
                dc.remove(r2)
                for k in range(j+1, n):
                    if l[k] in dc:
                        count += 1
                if (j+j-i) < n and l[j+j-i] in dc:
                    count -= 1
                dc += [r2]
        dc += [r1]
    print(count)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    cnt = s.count('R')*s.count('G')*s.count('B')

    for i in range(n-2):
        for j in range(i+1, n-1):
            k = j+j-i
            if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()