Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
cum = [[0]*3 for _ in range(N+1)]
for i,s in enumerate(S,1):
    cum[i]=cum[i-1].copy()
    if s=='R':
        cum[i][0]+=1
    elif s=='G':
        cum[i][1]+=1
    elif s=='B':
        cum[i][2]+=1


for i in range(len(S)):
  for j in range(i+1,len(S)):
    if S[i]==S[j]:
      continue
    else:
      x = 'RGB'.replace(S[i],'').replace(S[j],'')
      if x=='R':
        ans+=cum[-1][0]-cum[j][0]
        y = 2*j-i
        if y<S_l:
          if S[y]=='R':
            ans-=1
      elif x=='G':
        ans+=cum[-1][1]-cum[j][1]
        y = 2*j-i
        if y<S_l:
          if S[y]=='G':
            ans-=1
      else:
        ans+=cum[-1][2]-cum[j][2]
        y = 2*j-i
        if y<S_l:
          if S[y]=='B':
            ans-=1
 
print(ans)

Fixed submission:
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
from collections import deque
R = deque([])
G = deque([])
B  = deque([])
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    elif s=='B':
        B.append(i)

def res(X,Y,S,i,a,b):
  x = X.copy()
  ret = 0
  for y in Y:
    if len(x)!=0:
      while x[0]<y:
        x.popleft()
        if len(x)==0:
          break
    ret+=len(x)
    tmp = 2*y-i
    if tmp>=S_l:
      continue
    if S[tmp]==a:
      ret-=1
  
  y = Y.copy()
  for x in X:
    if len(y)!=0:
      while y[0]<x:
        y.popleft()
        if len(y)==0:
          break
    ret+=len(y)
    tmp = 2*x-i
    if tmp>=S_l:
      continue
    if S[tmp]==b:
      ret-=1

      
  return ret
    
for i in range(len(S)):
    if S[i]=='R':
      r = R.popleft()
      ans += res(G,B,S,i,'G','B')
    elif S[i]=='G':
      g = G.popleft()
      ans += res(R,B,S,i,'R','B')
    else:
      b = B.popleft()
      ans += res(R,G,S,i,'R','G')
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
from collections import deque
R = deque([])
G = deque([])
B  = deque([])
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    elif s=='B':
        B.append(i)

def res(X,Y,S,i,a,b):
  x = X.copy()
  ret = 0
  for y in Y:
    if len(x)!=0:
      while x[0]<y:
        x.popleft()
        if len(x)==0:
          break
    ret+=len(x)
    tmp = 2*y-i
    if tmp>=S_l:
      continue
    if S[tmp]==a:
      ret-=1
  
  y = Y.copy()
  for x in X:
    if len(y)!=0:
      while y[0]<x:
        y.popleft()
        if len(y)==0:
          break
    ret+=len(y)
    tmp = 2*x-i
    if tmp>=S_l:
      continue
    if S[tmp]==b:
      ret-=1

      
  return ret
    
for i in range(len(S)):
    if S[i]=='R':
      r = R.popleft()
      ans += res(G,B,S,i,'G','B')
    elif S[i]=='G':
      g = G.popleft()
      ans += res(R,B,S,i,'R','B')
    else:
      b = B.popleft()
      ans += res(R,G,S,i,'R','G')
      
print(ans)

Fixed submission:
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
from collections import deque
R = deque([])
G = deque([])
B  = deque([])
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    elif s=='B':
        B.append(i)

def res(X,Y,S,i,a,b):
  x = X.copy()
  ret = 0
  for y in Y:
    if len(x)!=0:
      while x[0]<y:
        x.popleft()
        if len(x)==0:
          break
    ret+=len(x)
    tmp = 2*y-i
    if tmp>=S_l:
      continue
    if S[tmp]==a:
      ret-=1
  
  y = Y.copy()
  for x in X:
    if len(y)!=0:
      while y[0]<x:
        y.popleft()
        if len(y)==0:
          break
    ret+=len(y)
    tmp = 2*x-i
    if tmp>=S_l:
      continue
    if S[tmp]==b:
      ret-=1

      
  return ret
    
for i in range(len(S)):
    if S[i]=='R':
      r = R.popleft()
      ans += res(G,B,S,i,'G','B')
    elif S[i]=='G':
      g = G.popleft()
      ans += res(R,B,S,i,'R','B')
    else:
      b = B.popleft()
      ans += res(R,G,S,i,'R','G')
      
print(ans)