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

Solution (Python):
N = int(input())
S = input()
R = set()
G = set()
B = set()
for i in range(N):
    if S[i] == 'R':
        R.add(i)
    elif S[i] == 'G':
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)
if ans == 0:
    print(0)
    exit()
for i in R:
    for j in G:
        d = abs(j-i)
        a = min(i, j)-d
        b = (i+j)/2
        c = max(i, j)+d
        s = {a, b, c}
        for k in s:
            if k in B:
                ans -= 1
print(ans)

Solution (Python):
import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
N=INT()
S=input()
if N<3:
  print(0)
  exit()
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(i+1,N-1):
    k=j*2-i
    if k>=N:
      continue
    if S[j]!=S[i] and S[k]!=S[i] and S[j]!=S[k]:
      ans-=1
print(ans)

Solution (Python):
import collections
from functools import reduce

n = int(input())
S = input()

c = collections.Counter(list(S))
ans = c['R'] * c['G'] * c['B']

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

print(ans)

Solution (Python):
from collections import Counter as C
n=int(input())
S=list(input())
a=1
L=C(S).most_common(3)
for l in L:
  a*=l[1]
for i in range(1,n):
  for j in range(n-2*i):
    if len(set((S[j],S[j+i],S[j+2*i])))==3:
      a-=1
print(a if len(L)>2 else 0)

Solution (Python):
N = int(input())
S = [("R", "G", "B").index(c) for c in input()]

sums = [[0] * (N + 1) for _ in range(3)]
for i, c in enumerate(S):
    for x in sums:
        x[i+1] = x[i]
    sums[c][i+1] += 1

ans = 0
for i in range(N - 2):
    si = S[i]
    for j in range(i + 1, N - 1):
        sj = S[j]
        if si == sj:
            continue
        sk = (set(range(3)) - {si, sj}).pop()
        count = sums[sk][N] - sums[sk][j+1]
        if j - i <= N - (j + 1):
            count -= (sums[sk][j+1+j-i] - sums[sk][j+1+j-i-1])
        ans += count
print(ans)

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

Solution (Python):
n=int(input())
s=input()
R=[]
G=[]
B=[]
def f():
  for i in range(n):
    if s[i]=="R":
      R.append(i)
    elif s[i]=="G":
      G.append(i)
    else:
      B.append(i)
  r=len(R)
  g=len(G)
  b=len(B)
  if r==0 or g==0 or b==0:
    return 0
  else:
    ans=r*g*b
    m=0
    for j in range(1,n-1):
      for k in range(1,min(j,n-1-j)+1):
        lt=s[j-k]
        md=s[j]
        rt=s[j+k]
        if lt!=md and md!=rt and rt!=lt:
          m+=1
    return ans-m
print(f())

Solution (Python):
def main():
  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:
        break
      if (S[i]!=S[j]) and (S[j]!=S[k]) and(S[i]!=S[k]):
        ans-=1      
  print(ans)

if __name__=='__main__':
  main()

Solution (Python):
n = int(input())
s = input()
ans = 0
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(0,n-2):
    for j in range(1,n-1):
        k = -i + 2*j
        if j < k <= n-1:
            if s[i] == s[j]:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue
        else:
            continue
        ans -= 1 
print(ans)

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

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

import bisect
def binary_search(a, x):
    
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return True
    else:
        return False

ans=0
for i in range(len(r)):
    for j in range(len(g)):
        p=0
        if r[i]<g[j]:
            if binary_search(b,g[j]-r[i]+g[j]):
                p+=1
            if binary_search(b,r[i]+(g[j]-r[i])/2):
                p+=1
            if binary_search(b,r[i]-(g[j]-r[i])):
                p+=1
        else:
            if binary_search(b,r[i]-g[j]+r[i]):
                p+=1
            if binary_search(b,g[j]+(r[i]-g[j])/2):
                p+=1
            if binary_search(b,g[j]-(r[i]-g[j])):
                p+=1
        ans+=len(b)-p

print(ans)

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

Solution (Python):
import itertools
import bisect
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))


INF=10**20
def main():
    N=ii()
    S=list(input())

    R,G,B=[],[],[]
    Rset,Gset,Bset = set(),set(),set()
    
    for i in range(N):
        s = S[i]
        if s == "R":
            R.append(i)
            Rset.add(i)

        elif s == "G":
            G.append(i)
            Gset.add(i)
        
        else:
            B.append(i)
            Bset.add(i)

    ans = 0
    dic = {"R":(R,Rset),"G":(G,Gset),"B":(B,Bset)}
    for comb in itertools.permutations(["R","G","B"]):
        X,Y,Z = dic[comb[0]][0], dic[comb[1]][0], dic[comb[2]][0]
        Xset,Yset,Zset = dic[comb[0]][1], dic[comb[1]][1], dic[comb[2]][1]

        
        for x in X:
            for y in Y:
                if not x < y: continue
                ignore_z = 2*y - x
                has_ignore_target = ignore_z in Zset

                iz = bisect.bisect_left(Z,y+1)
                if not 0<=iz<len(Z): continue
                z_count = len(Z)-iz

                if has_ignore_target:
                    z_count -= 1
                
                assert z_count >= 0
                ans += z_count
                


    print(ans)





