Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def main():

    N = int(input())
    S = input()
    count = Counter(S)

    # print(count)
    ans = 0
    R = count["R"]
    G = count["G"]
    B = count["B"]
    ans = R * G * B

    # for i in range(N - 2):
    #     if S[i] == "R":
    #         for j in range(i + 1, N - 1):
    #             if S[j] == "G":
    #                 b_count = Counter(S[j:])["B"]
    #                 ans += b_count

    # print(R, G, B, ans)

    for i in range(N):
        for j in range(N):
            if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
                ans -= 1
            if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
                ans -= 1
            if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
                ans -= 1
            if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
                ans -= 1
            if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
                ans -= 1
            if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
from collections import Counter

N = int(input())
S = input()
count = Counter(S)

# print(count)
ans = 0
R = count["R"]
G = count["G"]
B = count["B"]
ans = R * G * B

# for i in range(N - 2):
#     if S[i] == "R":
#         for j in range(i + 1, N - 1):
#             if S[j] == "G":
#                 b_count = Counter(S[j:])["B"]
#                 ans += b_count

# print(R, G, B, ans)

for i in range(N):
    for j in range(N):
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

N = int(input())
S = input()
count = Counter(S)

# print(count)
ans = 0
R = count["R"]
G = count["G"]
B = count["B"]
ans = R * G * B

# for i in range(N - 2):
#     if S[i] == "R":
#         for j in range(i + 1, N - 1):
#             if S[j] == "G":
#                 b_count = Counter(S[j:])["B"]
#                 ans += b_count

# print(R, G, B, ans)

for i in range(N):
    for j in range(N):
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
            ans -= 1
print(ans)


Fixed submission:
from collections import Counter

N = int(input())
S = input()
count = Counter(S)

# print(count)
ans = 0
R = count["R"]
G = count["G"]
B = count["B"]
ans = R * G * B

# for i in range(N - 2):
#     if S[i] == "R":
#         for j in range(i + 1, N - 1):
#             if S[j] == "G":
#                 b_count = Counter(S[j:])["B"]
#                 ans += b_count

# print(R, G, B, ans)

for i in range(N):
    for j in range(N):
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
            ans -= 1
print(ans)