Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N = int(input())
S = input()

# R: 0, G: 1, B: 2

# 後ろにいくつ自分と同じ文字があるかの表を作る
DP = []
for i in range(3):
    DP.append([0] * N)

# 末尾だけループの前に代入
if S[-1] == 'R':
    DP[0][-1] = 1
elif S[-1] == 'G':
    DP[1][-1] = 1
elif S[-1] == 'B':
    DP[2][-1] = 1

for i in reversed(range(0, N - 1)):
    if S[i] == 'R':
        DP[0][i] = DP[0][i + 1] + 1
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'G':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1] + 1
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'B':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1] + 1

#print(DP)

ans = 0
for i in range(0, N - 2):
    s1 = S[i]
    zero_flag = False
    for j in range(i + 1, N - 1):
        # この先3文字目が存在しないことが分かっていたらjをこれ以上進めても意味がない
        if zero_flag:
            break

        s2 = S[j]
        # i と j が一緒か確認
        if s1 == s2:
            # 一緒であればこの2文字目はスキップ
            continue
        else:
            #print('i: {}, j: {}'.format(i, j))
            # 一緒じゃなければ3文字目(s3)を探す
            ij = (S[i], S[j])
            s3 = 'B'
            int_s3 = 2
            if 'R' not in ij:
                s3 = 'R'
                int_s3 = 0
            elif 'G' not in ij:
                s3 = 'G'
                int_s3 = 1

            # 3文字目の後ろの文字数を取得
            s3_back = DP[int_s3][j + 1]
            #print('s3: {}, s3_back = {}'.format(s3, s3_back))

            # 0ならスキップ
            if s3_back == 0:
                zero_flag = True
                continue

            # j + (j - i)の場所に3文字目があれば文字数を1引く
            if j + (j - i) < N:
                if S[j + (j - i)] == s3:
                    s3_back -= 1
                    #print('kita')

            ans += s3_back

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
S = input()

# R, G, Bの数をカウント
R_count = 0
G_count = 0
B_count = 0
for s in list(S):
    if s == 'R':
        R_count += 1
    elif s == 'G':
        G_count += 1
    elif s == 'B':
        B_count += 1

# R, G, Bの全組み合わせ数を求める
ans = R_count * G_count * B_count

# 3文字が等間隔に存在する組み合わせがあれば引いていく
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        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):
# -*- coding: utf-8 -*-

N = int(input())
S = input()

# R: 0, G: 1, B: 2

# 後ろにいくつ自分と同じ文字があるかの表を作る
DP = []
for i in range(3):
    DP.append([0] * N)

# 末尾だけループの前に代入
if S[-1] == 'R':
    DP[0][-1] = 1
elif S[-1] == 'G':
    DP[1][-1] = 1
elif S[-1] == 'B':
    DP[2][-1] = 1

for i in reversed(range(0, N - 1)):
    if S[i] == 'R':
        DP[0][i] = DP[0][i + 1] + 1
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'G':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1] + 1
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'B':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1] + 1

#print(DP)

ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        # i と j が一緒か確認
        if S[i] == S[j]:
            # 一緒であればこの2文字目はスキップ
            continue
        else:
            #print('i: {}, j: {}'.format(i, j))
            # 一緒じゃなければ3文字目(s3)を探す
            ij = (S[i], S[j])
            s3 = 'B'
            int_s3 = 2
            if 'R' not in ij:
                s3 = 'R'
                int_s3 = 0
            elif 'G' not in ij:
                s3 = 'G'
                int_s3 = 1

            # 3文字目の後ろの文字数を取得
            s3_back = DP[int_s3][j + 1]
            #print('s3: {}, s3_back = {}'.format(s3, s3_back))

            # 0ならスキップ
            if s3_back == 0:
                continue

            # j + (j - i)の場所に3文字目があれば文字数を1引く
            if j + (j - i) < N:
                if S[j + (j - i)] == s3:
                    s3_back -= 1
                    #print('kita')

            ans += s3_back

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
S = input()

# R, G, Bの数をカウント
R_count = 0
G_count = 0
B_count = 0
for s in list(S):
    if s == 'R':
        R_count += 1
    elif s == 'G':
        G_count += 1
    elif s == 'B':
        B_count += 1

# R, G, Bの全組み合わせ数を求める
ans = R_count * G_count * B_count

# 3文字が等間隔に存在する組み合わせがあれば引いていく
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)