if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
r,g,b=[],[],[]
for i in range(n):
  if s[i]=='R':
    r+=[i+1]
  elif s[i]=='G':
    g+=[i+1]
  elif s[i]=='B':
    b+=[i+1]

ans=len(r)*len(g)*len(b)

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

print(ans)

Solution (Python):
import itertools
N=int(input())
S=input()
ans=S.count("R")*S.count("G")*S.count("B")
c=list(itertools.permutations(["R","B","G"],3))
for j in range(1,N):
    for i in range(N-j*2):
        if (S[i],S[i+j],S[i+j*2]) in c:
            ans-=1
print(ans)

Solution (Python):
N = int(input())
S = "A" + input()
r, g, b = 0, 0, 0

for s in S[1:]:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

pat = r*g*b

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

print(pat)

Solution (Python):
n=int(input())
s=input()
cnt=0
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(n):
        if s[i]!=s[j]:
            if j-i<=n//2:
                if i<j:
                    d=j-i
                    k=j+d
                    if k<n:
                        if s[k]!=s[j] and s[i]!=s[k]:
                            ans-=1
                        

      
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 j in range(i + 1, n):
        k = j + (j - i)
        if k >= n:
            continue
        elif len(set([s[i], s[j], s[k]])) == 3:
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter

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

c=Counter(S+'RGB')
ans = (c['R']-1)*(c['G']-1)*(c['B']-1)

max_d = (N-3)//2+1

for d in range(1,max_d+1):
  for i in range(N-2):
    j = i+d
    k = j+d
    if k > N-1:break
    if len(set([S[i],S[j],S[k]]))==3:
      ans -= 1
      
print(ans)

Solution (Python):

import collections


def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    cnt = collections.Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
from itertools import permutations

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

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

combi = list(permutations(["R","G","B"],3))

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

print(ans)

Solution (Python):
N=int(input())
S=str(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 = 2*j-i
        
        if k <= N-1:
            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()
total = S.count("R") * S.count("G") * S.count("B")
sub = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]:
            continue
        h = j + j - i
        if h > N - 1 or S[j] == S[h] or S[h] == S[i]:
            continue
        sub += 1
print(total - sub)

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

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

ans = r*g*b

for i in range(0,N):
  for j in range(i,N):
    k = 2*j - i
    if j < k <= N-1:
      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(input())

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

ans = r*g*b

for i in range(0,N):
  for j in range(i,N):
    k = 2*j - i
    if j < k <= N-1:
      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 = input()
    RGB = [0]*3
    for s in S:
        if s == "R":
            RGB[0] += 1
        elif s == "G":
            RGB[1] += 1
        else:
            RGB[2] += 1
    ans = RGB[0] * RGB[1] * RGB[2]
    for d in range(1, N):
        for i in range(N-d+1):
            if N <= i + 2*d:
                break
            if S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

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

Rr = 0
Gr = 0
Br = 0
Rl = 0
Gl = 0
Bl = 0

for i in range(1,N):
  if S[i] == "R":
    Rr += 1
  elif S[i] == "G":
    Gr += 1
  else:
    Br += 1

ans = 0
for i in range(N-1):
  if S[i] == "R":
    ans += Gl*Br
    ans += Bl*Gr
    Rl += 1
  elif S[i] == "G":
    ans += Rl*Br
    ans += Bl*Rr
    Gl += 1
  else:
    ans += Rl*Gr
    ans += Gl*Rr
    Bl += 1
    
  if S[i+1] == "R":
    Rr -= 1
  elif S[i+1] == "G":
    Gr -= 1
  else:
    Br -= 1
  
for i in range(1, N-1):
  for k in range(1, min(i, N-1-i)+1):
    if S[i] != S[i+k] and S[i] != S[i-k] and S[i+k] != S[i-k]:
      ans -= 1
  
print(ans)

Solution (Python):
N = int(input())
S = input()
d = {'R': 0, 'G': 0, 'B': 0}
for i in range(N):
    d[S[i]]+=1
tot = d['R']*d['G']*d['B']

counter = 0
for i in range(N):
    width = 1
    tmp = i+2
    while tmp < N:
        if S[i] != S[i+width] and S[i] != S[i+2*width] and S[i+width] != S[i+2*width]:
            counter += 1
        width += 1
        tmp = i + 2 * width
print(tot-counter)

