Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
Rs = set(R)
Bs = set(B)
Gs = set(G)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Bs)
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Gs)
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & Bs)
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & Gs)
print(res)


Fixed submission:
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
Rs = set(R)
Bs = set(B)
Gs = set(G)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Bs)
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Gs)
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & Bs)
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & Gs)
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(B))
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(G))
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & set(B))
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & set(G))
print(res)

Fixed submission:
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
Rs = set(R)
Bs = set(B)
Gs = set(G)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Bs)
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Gs)
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & Bs)
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & Gs)
print(res)