Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
for r in R:
    for g in G:
        if g - abs(r - g) in B:
            rem += 1
        if r - abs(g - r) in B:
            rem += 1
        if (g + r) / 2 in B:
            rem += 1
        if g + abs(r - g) in B:
            rem += 1
        if r + abs(g - r) in B:
            rem += 1


print(len(R)*len(G)*len(B) - rem)


Fixed submission:
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


print(len(R)*len(G)*len(B) - rem)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):    
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
# r, g, b = 0, 0, 0
# while (r < len(R) and g < len(G) and b < len(B)):
#     target = min(R[r], g, b)
#     if (abs(R[r] - g) != abs(g - b) and abs(R[r] - g) != abs(R[r] - b) and abs(g - b) != abs(R[r] - b)):
#         ans += 1
#     if target == R[r]:
#         r += 1
#     if target == g:
#         g += 1
#     if target == b:
#         b += 1
for r in R:
    for g in G:
        for b in B:
            if (abs(r - g) != abs(g - b) and abs(r - g) != abs(r - b) and abs(g - b) != abs(r - b)):
                ans += 1
            
print(ans)
    

Fixed submission:
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


print(len(R)*len(G)*len(B) - rem)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = 0
for i in range(len(S)):    
    for j in range(i, len(S)): 
        if (S[i] != S[j]):
            for k in range(j, len(S)):
                if(S[k] != S[i] and S[k] != S[j]):
                    if (j - i != k - j):
                        ans+=1
print(ans)
    

Fixed submission:
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


print(len(R)*len(G)*len(B) - rem)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = 0
for i in range(len(S)):    
    for j in range(i, len(S)):    
        for k in range(j, len(S)):
            if(S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
                if (j - i != k - j):
                    ans+=1
print(ans)
    

Fixed submission:
N = int(input())
S = input()

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


print(len(R)*len(G)*len(B) - rem)