Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
N = int(input())
S = input()
r,g,b = 0,0,0
for i in S:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  else:
    b += 1
minus = 0
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    k = 2 * j - i
    if k >= N:
      continue
    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      minus += 1
print(r*g*b - minus)

Solution (Python):
import math

n = int(input())
s = input()

R, G, B = s.count("R"), s.count("G"), s.count("B")
ans = R * G * B

for i in range(n):
    for j in range(n):
        c1 = i
        c2 = i + j
        c3 = i + j * 2
        if c3 >= n: break
        if c1 == c2 == c3: continue
        if s[c1] != s[c2] and s[c2] != s[c3] and s[c1] != s[c3]:
            ans -= 1

print(ans)

Solution (Python):
import math

n = int(input())
s = input()

R, G, B = s.count("R"), s.count("G"), s.count("B")
ans = R * G * B

for i in range(n):
    for j in range(n):
        c1 = i
        c2 = i + j
        c3 = i + j * 2
        if c3 >= n: continue
        if c1 == c2 == c3: continue
        if s[c1] != s[c2] and s[c2] != s[c3] and s[c1] != s[c3]:
            ans -= 1

print(ans)

Solution (Python):
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ct=r*g*b
for i in range(n-2):
    for j in range(i+1,n-1):
        k=2*j-i
        if k<=n-1:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ct-=1
print(ct)

Solution (Python):
N = int(input())
S = input()

nr = 0
ng = 0
nb = 0

for i in range(0, N):
    if S[i] == "R":
        nr += 1
    elif S[i] == "G":
        ng += 1
    else:
        nb += 1

count = 0

for i in range(0, N-2):
    for j in range(i+1, N-1):
        k = j + (j - i)
        if k < N and ord(S[i]) + ord(S[j]) + ord(S[k]) == ord("R") + ord("G") + ord("B"):
            count += 1

print(nr*ng*nb - count)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
s = input() 

r = [0]*n
g = [0]*n
b = [0]*n

for i in range(n):
    if s[i] == 'R':r[i]+=1
    elif s[i] == 'G':g[i]+=1
    else: b[i]+=1

from itertools import accumulate
r_nokori = list(reversed(r))
r_nokori = list(reversed(list(accumulate(r_nokori))))
g_nokori = list(reversed(g))
g_nokori = list(reversed(list(accumulate(g_nokori))))
b_nokori = list(reversed(b))
b_nokori = list(reversed(list(accumulate(b_nokori))))

ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]: continue

        if (s[i]=='R' and s[j]=='G') or (s[i]=='G' and s[j]=='R'):
            ans += b_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'B': ans-= 1

        if (s[i]=='G' and s[j]=='B') or (s[i]=='B' and s[j]=='G'):
            ans += r_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'R': ans-= 1

        if (s[i]=='B' and s[j]=='R') or (s[i]=='R' and s[j]=='B'):
            ans += g_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'G': ans-= 1

print(ans)

Solution (Python):
n = int(input())
s = list(input())
cnt = 0
cnt += s.count('R')*s.count('G')*s.count('B')
for i in range(1,n-1):
    if s[i] == 'R':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='G':
                cnt -=1
    if s[i] == 'G':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'R' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='R':
                cnt -=1
    if s[i] == 'B':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='R':
                cnt -=1
            if s[i-j] == 'R' and s[i+j]=='G':
                cnt -=1
print(cnt)

Solution (Python):
n=int(input())




s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b
for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k>=n:
            continue
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            ans-=1
print(ans)

Solution (Python):
n=int(input())




s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b
for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k>=n:
            continue
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            ans-=1
print(ans)

Solution (Python):
N = int(input())
S = input()
sum = 0
r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1

sum = r*g*b

for i in range(N):
    for j in range(N):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            break

print(sum)

Solution (Python):


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

count = 0
r = S.count("R")
g = S.count("G")
b = S.count("B")

count = r*g*b

for i in range(N - 1):
    for j in range(i+1, N):
        k = 2 * j -i
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            count -= 1

print(count)

Solution (Python):
N=int(input())
S=input()
count=0
r=S.count("R")
g=S.count("G")
b=S.count("B")

count+=r*g*b

for i in range(0,N-1):
	for j in range(i+1,N):
		k=2*j-i
		if k>=N:break
		if S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]:
			count-=1
