Buggy python submission (verdict: Time Limit Exceeded):
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y, z, a):
  if x==y and z==a:
    return 1
  return 0

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)
    
rgb = r * g * b

# Rが最小
for i in r0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'G', S[k], 'B')
      equal += func2(S[j], 'B', S[k], 'G')

# Gが最小
for i in g0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'R', S[k], 'B')
      equal += func2(S[j], 'B', S[k], 'R')

# Bが最小
for i in b0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'R', S[k], 'G')
      equal += func2(S[j], 'G', S[k], 'R')

print(rgb - equal)

Fixed submission:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b

# R<G<B
equal += func3(r0, g0, S, 'B')
# R<B<G
equal += func3(r0, b0, S, 'G')
# G<R<B
equal += func3(g0, r0, S, 'B')
# G<B<R
equal += func3(g0, b0, S, 'R')
# B<R<G
equal += func3(b0, r0, S, 'G')
# B<G<R
equal += func3(b0, g0, S, 'R')

print(rgb - equal)

Buggy python submission (verdict: Time Limit Exceeded):
def func1(x, y):
  if x == y:
    return 1 
  return 0

def func2(x, y, z):
  if x=='R' and y=='G' and z=='B':
    return 1
  return 0

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0

for i in range(N):
  r += func1(S[i], 'R')
  g += func1(S[i], 'G')
  b += func1(S[i], 'B')
    
rgb = r * g * b

for i in range(0, N-2):
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[i], S[j], S[k])
      equal += func2(S[i], S[k], S[j])
      equal += func2(S[j], S[i], S[k])
      equal += func2(S[j], S[k], S[i])
      equal += func2(S[k], S[i], S[j])
      equal += func2(S[k], S[j], S[i])

print(rgb - equal)

Fixed submission:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b

# R<G<B
equal += func3(r0, g0, S, 'B')
# R<B<G
equal += func3(r0, b0, S, 'G')
# G<R<B
equal += func3(g0, r0, S, 'B')
# G<B<R
equal += func3(g0, b0, S, 'R')
# B<R<G
equal += func3(b0, r0, S, 'G')
# B<G<R
equal += func3(b0, g0, S, 'R')

print(rgb - equal)

Buggy python submission (verdict: Time Limit Exceeded):
def judge(x, y, z):
  if y-x != z-y and y>x and z>y:
    return 1
  
  return 0


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

count = 0

for i in range(0, N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      if S[i]=='R' and S[j]=='G' and S[k]=='B':
        count += judge(i, j, k)
      elif S[i]=='R' and S[j]=='B' and S[k]=='G':
        count += judge(i, j, k)
      elif S[i]=='G' and S[j]=='R' and S[k]=='B':
        count += judge(i, j, k)
      elif S[i]=='G' and S[j]=='B' and S[k]=='R':
        count += judge(i, j, k)
      elif S[i]=='B' and S[j]=='R' and S[k]=='G':
        count += judge(i, j, k)
      elif S[i]=='B' and S[j]=='G' and S[k]=='R':
        count += judge(i, j, k)        
        
print(count)

Fixed submission:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b

# R<G<B
equal += func3(r0, g0, S, 'B')
# R<B<G
equal += func3(r0, b0, S, 'G')
# G<R<B
equal += func3(g0, r0, S, 'B')
# G<B<R
equal += func3(g0, b0, S, 'R')
# B<R<G
equal += func3(b0, r0, S, 'G')
# B<G<R
equal += func3(b0, g0, S, 'R')

print(rgb - equal)

Buggy python submission (verdict: Time Limit Exceeded):
# N, Sをinput
# Sの'R', 'G', 'B'のインデックスを、リストr, g, bに格納
# r, g, bをそれぞれループさせる
# r, g, bの各要素の差を比較し、r-g!=g-bの場合、countに加算

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

r = []
g = []
b = []
count = 0

for i in range(N):
  if S[i] == 'R':
    r.append(i)
  elif S[i] == 'G':
    g.append(i)
  elif S[i] == 'B':
    b.append(i)

for x in r:
  for y in g:
    for z in b:
      if x-y != y-z and x>y and y>z:
        count += 1
      if x-z != z-y and x>z and z>y:
        count += 1
      if y-x != x-z and y>x and x>z:
        count += 1
      if y-z != z-x and y>z and z>x:
        count += 1
      if z-x != x-y and z>x and x>y:
        count += 1
      if z-y != y-x and z>y and y>x:
        count += 1
        
print(count)

Fixed submission:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b

# R<G<B
equal += func3(r0, g0, S, 'B')
# R<B<G
equal += func3(r0, b0, S, 'G')
# G<R<B
equal += func3(g0, r0, S, 'B')
# G<B<R
equal += func3(g0, b0, S, 'R')
# B<R<G
equal += func3(b0, r0, S, 'G')
# B<G<R
equal += func3(b0, g0, S, 'R')

print(rgb - equal)