Buggy python submission (verdict: Time Limit Exceeded):
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

for r in r_set:
    for g in g_set:
        for b in b_set:
            mi = min(r, g, b)
            ma = max(r, g, b)
            set_re = {r, g, b} - {mi, ma}
            re = list(set_re)[0]
            if re - mi != ma - re:
                ans += 1

print(ans) 

Fixed submission:
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

len_b = len(b_set)

for r in r_set:
    for g in g_set:
        diff = abs(r - g)
        minus = set()
        if r < g:
            if g + diff in b_set:
                minus.add(g+diff)
            if r - diff in b_set:
                minus.add(r-diff)
        else:
            if r + diff in b_set:
                minus.add(r+diff)
            if g - diff in b_set:
                minus.add(g-diff)
        if (r + g) / 2 in b_set:
            minus.add((r+g)/2)
        ans += len_b - len(minus)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = int(input())
n = input()
ans = 0
for a in range(s):
    p = n[a]
    for b in range(a+1, s):
        q = n[b]
        if p != q:
            for c in range(b+1, s):
                if (b - a) != (c - b):
                    r = n[c]
                    if p != r and q!= r:
                        ans += 1
print(ans)

Fixed submission:
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

len_b = len(b_set)

for r in r_set:
    for g in g_set:
        diff = abs(r - g)
        minus = set()
        if r < g:
            if g + diff in b_set:
                minus.add(g+diff)
            if r - diff in b_set:
                minus.add(r-diff)
        else:
            if r + diff in b_set:
                minus.add(r+diff)
            if g - diff in b_set:
                minus.add(g-diff)
        if (r + g) / 2 in b_set:
            minus.add((r+g)/2)
        ans += len_b - len(minus)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = int(input())
n = input()
ans = 0
for a in range(s):
    p = n[a]
    for b in range(a+1, s):
        q = n[b]
        if p != q:
            for c in range(b+1, s):
                r = n[c]
                if p != r and q!= r and (b - a) != (c - b):
                    ans += 1
print(ans)

Fixed submission:
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

len_b = len(b_set)

for r in r_set:
    for g in g_set:
        diff = abs(r - g)
        minus = set()
        if r < g:
            if g + diff in b_set:
                minus.add(g+diff)
            if r - diff in b_set:
                minus.add(r-diff)
        else:
            if r + diff in b_set:
                minus.add(r+diff)
            if g - diff in b_set:
                minus.add(g-diff)
        if (r + g) / 2 in b_set:
            minus.add((r+g)/2)
        ans += len_b - len(minus)

print(ans)