Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
N = int(input())
S = 'D' + input()


rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1


dict={'R':0, 'G':1, 'B':2}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = rgb_count[1][0]*rgb_count[1][1]*rgb_count[1][2]
for i in range(2,N):
    ran = min([i-1, N-i])
    if ran < 1: continue
    for k in range(1, ran+1):
        if set([S[i-k], S[i], S[i+k]]) == set(['G', 'B', 'R']):
            ret -= 1

print(ret)

Fixed submission:
# coding: utf-8
N = int(input())
S = 'D' + input()


rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1


dict={'R':1, 'G':2, 'B':4}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = rgb_count[1][0]*rgb_count[1][1]*rgb_count[1][2]
for i in range(2,N):
    ran = min([i-1, N-i])
    if ran < 1: continue
    for k in range(1, ran+1):
        if dict[S[i-k]]+dict[S[i]]+dict[S[i+k]] == 7:
            ret -= 1

print(ret)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
N = int(input())
S = 'D' + input()

rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1

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


dict={'R':0, 'G':1, 'B':2}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = 0
for i in range(2,N):
    search = dic[S[i]]
    ret += (rgb_count[1][search[0]] - rgb_count[i][search[0]]) * rgb_count[i+1][search[1]]
    ret += (rgb_count[1][search[1]] - rgb_count[i][search[1]]) * rgb_count[i+1][search[0]]
    ran = min([i-1, N-i])

    if ran < 1: continue
    for k in range(1, ran+1):
        if set([S[i-k], S[i], S[i+k]]) == set(['G', 'B', 'R']):
            ret -= 1

print(ret)

Fixed submission:
# coding: utf-8
N = int(input())
S = 'D' + input()


rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1


dict={'R':1, 'G':2, 'B':4}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = rgb_count[1][0]*rgb_count[1][1]*rgb_count[1][2]
for i in range(2,N):
    ran = min([i-1, N-i])
    if ran < 1: continue
    for k in range(1, ran+1):
        if dict[S[i-k]]+dict[S[i]]+dict[S[i+k]] == 7:
            ret -= 1

print(ret)