print(count)

Solution (Python):
N = int(input())
S = str(input())

r = S.count("R")
g = S.count("G")
b = S.count("B")

ct = r*g*b

for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j-i
        if k<=N-1:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ct-=1
print(ct)

Solution (Python):
N = int(input())
S = str(input())

r = S.count("R")
g = S.count("G")
b = S.count("B")

ct = r*g*b

for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j-i
        if k<=N-1:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ct-=1
print(ct)

Solution (Python):
n = int(input())
s = list(input())
r = [i for i, x in enumerate(s) if x == "R"]
g = [i for i, x in enumerate(s) if x == "G"]
b = [i for i, x in enumerate(s) if x == "B"]
ans = len(r) * len(g) * len(b)

for i in r:
    for j in g:
        i_ = min(i, j)
        j_ = max(i, j)
        div = (j_ - i_)
        k_1 = i_ - div
        k_2 = div // 2 + i_
        k_3 = div + j_
        if k_1 >= 0 and s[k_1] == "B":
            ans -= 1
        if div % 2 == 0 and s[k_2] == "B":
            ans -= 1
        if k_3 < n and s[k_3] == "B":
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
cnt = Counter(S)
total = cnt["R"] * cnt["G"] * cnt["B"]
S = "

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

print(total - sub)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r*g*b
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            total -= 1
print(total)

Solution (Python):
n = int(input())
sl = list(input())

ans = sl.count("R") * sl.count("G") * sl.count("B")

for i in range(n):
    for j in range(i+1, n):
        k = 2 * j - i
        if k < n:
            if len({sl[i], sl[j], sl[k]}) ==  3:
                ans -= 1

print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
res = r*g*b
for i in range(n-1):
    for j in range(i+1,n):
        k = 2*j - i
        if k < n :
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                res -= 1
print(res)

Solution (Python):
N=int(input())
S=input()

r=S.count('R')
g=S.count('G')
b=S.count('B')

ad=r*g*b
mi=0

for i in range(N):
    for j in range(i+1,N):
        k=j+(j-i)
        if k<N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                mi+=1

print(ad-mi)

Solution (Python):
n=int(input())
s=input()
r=g=b=0
for i in s:
  if i=="R":r+=1
  elif i=="G":g+=1
  else:b+=1

ans=r*g*b
for i in range(n):
  for j in range(i+1,n):
    k_ = j+(j-i)
    if(k_ >=n):
      break
    ans -= int(s[i]!= s[j] and s[i] != s[k_] and s[j] != s[k_])

print(ans)

Solution (Python):
n = int(input())
s = input()
s = s.replace('R', '0') 
s = s.replace('G', '1')
s = s.replace('B', '2')

t = [[0] * (n + 5) for _ in range(3)]

for i in range(n):
    for j in range(3):
        t[j][i + 1] = t[j][i] + (s[i] == chr(48 + j))

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        h = 99 - (ord(s[i]) + ord(s[j]))
        ans += t[h][n] - t[h][j]
        if 2 * j - i < n and s[2 * j - i] == chr(48 + h): ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
s = s.replace('R', '0')
s = s.replace('G', '1')
s = s.replace('B', '2')

s0 = [0] * (n + 5)
s1 = [0] * (n + 5)
s2 = [0] * (n + 5)

for i in range(n):
    s0[i + 1] = s0[i] + (s[i] == '0')
    s1[i + 1] = s1[i] + (s[i] == '1')
    s2[i + 1] = s2[i] + (s[i] == '2')





ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        h = 99 - (ord(s[i]) + ord(s[j]))
        if h == 0:
            ans += s0[n] - s0[j]
            if 2 * j - i < n and s[2 * j - i] == '0': ans -= 1
        if h == 1:
            ans += s1[n] - s1[j]
            if 2 * j - i < n and s[2 * j - i] == '1': ans -= 1
        if h == 2:
            ans += s2[n] - s2[j]
            if 2 * j - i < n and s[2 * j - i] == '2': ans -= 1
        
print(ans)

Solution (Python):
n = int(input())
s = input()

R, G, B = 0, 0, 0
for i in range(n):
	if s[i] == 'R':
		R += 1
	elif s[i] == 'G':
		G += 1
	else:
		B += 1

res = R * G * B

