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=s.count('R')
g=s.count('G')
b=s.count('B')
ans=r*g*b
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if s[i]!=s[j]:
            k=j+j-i
            if k>=len(s):
                break
            else:
                if s[i]!=s[k] and s[j]!=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 x in range(N):
    for a in range(N):
        y = x+a
        z = y+a
        if z >= N:
            continue
        if S[x] == S[y]:
            continue
        if S[y] == S[z]:
            continue
        if S[x] == S[z]:
            continue
        ans = ans-1
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
s = input()
cnt = Counter(s)
ans = cnt["R"]*cnt["G"]*cnt["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[i] != s[k] and s[j] != s[k]: ans -= 1
print(ans)

Solution (Python):
import sys
from collections import defaultdict
N = int(input())
if N <= 3:
    print(0)
    sys.exit()
s = input()
ls = [defaultdict(int) for i in range(N)]
a = ['R', 'G', 'B']
for i, x in enumerate(s):
    for y in a:
        if x == y:
            ls[i][y] = ls[max(0, i-1)][y] + 1
        else:
            ls[i][y] = ls[max(0, i-1)][y]
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if s[i] != s[j]:
            for k in a:
                if k != s[i] and k != s[j]:
                    ans += ls[-1][k] - ls[j][k]
            if 2*j-i <N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
                ans -= 1
print(ans)

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

A = [0]*3

for s in S:
    if s == "R":
        A[0] += 1
    elif s =="G":
        A[1] += 1
    elif s == "B":
        A[2] += 1
        
ans = 1

for a in A:
    ans *= a

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

Solution (Python):
from collections import Counter

def SUMCHK():
    N = int(input())
    S = input()

    C=Counter(S)
    r = C['R']
    g = C['G']
    b = C['B']


    cnt = r * g * b

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

    print(cnt)

SUMCHK()

Solution (Python):
import itertools
from collections import Counter

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

C=Counter(S)
r = C['R']
g = C['G']
b = C['B']


cnt = r * g * b

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

print(cnt)

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

R = []
G = []
list = [0]*n
number_B = 0
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    list[i] = 1
    number_B += 1
    

ans = 0
for i in R:
  for j in G:
    minus = 0
    if i < j:
      if 2*i-j >= 0 and list[2*i-j] == 1:
        minus += 1   
      if (i+j)%2 == 0 and list[int((i+j)/2)] == 1:
        minus += 1      
      if 2*j-i<n and list[2*j-i] == 1:
        minus += 1
    else:
      if 2*j-i >= 0 and list[2*j-i] == 1:
        minus += 1   
      if (i+j)%2 == 0 and list[int((i+j)/2)] == 1:
        minus += 1      
      if 2*i-j<n and list[2*i-j] == 1:
        minus += 1
    ans += (number_B-minus)
    
print(ans)

Solution (Python):
from collections import Counter
N=int(input())
S=input()
C=Counter(S)
nr=C["R"]
ng=C["G"]
nb=C["B"]
ans=nr*ng*nb
for i in range(N-2):
    for j in range(i+1,N-1):
        k=j+j-i
        if k<N:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1

print(ans)

Solution (Python):
from collections import Counter

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

dic = Counter(S)
cnt = 0

for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] == S[j]:
      continue
    elif 2*j - i < N:
      if S[i] != S[2*j-i] and S[j] != S[2*j-i]:
        cnt += 1
        
print(dic["R"]*dic["B"]*dic["G"]-cnt)

