Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    alls = set(range(n))
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = alls - r - g

    ans = 0
    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    b = set()
    r_add = r.add
    g_add = g.add
    b_add = b.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
        else:
            b_add(ind)
    ans = 0
    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = []
    g = []
    b = []
    for ind, c in enumerate(s):
        if c == 'R':
            r.append(ind)
        elif c == 'G':
            g.append(ind)
        else:
            b.append(ind)
    ans = 0

    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

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


def main():
    n = int(input())
    s = input()
    # comb = itertools.combinations(range(n), 3)
    ans = 0
    for a in range(n):
        for b in range(a+1, n):
            for c in range(b+1, n):
                if b - a == c - b:
                    continue
                if len(set((s[a], s[b], s[c]))) == 3:
                    ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

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


def main():
    n = int(input())
    s = input()
    comb = itertools.combinations(range(n), 3)

    def gen(comb):
        for cm in comb:
            a, b, c = cm
            if b - a == c - b:
                continue
            if len(set((s[a], s[b], s[c]))) == 3:
                yield True

    return sum(gen(comb))


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

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


def main():
    n = int(input())
    s = input()
    index = [i for i in range(n)]
    comb = itertools.combinations(index, 3)
    ans = 0
    for c in comb:
        if c[1] - c[0] == c[2] - c[1]:
            continue
        if s[c[0]] != s[c[1]] and s[c[0]] != s[c[2]] and s[c[1]] != s[c[2]]:
            ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

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


def main():
    n = int(input())
    s = input()
    index = [i for i in range(n)]
    comb = itertools.combinations(index, 3)
    ans = 0
    for c in comb:
        if c[1] - c[0] == c[2] - c[1]:
            continue
        if s[c[0]] != s[c[1]] and s[c[0]] != s[c[2]] and s[c[1]] != s[c[2]]:
            ans += 1
    return ans


if __name__ == '__main__':
    print(main())


Fixed submission:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())