Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())
S = input()
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)

for r in R_index:
    for g in G_index:
        for b in B_index:
            l = [r,g,b]
            l.sort()
            if l[1] - l[0] == l[2] -l[1]:
                ans -= 1
print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

# def TLE():
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)
for i in range(N):
    if S[i] == 'R':
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans -= 1
                # ans += sum(x > j for x in B_index) -1
            continue
            # else:
                # ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans -= 1
                # ans += sum(x > j for x in G_index) -1
            continue
            # else:
                # ans += sum(x > j for x in G_index)
    elif S[i] == 'G':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans -= 1
                # ans += sum(x > j for x in B_index) -1
            continue
            # else:
                # ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans -= 1
                # ans += sum(x > j for x in R_index) -1
            continue
            # else:
                # ans += sum(x > j for x in R_index)
    elif S[i] == 'B':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans -= 1
                # ans += sum(x > j for x in G_index) -1
            continue
            # else:
                # ans += sum(x > j for x in G_index) 
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans -= 1
                # ans += sum(x > j for x in R_index) -1
            continue
            # else:
                # ans += sum(x > j for x in R_index)



# ans = 0
def TLE2():
    R_index = [n for n, v in enumerate(S) if v == 'R']
    G_index = [n for n, v in enumerate(S) if v == 'G']
    B_index = [n for n, v in enumerate(S) if v == 'B']
    ans = len(R_index) *len(G_index) *len(B_index)
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == 'R' and S[j] =='G':
                # ans += sum(x > j for x in B_index)
                if j+j-i in B_index:
                    ans -= 1
                continue
            elif S[i] == 'R' and S[j] =='B':
                # ans += sum(x > j for x in G_index)
                if j+j-i in G_index:
                    ans -= 1
                continue
            elif S[i] == 'G' and S[j] =='R':
                # ans += sum(x > j for x in B_index)
                if j+j-i in B_index:
                    ans -= 1
                continue
            elif S[i] == 'G' and S[j] =='B':
                # ans += sum(x > j for x in R_index)
                if j+j-i in R_index:
                    ans -= 1
                continue
            elif S[i] == 'B' and S[j] =='R':
                # ans += sum(x > j for x in G_index)
                if j+j-i in G_index:
                    ans -= 1
                continue
            elif S[i] == 'B' and S[j] =='G':
                # ans += sum(x > j for x in R_index)
                if j+j-i in R_index:
                    ans -= 1
                continue
            # for k in range(j+1, N):
            #     if j-i == k-j:
            #         continue
            #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            #         ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())
S = input()
# ans = 0
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)
for i in range(N):
    for j in range(i+1, N):
        if S[i] == 'R' and S[j] =='G':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'R' and S[j] =='B':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='R':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='B':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='R':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='G':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        # for k in range(j+1, N):
        #     if j-i == k-j:
        #         continue
        #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        #         ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())
S = input()
ans = 0
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
for i in range(N):
    for j in range(i+1, N):
        if S[i] == 'R' and S[j] =='G':
            ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'R' and S[j] =='B':
            ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='R':
            ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='B':
            ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='R':
            ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='G':
            ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        # for k in range(j+1, N):
        #     if j-i == k-j:
        #         continue
        #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        #         ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

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

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = 0
for i in range(N):
    if S[i] == 'R':
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans += sum(x > j for x in B_index) -1
            else:
                ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans += sum(x > j for x in G_index) -1
            else:
                ans += sum(x > j for x in G_index)
    elif S[i] == 'G':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans += sum(x > j for x in B_index) -1
            else:
                ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans += sum(x > j for x in R_index) -1
            else:
                ans += sum(x > j for x in R_index)
    elif S[i] == 'B':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans += sum(x > j for x in G_index) -1
            else:
                ans += sum(x > j for x in G_index) 
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans += sum(x > j for x in R_index) -1
            else:
                ans += sum(x > j for x in R_index)
print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)