Solution (Python):
N = int(input())
S = list(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

ans = sum(R)*sum(G)*sum(B)

for i in range(N):
    for j in range(i+1,min((N+i+3)//2,N)):
        k = 2*j-i
        if  k<N:
            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 = list(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

ans = sum(R)*sum(G)*sum(B)

for i in range(N):
    for j in range(i+1,N):
        k = 2*(j+1)-(i+1)-1
        if  k<N:
            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=[[0,0,0] for _ in range(n)]
c=0
l={"GB":0,"BG":0,"RB":1,"BR":1,"RG":2,"GR":2}
for i in range(n):
  if s[i] =="R":
    r[i][0]=r[i-1][0]+1
    r[i][1]=r[i-1][1]
    r[i][2]=r[i-1][2]
  elif s[i]=="G":
    r[i][1]=r[i-1][1]+1
    r[i][0]=r[i-1][0]
    r[i][2]=r[i-1][2]
  elif s[i]=="B":
    r[i][2]=r[i-1][2]+1
    r[i][0]=r[i-1][0]
    r[i][1]=r[i-1][1]

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

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

from collections import defaultdict

memo = [defaultdict(int) for j in range(N+1)]

ans = 0



for i,s in enumerate(S):
    memo[i+1]["R"] = memo[i]["R"]
    memo[i+1]["G"] = memo[i]["G"]
    memo[i+1]["B"] = memo[i]["B"]
    memo[i+1][s]+=1
    others = set(["R","G","B"])
    others.remove(s)
    others = list(others)
    ans += memo[i+1][others[0]] * memo[i+1][others[1]]

rgb = set(["R","G","B"])


if N > 2:
    if N %2 == 1:
        length = N // 2
    else:
        length = N // 2 - 1

    
    
    for skip in range(length):
        for i in range(len(S)):
            
            if i+2+skip*2 > len(S)-1:

                break
            a,b,c = S[i],S[i+1+skip],S[i+2+skip*2]
            
            if set([a,b,c]) == rgb:
                ans -= 1
        

print(ans)

Solution (Python):
def readinput():
    n=int(input())
    s=input()
    s=' '+s
    return n,s

def main(n,s):
    count=0
    all=set(['R','G','B'])
    for i in range(1, n-1):
        l1=s[i]
        for j in range(i+1, n):
            l2=s[j]
            if l1==l2:
                continue
            l3=(all - set([l1,l2])).pop()
            ij=j-i
            sub1=s[j+1:j+ij]
            sub2=s[j+ij+1:]
            count+=sub1.count(l3)
            count+=sub2.count(l3)
    return count

def main2(n,s):
    count=0
    nr=s.count('R')
    ng=s.count('G')
    nb=s.count('B')
    all=set(['R','G','B'])
    for i in range(1, n-1):
        l1=s[i]
        for j in range(i+1, n):
            l2=s[j]
            if l1==l2:
                continue
            ij=j-i
            jk=ij
            k=j+jk
            if(k>n):
                break
            l3=s[k]
            if l3==l1 or l3==l2:
                continue
            count+=1
    return nr*ng*nb-count



if __name__=='__main__':
    n,s=readinput()
    ans=main2(n,s)
    print(ans)

Solution (Python):
N = int(input())
S = input()
rgb = [0]*3

for i in range(N):
    if S[i] == "R":
        rgb[0] += 1
    elif S[i] == "G":
        rgb[1] += 1
    else:
        rgb[2] += 1
ans = 0
for i in range(N):
    for h in range(N):
        j = i+h
        k = i+2*h
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans += 1

print(rgb[0]*rgb[1]*rgb[2]-ans)

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

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

for i in range(n):
    if lis[i] == "R":
        lis[i] = 1
    elif lis[i] == "G":
        lis[i] = 2
    else:
        lis[i] = 4

for i in range(n):
    for j in range(i+1, n):
        k = 2*j -i
        if k >= n:
            continue
        if lis[i] + lis[j] + lis[k] == 7:
            ans -= 1
        
print(ans)

Solution (Python):
from collections import Counter
N=int(input())
S=input()
C=Counter(S)
nr=C["R"]
ng=C["G"]
nb=C["B"]
ans=nr*ng*nb
for i in range(N-2):
    for j in range(i+1,N-1):
        k=j+j-i
        if k<N:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1

print(ans)

Solution (Python):
from collections import Counter
N=int(input())
S=input()
C=Counter(S)
nr=C["R"]
ng=C["G"]
nb=C["B"]
ans=nr*ng*nb
for i in range(N-2):
    for j in range(i+1,N-1):
        k=j+j-i
        if k<N:
            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=[]
G={}
B=[]
for i in range(n):
  if s[i]=='R':
    R.append(i+1)
  elif s[i]=='G':
    G[i+1]=G.get(i+1,0)+1
  else:
    B.append(i+1)
res=len(R)*len(G)*len(B)
for i in R:
  for j in B:
    if ((i+j)%2==0 and G.get((i+j)//2,0)==1):
      res-=1
    if G.get(2*max(i,j)-min(i,j),0)==1:
      res-=1
    if G.get(2*min(i,j)-max(i,j),0)==1:
      res-=1
print(res)

Solution (Python):
n=int(input())
s=input()
R=[]
G={}
B=[]
for i in range(n):
  if s[i]=='R':
    R.append(i+1)
  elif s[i]=='G':
    G[i+1]=G.get(i+1,0)+1
  else:
    B.append(i+1)
res=len(R)*len(G)*len(B)
for i in R:
  for j in B:
    if ((i+j)%2==0 and G.get((i+j)//2,0)==1):
      res-=1
    if G.get(2*max(i,j)-min(i,j),0)==1:
      res-=1
    if G.get(2*min(i,j)-max(i,j),0)==1:
      res-=1
print(res)

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(1, n):
        j = i + d
        k = j + d
        if k >= n:
            break
        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 = str(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(1, N):
        j = i + d
        k = j + d
        if k >= N:break
        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 = input()

num_R = S.count('R')
num_G = S.count('G')
num_B = S.count('B')
S = '_' + S

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

print(num_R*num_G*num_B - count)

Solution (Python):
n = int(input())
s = input()
d ={'R':set(), 'G':set(),'B':set()}

for i,x in enumerate(s):
    d[x].add(i+1)

ans = 1
for k in d:
    ans*=len(d[k])
    
for i in d['R']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['B']:ans-=1
                
for i in d['R']:
    for j in d['B']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['G']:ans-=1
                
for i in d['B']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['R']:ans-=1

print(ans)

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

al=s.count("R")*s.count("G")*s.count("B")

for i in range(n-2):
    for k in range(i+1,n-1):
        l=k+(k-i)
        if l>=n:
            continue
        if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
            al-=1

print(al)

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, n-1):
        if s[i] != s[j]:
            if 2*j-i <= n-1 and s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = []
g = []
b = [0]*40000
for i,j in enumerate(s):
  if j == 'R':
    r.append(i)
  if j == 'G':
    g.append(i)
  if j == 'B':
    b[i] += 1
cnt = len(r)*len(g)*sum(b)
for i in r:
  for j in g:
    if b[2*j-i] == 1 :
      cnt -= 1
    if b[2*i-j] == 1 :
      cnt -= 1
    if (i+j)%2 == 0 and b[(i+j)//2] == 1:
      cnt -= 1
print(cnt)

Solution (Python):
n = int(input())
s = input()
c_r = 0
c_g = 0
c_b = 0
for i in range(n):
    if s[i] == "R":
        c_r += 1
    elif s[i] == "G":
        c_g += 1
    elif s[i] == "B":
        c_b += 1
c = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == "R" and s[j] == "G":
            if j < 2*j-i < n:
                if s[2*j-i] == "B":
                    c += 1
        if s[i] == "R" and s[j] == "B":
            if j < 2*j-i < n:
                if s[2*j-i] == "G":
                    c += 1
        if s[i] == "G" and s[j] == "R":
            if j < 2*j-i < n:
                if s[2*j-i] == "B":
                    c += 1
        if s[i] == "G" and s[j] == "B":
            if j < 2*j-i < n:
                if s[2*j-i] == "R":
                    c += 1
        if s[i] == "B" and s[j] == "R":
            if j < 2*j-i < n:
                if s[2*j-i] == "G":
                    c += 1
        if s[i] == "B" and s[j] == "G":
            if j < 2*j-i < n:
                if s[2*j-i] == "R":
                    c += 1
        else:
            pass
print(c_r*c_g*c_b-c)

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

total = s.count("R") * s.count("G") * s.count("B")



lR = [0]*n
lG = [0]*n
lB = [0]*n

for i in range(len(s)):
    if s[i] == "R":
        lR[i] += 1
    elif s[i] == "G":
        lG[i] += 1
    else:
        lB[i] += 1





for i in range(n):
    j = 1
    if lR[i]:
        while i-j >= 0 and i+j < n:
            if lG[i+j]!=0 and lG[i+j] == lB[i-j]:
                total -= 1
            elif lG[i-j]!=0 and lG[i-j] == lB[i+j]:
                total -= 1
            j += 1

    elif lG[i]:
        while i-j >= 0 and i+j < n:
            if lR[i+j]!=0 and lR[i+j] == lB[i-j]:
                total -= 1
            elif lR[i-j]!=0 and lR[i-j] == lB[i+j]:
                total -= 1
            j += 1

    elif lB[i]:
        while i-j >= 0 and i+j < n:
            if lG[i+j]!=0 and lG[i+j] == lR[i-j]:
                total -= 1
            elif lG[i-j]!=0 and lG[i-j] == lR[i+j]:
                total -= 1
            j += 1

print(total)

Solution (Python):
def solution1():
    
    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
    
    total = r*g*b
    

    is_rgb = ord('R') + ord('G') + ord('B')

    for i in range(n):
        for j in range(i+1, n):
            
            k = 2*j - i
            if k < n:
                if ord(s[i]) + ord(s[j]) + ord(s[2*j-i]) == is_rgb:
                    total -= 1
    print(total)

solution1()

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

rinds, ginds, binds = [], [], set()

for i in range(n):
    if s[i] == 'R': rinds.append(i)
    elif s[i] == 'G': ginds.append(i)
    else: binds.add(i)

rlen, glen, blen = len(rinds), len(ginds), len(binds)


ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        
        if j_ + dist in binds:
            ans -= 1
        if dist%2==0 and i_ + dist//2 in binds:
            ans -= 1
        if i_ - dist in binds:
            ans -= 1

        
        
        
        
        
        
        
        

print(ans)

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

rinds, ginds, binds = [], [], []

for i in range(n):
    if s[i] == 'R': rinds.append(i)
    elif s[i] == 'G': ginds.append(i)
    else: binds.append(i)


rlen, glen, blen = len(rinds), len(ginds), len(binds)

def binsearch_same(binds, blen, dist, i):
    lo, hi = 0, blen-1
    while lo<=hi:
        mid = lo + (hi-lo)//2
        d = binds[mid] - i
        if dist == d:
            return True
        elif d < dist:
            lo = mid + 1
        else:
            hi = mid - 1
    return False

ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        

        if binsearch_same(binds, blen, dist, j_):
            ans -= 1
        
        if dist%2==0 and binsearch_same(binds, blen, dist//2, i_):
            ans -= 1
        
        if binsearch_same(binds, blen, -dist, i_):
            ans -= 1

print(ans)

Solution (Python):

N= int(input())
S =input().strip()


R =set([i for i, x in enumerate(S) if x == 'R'])
G =set([i for i, x in enumerate(S) if x == 'G'])
B =set([i for i, x in enumerate(S) if x == 'B'])


count =0
rgb =[]
for i in R:
    for j in G:
        k1 =j +(j -i)
        if k1 in B: 
            count +=1

for i in B:
    for j in R:
        k1 =j +(j -i)
        if k1 in G: 
            count +=1

for i in G:
    for j in B:
        k1 =j +(j -i)
        if k1 in R: 
            count +=1

all =len(R) *len(G) *len(B)
print(all -count)

Solution (Python):
n = int(input())
s = list(input())
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

ans = r * g * b

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


print(ans)

Solution (Python):
import sys

readline = sys.stdin.buffer.readline
import math

sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    S = readline().decode('utf-8')

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

    for stride in range(1, math.ceil(N / 2) + 1):
        for start in range(N):
            if start + stride * 2 >= N:
                break
            else:
                if S[start] != S[start + stride] and S[start + stride] != S[start + stride * 2] and S[start + stride * 2] != S[start]:
                    total -= 1

    print(total)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit
def main():
    
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit("i8(i8,i8,i8[:])")
def f(n,ans,s):
    for i in range(n):
        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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit
def main():
    
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit("i8(i8,i8,i8[:])",cache=True)
def f(n,ans,s):
    for i in range(n):
        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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit
def main():
    
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit(cache=True)
def f(n,ans,s):
    for i in range(n):
        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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit
def main():
    
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit
def f(n,ans,s):
    for i in range(n):
        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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    mx = S.count('R') * S.count('G') * S.count('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[i] != S[k] and S[j] != S[k]:
                mx -= 1
    print(mx)


if __name__ == '__main__':
    main()

Solution (Python):
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)

ans = len(R)*len(G)*len(B)
for i in range(n-2):
    for j in range(i+1,n-1):
        k  = 2*j-i
        if k<n:
            if (s[i]!=s[j]) and (s[i]!=s[k]) and (s[j]!=s[k]):
                ans-=1
print(ans)

Solution (Python):

def main():
    N = int(input())
    S = [s for s in 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 N - 1 < k:
                continue
            if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = [s for s in 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 N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = [s for s in 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 N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = list(input())
ans = 0
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 = len(R) * len(G) * len(B)


for i in range(0,n-2):
    for j in range(i+1,n-1):
        k = j + ( j -i )
        if k < n:
            if s[i] != s[j] and s[k] != s[j] and s[i] != s[k]:
                ans -= 1


print(ans)

Solution (Python):
n = int(input())
s = str(input())
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
p = r*g*b
for i in range(n-2):
    for j in range(int(n/2)+1):
        if i + 2*j >n-1:
            break
        if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+2*j] != s[i+j]:
            p -= 1
print(p)

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

from collections import Counter

a = [None] * n
for i in range(n):
    if s[i] == "R":
        a[i] = 0
    if s[i] == "G":
        a[i] = 1
    if s[i] == "B":
        a[i] = 2

c = [0] * 3
for v in a:
    c[v] += 1

ans = 1
for v in c:
    ans *= v

cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = 2 * j - i
        if j < k and k < n:
            if s[i] == s[j]:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue
            cnt += 1

ans -= cnt

print(ans)

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

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n = int(input())
    s = input()
    rgb = {"R":0,"G":0,"B":0}
    for i in s:
        rgb[i] += 1
    ans = 1
    for i in rgb.keys():
        ans *= rgb[i]
    for i in range(n-2):
        for j in range(i+1,n-1):
            k = j + (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)
    return

if __name__=='__main__':
    main()

Solution (Python):
l =int(input())
s =input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
A = r * g * b
B = 0
gapmax = (l - 3) // 2
for _ in range(gapmax+1):
  for i in range(l-2-2*_):
    c1, c2, c3 = s[i], s[i+1+_],s[i+2+2*_]
    if not (c1 == c2) and not (c1 == c3) and not (c2 ==c3):
      B += 1
      
print(A-B)

Solution (Python):
import collections
import sys
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    s = readline().rstrip()
    cnt = s.count("R") * s.count("G") * s.count("B")

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


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = list(input())
ans = s.count('R')*s.count('G')*s.count('B')

for i in range(n):
  if s[i] == 'R':
    s[i] = 1
  elif s[i] == 'G':
    s[i] = 2
  elif s[i] == 'B':
    s[i] = 4
  
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]+s[k]==7:
      ans-=1

print(ans)

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

rs = s.count('R')
gs = s.count('G')
bs = s.count('B')

t = rs * gs * bs

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

print(t)

Solution (Python):
n = int(input())
s = str(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] and s[j] != s[k] and s[k] != s[i]:
            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):
    for j in range(i+1,n):
        k = 2*j-i
        if k>=n:
            continue
        if S[i]==S[j] or S[j]==S[k] or S[i]==S[k]:
            continue
        else:
            ans -=1
print(ans)

Solution (Python):

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

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

for start in range(0,N-2):
    for interval in range(1,N):
        try:
            if S[start] != S[start+interval] and S[start] != S[start+2*interval] and S[start+interval] != S[start+2*interval]:
                ans -= 1
        except IndexError:
            break
print(ans)

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    s=STR()
    r=s.count('R')
    g=s.count('G')
    b=s.count('B')
    ans=r*g*b
    
    for i in range(n+1):
        for j in range(1,n//2+2):
            k=i+2*j
            if k<=n-1:
                
                if (s[i]!=s[i+j] and s[k]!=s[i+j] and s[i]!=s[k]):
                    ans-=1
                    
    print(ans)

                
if __name__ == '__main__':
    do()

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

for i in range(n):
    for j in range(i + 1, n):
        if j * 2 - i >= n:
            continue
        if s[i] == s[j] or s[j] == s[2 * j - i] or s[i] == s[2 * j - i]:
            continue
        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):
    for j in range(i + 1, n):
        if j * 2 - i >= n:
            continue
        if s[i] == s[j] or s[j] == s[2 * j - i] or s[i] == s[2 * j - i]:
            continue
        ans -= 1

print(ans)

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

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

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

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

Solution (Python):
N=int(input())
S=input()
r=S.count('R')
b=S.count('B')
g=S.count('G')
ans=r*b*g
for i in range(N):
    for s in range(N):
        j=i+s
        k=j+s
        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):
import collections as cl
N = int(input())
S = input()
a,b = '',''
d = cl.Counter(S)
rgb = d['R']*d['B']*d['G']
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,(N+i-1)//2+1):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if S[k]!=b and S[k]!=a:
        expt += 1
print(rgb-expt)