for i in range(n):
	for j in range(i+1, n):
		temp = 2*j - i
		if temp < n and s[i] != s[j] and s[j] != s[temp] and s[temp] != s[i]:
			res -= 1
print(res)

Solution (Python):
N = int(input())
S = input()


R = S.count('R')
G = S.count('G')
B = S.count('B')


ad = R * G * B
ed = 0

for i in range(0, N-1):
  for j in range(i+1, N):
    
    P = j+(j-i)
    if P < N:
      if S[i] != S[j] and S[j] != S[P] and S[P] != S[i]:
        ed += 1

print(ad-ed)

Solution (Python):
from itertools import permutations as pm

def main():
    n = int(input())
    s = input()

    red = [0 for i in range(n+1)]
    green = [0 for i in range(n+1)]
    blue = [0 for i in range(n+1)]

    for i in range(2,n+1):
        if s[i-2]=='R':
            red[i] = red[i-1] + 1
        else:
            red[i] = red[i-1]
        if s[i-2]=='G':
            green[i] = green[i-1] + 1
        else:
            green[i] = green[i-1]
        if s[i-2]=='B':
            blue[i] = blue[i-1] + 1
        else:
            blue[i] = blue[i-1]
    
    
    

    
    
    


    ans = 0
    for i in range(1,n+1):
        for j in range(i+2,n+1):

            if s[i-1]+s[j-1] == "RB" or s[i-1]+s[j-1] =="BR":
                ans += green[j] - green[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "G":
                    ans -= 1
            elif s[i-1]+s[j-1] == "RG" or s[i-1]+s[j-1] =="GR":
                ans += blue[j] - blue[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "B":
                    ans -= 1
            elif s[i-1]+s[j-1] == "BG" or s[i-1]+s[j-1] =="GB":
                ans += red[j] - red[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "R":
                    ans -= 1

    print(ans)

if __name__== "__main__":
    main()

Solution (Python):
n = int(input())
s = input()

r = s.count("R")
g = s.count("G")
b = s.count("B")

ans = r*g*b

for i in range(n-2):
    for j in range(1,n):
        if(i + (j*2) > n-1):
            break
        if(s[i] != s[i+j] and s[i] != s[i+j+j] and  s[i+j] != s[i+j+j]):
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
sum = 0
r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1

sum = r*g*b

for i in range(N):
    for j in range((N-i)//2 +1):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            continue

print(sum)

Solution (Python):
def checkRGB(a,b,c):
    if a == b or b == c or c == a:
        return 0
    else:
        return 1

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

if N<4:
    print(0)
    exit()
sum1 = S.count("R")*S.count("G")*S.count("B")
sum2 = 0
for i in range(0,N-2):
    left = S[i]
    for j in range(i+1,(N+i)//2+(N+i)%2):
        center = S[j]
        right = S[2*j-i]
        if checkRGB(left,center,right):
            sum2 += 1
print(sum1-sum2)

Solution (Python):
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
s=input()
R=[0]
G=[0]
B=[0]
ans=0

for i in range(N):
  if s[i]=="R":
    R[i]+=1
  elif s[i]=="G":
    G[i]+=1
  else:
    B[i]+=1
  R.append(R[i])
  G.append(G[i])
  B.append(B[i])
del R[-1]
del G[-1]
del B[-1]

 

for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if "R" not in a+b:
      ans+=R[-1]-R[j]
      if k>=N:
        continue
      if s[k]=="R":
        ans-=1
    
    if "G" not in a+b:
      ans+=G[-1]-G[j]
      if k>=N:
        continue
      if s[k]=="G":
        ans-=1

    if "B" not in a+b:
      ans+=B[-1]-B[j]
      if k>=N:
        continue
      if s[k]=="B":
        ans-=1
print(ans)

Solution (Python):
N=int(input())
s=input()
R=s.count("R")
G=s.count("G")
B=s.count("B")
ans=R*G*B
 

for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if k>=N:
      continue
    if s[k]!=a and s[k]!=b:
      ans-=1
print(ans)

Solution (Python):
N=int(input())
s=input()
R=s.count("R")
G=s.count("G")
B=s.count("B")
ans=R*G*B
 

for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if k>=N:
      continue
    if s[k]!=a and s[k]!=b:
      ans-=1
print(ans)

Solution (Python):
N=int(input())
s=input()
R=s.count("R")
G=s.count("G")
B=s.count("B")
ans=R*G*B


for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if "R" not in a+b:
      try:
        if s[k]=="R":
          ans-=1
      except:
        pass
      
    if "G" not in a+b:
      try:
        if s[k]=="G":
          ans-=1
      except:
        pass
      
    if "B" not in a+b:
      try:
        if s[k]=="B":
          ans-=1
      except:
        pass

print(ans)

Solution (Python):
N=int(input())
s=input()
R=[0]
G=[0]
B=[0]
ans=0

for i in range(N):
  if s[i]=="R":
    R[i]+=1
  elif s[i]=="G":
    G[i]+=1
  else:
    B[i]+=1
  R.append(R[i])
  G.append(G[i])
  B.append(B[i])
del R[-1]
del G[-1]
del B[-1]



for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if "R" not in a+b:
      ans+=R[-1]-R[j]
      try:
        if s[k]=="R":
          ans-=1
      except:
        pass
    
    if "G" not in a+b:
      ans+=G[-1]-G[j]
      try:
        if s[k]=="G":
          ans-=1
      except:
        pass
        
    if "B" not in a+b:
      ans+=B[-1]-B[j]
      try:
        if s[k]=="B":
          ans-=1
      except:
        pass
print(ans)

Solution (Python):
N=int(input())
S=input()

r_cnt=S.count('R')
g_cnt=S.count('G')
b_cnt=S.count('B')

ans=r_cnt*g_cnt*b_cnt

for i in range(N):
  for d in range(N):
    j=i+d
    k=j+d
    if k>=N:break

    if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
      ans-=1
      
print(ans)

Solution (Python):
N=int(input())
S=input()
from collections import Counter
C=Counter(S)
ans=C['R']*C['G']*C['B']
for i in range(N):
    for j in range(i+1,N):
        if j+(j-i) >= N: break
        if S[i] != S[j] != S[j+(j-i)] != S[i]: ans-=1
print(ans)

Solution (Python):
N = int(input())
s = list(input())
r = 0;g = 0;b= 0
for i in s:
    if i=="R":
        r += 1
    elif i=="G":
        g += 1
    else:
        b += 1
all = r*g*b
sub = 0
for i in range(N-1):
    for j in range(i+1,N):
        if s[i]==s[j]:
            continue
        k = 2*j - i
        if k >= N:
            continue
        else:
            if (s[k]!=s[i] and s[k]!=s[j]):
                sub += 1
print(all-sub)

Solution (Python):
N = int(input())
S = input()

cnt = {'R' : 0, 'G' : 0, 'B' : 0}
for Si in S:
    cnt[Si] += 1

ans = 1    
for key, val in cnt.items():
    ans *= val

    
for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k >= N: continue
        
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = input()
a = S.count("R") * S.count("G") * S.count("B")
c = 0
for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            c += 1
print(a - c)

Solution (Python):
n=int(input())
s=input()
r=g=b=0
for ss in s:
  if ss=="R":r+=1
  elif ss=="G":g+=1
  elif ss=="B":b+=1
ans=r*g*b
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j+(j-i)
    if k>=n:break
    if not(s[i]==s[j] or s[j]==s[k] or s[i]==s[k]):ans-=1
print(ans)

Solution (Python):
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+i-1)//2+1):
        if set([S[i], S[j], S[2*j-i]]) == {"R", "G", "B"}:
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
rgb = r*g*b
minus = 0
for i in range(n):
    dis = 1
    while i+dis*2 < n:
        if s[i] != s[i+dis] and s[i] != s[i+dis*2] and s[i+dis] != s[i+dis*2]:
            minus += 1
        dis += 1
print(rgb-minus)

Solution (Python):
n = int(input())
s = list(input())

r = 0
g = 0
b = 0
for c in s:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1

ans = r * g * b

for i in range(n - 2):
    for j in range(i, n - 1):
        if s[i] == s[j]:
            continue
        k = 2 * j - i
        if k >= n:
            continue
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = list(str(input()))

if n <= 2:
    print(0)

C = [[0]*(n+1) for i in range(3)]
for i in range(n):
    for j in range(3):
        C[j][i+1] = C[j][i]
    if s[i] == 'R':
        C[0][i+1] += 1
    elif s[i] == 'G':
        C[1][i+1] += 1
    else:
        C[2][i+1] += 1


D = [[0]*(n+1) for i in range(3)]
s.reverse()
for i in range(n):
    for j in range(3):
        D[j][i+1] = D[j][i]
    if s[i] == 'R':
        D[0][i+1] += 1
    elif s[i] == 'G':
        D[1][i+1] += 1
    else:
        D[2][i+1] += 1

D = [list(reversed(d)) for d in D]


s.reverse()
ans = 0
for i in range(1, n-1):
    
    if s[i] == 'R':
        ans += C[1][i]*D[2][i+1] + C[2][i]*D[1][i+1]
    elif s[i] == 'G':
        ans += C[0][i]*D[2][i+1] + C[2][i]*D[0][i+1]
    else:
        ans += C[0][i]*D[1][i+1] + C[1][i]*D[0][i+1]
    for j in range(1, i+1):
        if i+j >= n:
            continue
        if s[i-j] != s[i+j] and s[i-j] != s[i] and s[i+j] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
R = S.count("R")
G = S.count("G")
B = S.count("B")

def f(S):
    N = len(S)
    ret = 0
    for i in range(N):
        for j in range(i+1, N):
            k = j + j - i
            if k >= N: break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ret += 1
    return ret

ans = R * G * B - f(S)
print(ans)

Solution (Python):
N = int(input())
S = input()
R = S.count('R')
G = S.count('G')
B = N-R-G
ans = R*G*B
for d in range(1,N//2+1):
    for i in range(N):
        if i + d*2 >= N: break
        a,b,c = S[i],S[i+d],S[i+d+d]
        if a!=b and b!=c and c!=a:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
r = g = b = 0
for s in S:
    if s == 'R':
        r += 1
    if s == 'G':
        g += 1
    if s == 'B':
        b += 1

ans = r * g * b
for i in range(N - 1):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k >= N:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
n=int(input())
s=input()
tmp = s.count("R") * s.count("G") * s.count("B")

for i in range(n):
    for j in range(i+1, n):
        if 2*j-i < n:
            if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                tmp-=1
print(tmp)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
l = Counter(S)
diff = 0
for i in range(N-2):
  for j in range(1,(N-i-1)//2 + 1):
    
    if set(S[i:i+3*j:j]) == {'R','G','B'}:
      diff += 1
print(l['R'] * l['G'] * l['B'] - diff)

Solution (Python):
N=int(input())
S=input()
from collections import Counter
C=Counter(S)
ans=C['R']*C['G']*C['B']
for i in range(N):
    for j in range(i+1,N):
        if j+(j-i) >= N: break
        if S[i] != S[j] and S[j] != S[j+(j-i)] and S[i] != S[j+(j-i)]:
            ans-=1
print(ans)

Solution (Python):
n = int(input())
s = list(input())
r = s.count("R")
g = s.count("G")
b = s.count("B")
c = 0
for i in range(n):
    j = 1
    while i+j+j+1 <= n:
        if  len(set([s[i],s[i+j],s[i+j+j]]))==3:
            c += 1
        j += 1
print(r*g*b - c)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip("\n")
    toInt = {"R": 0, "G":1, "B": 2}
    count = [[0, 0, 0] for _ in range(N)]
    count[N-1][toInt[S[N-1]]] += 1
    for i in reversed(range(N-1)):
        for j in range(3): count[i][j] = count[i+1][j]
        count[i][toInt[S[i]]] += 1

    ans = 0
    for i in range(N-2):
        iInt = toInt[S[i]]
        for j in range(i+1, N-1):
            right = {0, 1, 2} - {iInt, toInt[S[j]]}
            if len(right) == 1:
                for key in right:
                    ans += count[j + 1][key]
                    if 2 * j - i < N and toInt[S[2*j - i]] == key: ans -= 1
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
from collections import Counter
def solve():
    n = int(input())
    s = input()
    c = Counter(s)
    ans = c['R']*c['G']*c['B']
    for i in range(n):
        for j in range(i+1,n):
            k = 2*j-i
            if k>=n:
                continue
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans-=1
    print(ans)
if __name__=='__main__':
    solve()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(g,b):
        left = bisect.bisect_left(g,j)
        right = bisect.bisect_left(b,j)
        res = left*(len(b)-right)
        for i in range(left):
            k = bisect.bisect_left(b,j2-g[i])
            if k < len(b) and b[k]+g[i] == j2:
                res -= 1
        return res
    n = I()
    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)
    ans = 0
    for j in range(1,n-1):
        sj = s[j]
        j2 = 2*j
        if sj == "R":
            ans += f(g,b)+f(b,g)
        elif sj == "G":
            ans += f(r,b)+f(b,r)
        else:
            ans += f(r,g)+f(g,r)
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
n=int(input())
s=input()
r=g=b=0
for i in s:
  if i=="R":r+=1
  elif i=="G":g+=1
  else:b+=1
ans=r*g*b
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j+(j-i)
    if k>=n:break
    if not(s[i]==s[j] or s[j]==s[k] or s[i]==s[k]):ans-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count("R") * s.count("G") * s.count("B")
cnt = 0
for i in range(n):
    i_ = s[i]
    for j in range(i+1, n):
        if s[j] == i_: continue
        k = 2 * j - i
        j_ = s[j]
        if k >= n or s[k] == i_ or s[k] == j_: continue
        cnt += 1
print(ans - cnt)

Solution (Python):
n = int(input())
s = []
r, g, b = [0] * 3
for c in input():
    if c == 'R':
        s.append(0)
        r += 1
    elif c == 'G':
        s.append(1)
        g += 1
    else:
        s.append(2)
        b += 1

c = r * g * b
for i in range(n):
    for j in range(i + 1, n):
        k = j + (j - i)
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            c -= 1

print(c)

Solution (Python):


n = int(input())
s = list(str(input()))

num_list = []
r, g, b = 0, 0, 0
for i in range(n-1, -1, -1):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    elif s[i] == "B":
        b += 1
    num_list.append([r, g, b])

num_list.reverse()

ans = 0
for i in range(n-2):
    if s[i] == "R":
        ans += num_list[i+1][1] * num_list[i+1][2]  
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            
            if {s[b], s[c]} == {"G", "B"}:
                ans -= 1
    if s[i] == "G":
        ans += num_list[i+1][0] * num_list[i+1][2]  
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            
            if {s[b], s[c]} == {"R", "B"}:
                ans -= 1
    if s[i] == "B":
        ans += num_list[i+1][0] * num_list[i+1][1]  
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            
            if {s[b], s[c]} == {"R", "G"}:
                ans -= 1

print(ans)

Solution (Python):
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b

for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>=n:
            break
        if (s[i]!=s[j]) & (s[j]!=s[k]) & (s[k]!=s[i]):
            ans-=1

print(ans)

Solution (Python):
n=int(input())
s=input()

w=s.count("R")*s.count("G")*s.count("B")
cnt=0

for i in range(n):
    for j in range(i+1,n):
        k=j+j-i
        if k>=n:
            break        
        if s[i]!=s[j] and s[k]!=s[j] and s[i]!=s[k]:
            cnt+=1
            

answer=w-cnt
print(answer)

Solution (Python):
from itertools import combinations

n = int(input())
s = input()
rs = s.count("R")
gs = s.count("G")
bs = s.count("B")
ans = rs * gs * bs

for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
 
r = S.count('R')
g = S.count('G')
b = S.count('B')
max = r * g * b 
 
count = 0
for i in range(N-2):
	for j in range(i+1, N-1):
		k = 2 * j - i
		if k > N - 1:
			break
	
		if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
			count += 1
		
		
 
print(max - count)

Solution (Python):
N = int(input())
S = input()

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)

Solution (Python):
def main():
    n, s = int(input()), input()
    r, g, b = 0, 0, 0
    for i in s:
        if i == 'R':
            r += 1
        if i == 'G':
            g += 1
        if i == 'B':
            b += 1

    ans = r * g * b
    s = tuple(s)
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, s = int(input()), input()
    r, g, b = s.count('R'), s.count('G'), s.count('B')
    ans = r * g * b
    s = tuple(s)
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, s = int(input()), input()
    r, g, b = s.count('R'), s.count('G'), s.count('B')
    ans = r * g * b
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
r = g = b = 0
for i in range(n):
  if s[i] == 'R':
    r += 1
  elif s[i] == 'G':
    g += 1
  else:
    b += 1
res = r * g  * b
for i in range(n):
  for j in range(i+1, n):
    k = 2 * j - i
    if 0 <= k < n and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
      res -= 1
print(res)