Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  s = input()
  s1 = []
  s2 = []
  s3 = []
  ss = [("R",s.count("R")),("G",s.count("G")),("B",s.count("B"))]
  ss = sorted(ss,key=lambda x:x[1])
  ans = ss[0][1]*ss[1][1]*ss[2][1]
  for i in range(n):
    if s[i]==ss[2][0]:
      s3.append(i)
    elif s[i]==ss[1][0]:
      s2.append(i)
    elif s[i]==ss[0][0]:
      s1.append(i)
      
  for i in s1:
    for j in s2:
      if i*2-j in s3:
        ans -= 1
      if j*2-i in s3:
        ans -= 1
      if (i+j)%2==0 and (i+j)/2 in s3:
        ans -= 1
  print(ans)
main()

Fixed submission:
def main():
  n = int(input())
  s = input()
  ans = s.count("R")*s.count("G")*s.count("B")
  for i in range(n-2):
    for j in range(i+1,n-1):
      k = j + j - i
      if k >= n:
        break
      elif s[i] != s[j] and s[i] != s[k] and s[k] != s[j]:
        ans -=1
  print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  s = input()
  s1 = []
  s2 = []
  s3 = []
  ss = [("R",s.count("R")),("G",s.count("G")),("B",s.count("B"))]
  ss = sorted(ss,key=lambda x:x[1])
  ans = ss[0][1]*ss[1][1]*ss[2][1]
  for i in range(n):
    if s[i]==ss[0][0]:
      s1.append(i)
    if s[i]==ss[1][0]:
      s2.append(i)
    if s[i]==ss[2][0]:
      s3.append(i)
      
  for i in s1:
    for j in s2:
      if i*2-j in s3:
        ans -= 1
      if j*2-i in s3:
        ans -= 1
      if (i+j)/2 in s3:
        ans -= 1
  print(ans)
main()

Fixed submission:
def main():
  n = int(input())
  s = input()
  ans = s.count("R")*s.count("G")*s.count("B")
  for i in range(n-2):
    for j in range(i+1,n-1):
      k = j + j - i
      if k >= n:
        break
      elif s[i] != s[j] and s[i] != s[k] and s[k] != s[j]:
        ans -=1
  print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  s = input()
  r = []
  g = []
  b = []
  for i in range(n):
    if s[i]=="R":
      r.append(i)
    if s[i]=="G":
      g.append(i)
    if s[i]=="B":
      b.append(i)
  ans = 0
  for i in r:
    for j in g:
      for k in b:
        l = sorted([i,j,k])
        if l[1]-l[0] != l[2]-l[1]:
          ans += 1
  print(ans)
main()

Fixed submission:
def main():
  n = int(input())
  s = input()
  ans = s.count("R")*s.count("G")*s.count("B")
  for i in range(n-2):
    for j in range(i+1,n-1):
      k = j + j - i
      if k >= n:
        break
      elif s[i] != s[j] and s[i] != s[k] and s[k] != s[j]:
        ans -=1
  print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  s = input()
  r = []
  g = []
  b = []
  for i in range(n):
    if s[i]=="R":
      r.append(i)
    if s[i]=="G":
      g.append(i)
    if s[i]=="B":
      b.append(i)
  ans = 0
  for i in r:
    for j in g:
      for k in b:
        l = sorted([i,j,k])
        if l[1]-l[0] != l[2]-l[1]:
          ans += 1
  print(ans)
main()

Fixed submission:
def main():
  n = int(input())
  s = input()
  ans = s.count("R")*s.count("G")*s.count("B")
  for i in range(n-2):
    for j in range(i+1,n-1):
      k = j + j - i
      if k >= n:
        break
      elif s[i] != s[j] and s[i] != s[k] and s[k] != s[j]:
        ans -=1
  print(ans)
main()