Solution (Python):
def main():
    import bisect
    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
    ans = r*g*b
    for i in range(n-1):
        for j in range(i+1,n):
            k = 2*(j+1)-(i+1)-1
            if k>n-1:
                continue
            if s[i]!=s[j] and s[i] != s[k] and s[j]!=s[k]:
                ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


n = ini()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b

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

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()

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

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()

r = s.count('R')
g = s.count('G')
b = n - r - g
ans = comb(n, 3) - ( comb(r, 2) + comb(g, 2) + comb(b, 2) ) * (n-2) + ( comb(r, 3) + comb(g, 3) + comb(b, 3) ) * 2
for i in range(n):
    d = 1
    j = i + d
    k = j + d
    while k < n:
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
        d += 1
        j += 1
        k += 2
print(ans)

Solution (Python):
n = int(input())
S = input()
rs = [s=="R" for s in S]
gs = [s=="G" for s in S]
bs = [s=="B" for s in S]

ind_r = [i for i,s in enumerate(rs) if s == 1]
ind_g = [i for i,s in enumerate(gs) if s == 1]
ind_b = [i for i,s in enumerate(bs) if s == 1]

ans = 0
ng = sum(gs)
for i in ind_r:
    for j in ind_b:
        
        kM = max(i,j) + abs(i-j)
        km = min(i,j) - abs(i-j)
        ans += ng
        if (max(i,j) - min(i,j))%2==0:
            
            ans -= gs[(max(i,j)+min(i,j))//2]
        if kM < n:
            ans -= gs[kM]
        if 0 <= km:
            ans -= gs[km]
print(ans)

Solution (Python):
def solve():
    N = int(input())
    S = input()
    cnt = {}
    for c in S:
        cnt[c] = cnt.get(c, 0) + 1
    total_triplets = cnt.get('R', 0) * cnt.get('G', 0) * cnt.get('B', 0)
    for stride in range(1, N):
        for i in range(0, N-2*stride):
            j = i + stride
            k = j + stride
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                total_triplets -= 1
    print(total_triplets)

if __name__ == "__main__":
    solve()

Solution (Python):

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

    res = 0
    R, G, B = 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:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                res += 1
    print(R * G * B - res)


if __name__ == '__main__':
    main()

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

Solution (Python):

import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]









n = I()
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    for k in range(i+1,n):
        if (i+k)%2 == 0:
            j = (i+k)//2
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans+=1
ans_2 = c["R"]*c["G"]*c["B"]
print(ans_2-ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
S = S()
R_num = [n for n, v in enumerate(S) if v == 'R']
G_num = [n for n, v in enumerate(S) if v == 'G']
B_num = [n for n, v in enumerate(S) if v == 'B']


cnt = 0
for r in R_num:
    for g in G_num:
        x = r + g
        if x % 2 == 0:
            if x // 2 in B_num:
                cnt += 1
for g in G_num:
    for b in B_num:
        x = g + b
        if x % 2 == 0:
            if x // 2 in R_num:
                cnt += 1
for r in R_num:
    for b in B_num:
        x = r + b
        if x % 2 == 0:
            if x // 2 in G_num:
                cnt += 1

ans = len(R_num) * len(G_num) * len(B_num) - cnt
print(ans)

Solution (Python):
def main5():
    N = int(input())
    S = list(input())

    r = g = b = 0
    for c in S:
        if c == "R":
            r += 1
        if c == "G":
            g += 1
        if c == "B":
            b += 1

    tmp = 0
    for i in range(len(S)):
        for j in range(i + 1, len(S)):
            k = 2*j - i
            if  k < len(S) and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                tmp += 1
    
    ans = r*g*b - tmp

    print(ans)

if __name__ == "__main__":
    main5()

Solution (Python):
def main5():
    N = int(input())
    S = list(input())

    r = g = b = 0
    for c in S:
        if c == "R":
            r += 1
        if c == "G":
            g += 1
        if c == "B":
            b += 1

    tmp = 0
    for i in range(len(S)):
        for j in range(len(S)):
            if i < j:
                k = 2*j - i
                if  k < len(S) and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    tmp += 1
    
    ans = r*g*b - tmp

    print(ans)

if __name__ == "__main__":
    main5()

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

    R = set()
    G = set()
    B = set()

    for i in range(len(S)):
        if   S[i] == "R":
            R.add(i)
        elif S[i] == "G":
            G.add(i)
        elif S[i] == "B":
            B.add(i)

    ans = 0
    for r in R:
        for g in G:
            a, b = sorted([r, g])
            tmp = len(B)

            if ((b + a) % 2 == 0) and ((b + a) // 2) in B:
                tmp -= 1

            if (2*a - b) in B:
                tmp -= 1

            if (2*b - a) in B:
                tmp -= 1

            ans += tmp
                
    print(ans)

if __name__ == "__main__":
    main4()

Solution (Python):
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))


