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

R = [0] * N
G = [0] * N
B = [0] * N

for i in range(N):
    if S[i] == 'R':
        R[i] = 1
    elif S[i] == 'G':
        G[i] = 1
    else:
        B[i] = 1

sumR = sum(R)
sumG = sum(G)
sumB = sum(B)

cnt = 0
for i in range(N):
    sumR -= R[i]
    sumG -= G[i]
    sumB -= B[i]
    sum_r = sumR
    sum_g = sumG
    sum_b = sumB
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
            if S[i] == 'B':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
print(cnt)


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

R = [0] * N
G = [0] * N
B = [0] * N

for i in range(N):
    if S[i] == 'R':
        R[i] = 1
    elif S[i] == 'G':
        G[i] = 1
    else:
        B[i] = 1

sumR = sum(R)
sumG = sum(G)
sumB = sum(B)

cnt = 0
for i in range(N):
    sumR -= R[i]
    sumG -= G[i]
    sumB -= B[i]
    sum_r = sumR
    sum_g = sumG
    sum_b = sumB
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        check_index = j + (j - i)
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
            if S[i] == 'B':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
print(cnt)

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

R = [0] * N
G = [0] * N
B = [0] * N

for i in range(N):
    if S[i] == 'R':
        R[i] = 1
    elif S[i] == 'G':
        G[i] = 1
    else:
        B[i] = 1

cnt = 0
for i in range(N):
    sum_r = sum(R) - sum(R[:(i + 1)])
    sum_g = sum(G) - sum(G[:(i + 1)])
    sum_b = sum(B) - sum(B[:(i + 1)])
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
            if S[i] == 'B':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
                    
print(cnt)

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

R = [0] * N
G = [0] * N
B = [0] * N

for i in range(N):
    if S[i] == 'R':
        R[i] = 1
    elif S[i] == 'G':
        G[i] = 1
    else:
        B[i] = 1

sumR = sum(R)
sumG = sum(G)
sumB = sum(B)

cnt = 0
for i in range(N):
    sumR -= R[i]
    sumG -= G[i]
    sumB -= B[i]
    sum_r = sumR
    sum_g = sumG
    sum_b = sumB
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        check_index = j + (j - i)
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
            if S[i] == 'B':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
print(cnt)