n = ri()
s = rr()
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:
            if s[k] != s[j] and s[j] != s[i] and s[k] != s[i]:
                ans -= 1
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[j] != s[k] and s[k] != s[i]:

            ans -= 1
print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()
    r = s.count('R') * s.count('G') * s.count('B')

    for i1 in range(n-2):
        for i2 in range(i1 + 1, n-1):
            k = i2 * 2 - i1
            if k < n:
                if s[i1] != s[i2] and s[i1] != s[k] and s[i2] != s[k]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n = int(input())
s = list(input())
c = Counter(s)
ans = c["R"]*c["G"]*c["B"]
for i in range(n-2):
  for j in range(1,n):
    if i+2*j >= n:
      break
    if sorted([s[i],s[i+j],s[i+2*j]]) == ["B","G","R"]:
      ans -= 1
print(ans)

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

count = S.count("R")*S.count("G")*S.count("B")

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

print(count)

Solution (Python):

from collections import Counter

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

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

print(ctr["R"] * ctr["G"] * ctr["B"] - res)

Solution (Python):

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

d = {"R": 0, "G": 1, "B": 2}
ctr = [[0] * 3 for _ in range(N + 1)]
for i in range(N):
    ctr[i + 1][d[S[i]]] += 1
    for j in range(3):
        ctr[i + 1][j] += ctr[i][j]


ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        if S[i] != S[j]:
            c = (set("RGB") - set([S[i], S[j]])).pop()
            cnt = ctr[-1][d[c]] - ctr[j + 1][d[c]]

            k = 2 * (j + 1) - (i + 1) - 1
            if j < k < N and c == S[k]:
                cnt -= 1

            ans += max(0, cnt)

print(ans)

Solution (Python):
n=int(input())
s=input()
ans=0
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
for i in range(1,n):
    for j in range(i+1,n):
        if (j-i)+j<=n:
            k=(j-i)+j-1
            if  s[i-1]!=s[j-1] and  s[j-1]!=s[k] and s[k]!=s[i-1]:
                ans-=1
ans+=r*g*b
print(ans)

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

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

t="RGBRGB"

c=0
cm=0
for i in range(3):
    for j in range(1,n-1):
        if s[j]==t[i] :
            low = s[:j].count(t[i+1])
            high = s[j:].count(t[i+2])
            c += low*high
            low = s[:j].count(t[i+2])
            high = s[j:].count(t[i+1])
            c += low*high
            for k in range(1,j+1):
                if j+k>n-1 :
                    break
                if s[j-k]!=t[i] and s[j+k]!=t[i] and s[j-k]!=s[j+k]:
                    cm+=1


print(c-cm)

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

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

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[i] != S[k] and S[j] != S[k]:
            cnt += 1
            
print(total-cnt)

Solution (Python):
import copy

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

cnt = [[0]*n for i in range(3)]
r, g, b = 0, 0, 0
for i in range(n-1, -1, -1):
    
    if s[i] == 'R':
        r += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
    if s[i] == 'G':
        g += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
    if s[i] == 'B':
        b += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
        

ans = 0
t = ['R', 'G', 'B']
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] != 'R':
            k = 0
        elif s[i] != 'G' and s[j] != 'G':
            k = 1
        else:
            k = 2
        if 2*j - i < n and s[2*j - i] == t[k]:
            ans += cnt[k][j+1] - 1
        else:
            ans += cnt[k][j+1]
print(ans)

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

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

for start in range(n):
    for kousa in range(n):
        if start+2*kousa>n-1:
            break
        if s[start]!=s[start+kousa] and s[start+kousa]!=s[start+2*kousa] and s[start+2*kousa]!=s[start]:
            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:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
c = Counter(S)
ans = c['R'] * c['G'] * c['B']

for s in range(N-2):
    for d in range(1, (N-1-s)//2+1):
        t = s + d
        u = t + d
        if S[s] != S[t] and S[t] != S[u] and S[u] != S[s]:
            ans -= 1
print(ans)

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

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

cnt = 0
for i in range(n):
    j = 1
    while i + 2*j <= n-1:
        
        if len({s[i], s[i+j], s[i+2*j]}) == 3:
            cnt += 1
        j += 1

print(r*g*b-cnt)

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

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

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

print(r*g*b-cnt)

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

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

ans=R*G*B-rest

print(ans)

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

nr = 0
ng = 0
nb = 0

for i in S:
    if i == 'R':
        nr += 1
    elif i == 'G':
        ng += 1
    elif i == 'B':
        nb += 1
    else:
        raise RuntimeError

ans = nr * ng * nb

for j in range(N):
    for i in range(j):
        k = 2 * j - i
        if k < N:
            if S[i] == S[j]: continue
            if S[i] == S[k]: continue
            if S[j] == S[k]: continue
            ans -= 1

print(ans)