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=list(input())
ans=0
how_r=[0 for i in range(n+1)]
how_g=[0 for i in range(n+1)]
how_b=[0 for i in range(n+1)]
for i in range(n):
    string=s[i]
    if string=="R":
        how_r[i+1]+=1
    elif string=="G":
        how_g[i+1]+=1
    elif string=="B":
        how_b[i+1]+=1
for i in range(n):
    how_r[i+1]+=how_r[i]
    how_g[i+1]+=how_g[i]
    how_b[i+1]+=how_b[i]


for  i in range(n):
    for j in range(i+1,n):
        x=s[i]
        y=s[j]
        if x!=y:
            if "R" not in [x,y]:
                ans+=how_r[j+1]-how_r[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="R":
                        ans-=1
                
            
            elif "G" not in [x,y]:
                ans+=how_g[j+1]-how_g[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="G":
                        ans-=1
                
            elif "B" not in [x,y]:
                ans+=how_b[j+1]-how_b[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="B":
                        ans-=1  
print(ans)

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""


"""def Erato(n):
    prime = []
    is_prime = [True] * (n+1)
    p = 0
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
      if is_prime[i] == True:
        prime.append(i)
        p += 1
        for j in range(2 * i,n+1,i):
          is_prime[j] = False
    return p"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  S = input()
  

  R_ = [i for i in range(n) if S[i] == 'R']
  G_ = [j for j in range(n) if S[j] == 'G']
  B_ = [k for k in range(n) if S[k] == 'B']
  
  
  ans = len(R_) * len(G_) * len(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)

  

if __name__ == '__main__':
  main()

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

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

for i in range(n-1):
    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]:
                ans-=1
print(ans)

Solution (Python):
def main():
    N = int(input())
    s = input()

    r, g, b = 0, 0, 0
    for i, c in enumerate(s, 1):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1

    ans = r * g * b
    sub = 0
    for j in range(N):
        for i in range(j):
            if s[i] == s[j]:
                continue
            k = (j - i) + j
            if k >= N:
                continue
            if (s[i] == s[k]) or (s[j] == s[k]):
                continue
            sub += 1  

    ans -= sub

    print(ans)


if __name__ == '__main__':
    main()

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):
n = int(input())
s = input()
s = "0" + s
ans = s.count("R") * s.count("G") * s.count("B")

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

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

def main():
    n = int(input())
    li = list(input().rstrip())

    r = []
    g = []
    b = []

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

    if (len(r) < 1 or len(g) < 1) or len(b) < 1:
        print(0)
    else:
        all_c = len(r) * len(g) * len(b)

        for i in range(n):
            for j in range(i+1,n):
                k = 2*j-i
                if k >= n:
                    continue

                if (li[i] != li[j] and li[j] != li[k]) and li[i] != li[k]:
                    all_c -= 1

        print(all_c)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
R_sum = [0]*N
G_sum = [0]*N
B_sum = [0]*N
rcnt = 0
gcnt = 0
bcnt = 0
cnt = 0
for c in S:
    if c == 'R':
        rcnt += 1
    elif c == 'G':
        gcnt += 1
    elif c == 'B':
        bcnt += 1
    R_sum[cnt] = rcnt
    G_sum[cnt] = gcnt
    B_sum[cnt] = bcnt
    cnt += 1

anssum = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]: continue
        clist = [S[i], S[j]]
        diff_idx = j - i

        if not ('R' in clist):
            anssum += (R_sum[N-1] - R_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'R':
                    anssum -= 1
        elif not ('G' in clist):
            anssum += (G_sum[N-1] - G_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'G':
                    anssum -= 1
        elif not ('B' in clist):
            anssum += (B_sum[N-1] - B_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'B':
                    anssum -= 1
print(anssum)

Solution (Python):
n = int(input())
s = input()
cnt = s.count('R')*s.count('G')*s.count('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-1:
                break
            if s[2*j-i] not in(s[i],s[j]):
                cnt -= 1
print(cnt)

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):

N = int(input())
S = 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

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

print(R*G*B-count)

Solution (Python):



import sys


def input():
    return sys.stdin.readline()


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

    count_dic = {}

    for i in range(n):
        count_dic[str(i)+"R"] = s.count("R", i, n)
        count_dic[str(i)+"G"] = s.count("G", i, n)
        count_dic[str(i)+"B"] = s.count("B", i, n)

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

            if s[i] == "R" and s[j] == "G":
                target_str = "B"
            elif s[i] == "R" and s[j] == "B":
                target_str = "G"
            elif s[i] == "G" and s[j] == "R":
                target_str = "B"
            elif s[i] == "G" and s[j] == "B":
                target_str = "R"
            elif s[i] == "B" and s[j] == "R":
                target_str = "G"
            elif s[i] == "B" and s[j] == "G":
                target_str = "R"

            count += count_dic[str(j)+target_str]

            if 2*j-i < n:
                if target_str == s[2*j-i]:
                    count -= 1
            """
            for k in range(j+1, n):
                if j - i == k -j:
                    continue
                
                if s[i] == s[k] or s[j] == s[k]:
                    continue

                count += 1
            """

    print(count)


if __name__ == "__main__":
    resolve()

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):
from bisect import bisect_right
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if Ron[i]==1:
        
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(B)>0:
                ind1 = bisect_right(B,indg)
                if 2*indg-indr>B[-1] or Bon[2*indg-indr]==0 or Bon[2*indg-indr]==1 and 2*indg-indr<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
        
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(G)>0:
                ind1 = bisect_right(G,indb)
                if 2*indb-indr>G[-1] or Gon[2*indb-indr]==0 or Gon[2*indb-indr]==1 and 2*indb-indr<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
    elif Gon[i]==1:
        
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(R)>0:
                ind1 = bisect_right(R,indb)
                if 2*indb-indg>R[-1] or Ron[2*indb-indg]==0 or Ron[2*indb-indg]==1 and 2*indb-indg<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
        
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(B)>0:
                ind1 = bisect_right(B,indr)
                if 2*indr-indg>B[-1] or Bon[2*indr-indg]==0 or Bon[2*indr-indg]==1 and 2*indr-indg<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
    elif Bon[i]==1:
        
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(G)>0:
                ind1 = bisect_right(G,indr)
                if 2*indr-indb>G[-1] or Gon[2*indr-indb]==0 or Gon[2*indr-indb]==1 and 2*indr-indb<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
        
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(R)>0:
                ind1 = bisect_right(R,indg)
                if 2*indg-indb>R[-1] or Ron[2*indg-indb]==0 or Ron[2*indg-indb]==1 and 2*indg-indb<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
print(cnt)

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 = 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)
main()

Solution (Python):

N = int(input())
S = 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

count = 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] and S[k]!=S[i] and S[k]!=S[j]:
      count += 1

print(R*G*B-count)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    S = readline().strip()

    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(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]:
                ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = 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(1, (n-i-1)//2+1):
        if s[i]!=s[i+j] and s[i]!=s[i+2*j] and s[i+j]!=s[i+2*j]:
            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,n):
    if 2*j-i>=n:
      break
    else:
      if s[i]!=s[j]!=s[2*j-i]!=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):
n=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for j in range(n):
  J=s[j]
  for i in range(j):
    k=2*j-i
    if k<n:
      I=s[i]
      K=s[k]
      if I!=J!=K!=I:
        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 - 1):
    for j in range(i + 1, n):
        k = j + j - i

        if k >= n:
            break

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

Solution (Python):
import math
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(i+1,math.ceil((i+n)/2)):
    if s[i] != s[j] and s[j] != s[j*2-i] and s[j*2-i] != s[i]:
      ans -= 1
print(ans)

Solution (Python):
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()

if len(set(S)) == 1 or N < 3:
    print(0)
    sys.exit()

Rs = S.count("R")
Gs = S.count("G")
Bs = S.count("B")
ans = Rs * Gs * Bs


for i in range(N - 1):
    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=set()
g=set()
b=set()

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

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in r:
    for ig in g:
        if (ir+ig)/2 in b: 
            isum=isum+1
for ir in r:
    for ib in b:
        if (ir+ib)/2  in g: 
            isum=isum+1
for ig in g:
    for ib in b:
        if (ig+ib)/2  in r: 
            isum=isum+1

isum=lenr*leng*lenb-isum
                
print(isum)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

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

R = []
G = []
B = []

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

base = len(R)*len(G)*len(B)

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if k >= N or S[i] == S[j] or S[j] == S[k] or S[k] == S[i]:
            continue
        cnt += 1

print(base-cnt)

Solution (Python):
def main():
  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(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Solution (Python):
import itertools

n = int(input())
s = input().rstrip()

colname = {'R': 0, 'G': 1, 'B': 2}
color = [[], [], []]
for i in range(n):
    ci = colname[s[i]]
    color[ci].append(i + 1)
color = sorted(color, key=lambda x: len(x))

color[2] = set(color[2])
nc2 = len(color[2])

x = 0
for r in color[0]:
    for g in color[1]:
        x += nc2
        if (r + r - g) in color[2]:
            x -= 1
        if (g + g - r) in color[2]:
            x -= 1
        c = r + g
        if c % 2 == 0 and (c // 2) in color[2]:
            x -= 1
print(x)

Solution (Python):

N = int(input())
S = 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
    
count = 0
for i in range(N-1):
  for j in range(i+1,N):
    if 2*j-i>=N:
      break
    if S[i]!=S[j] and S[2*j-i]!=S[i] and S[2*j-i]!=S[j]:
      count += 1

print(R*G*B-count)

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

Solution (Python):
n,s=open(0);n=int(n);c=s.count;print(c('R')*c('G')*c('B')-sum(s[i]!=s[i+j>>1]!=s[j]!=s[i]for i in range(n)for j in range(i+2,n,2)))

Solution (Python):
a=int(input())
b=str(input())
sum=0

riron_sum=b.count('R')*b.count('G')*b.count('B')

for i in range(0,a):
    for n in range(1,a):
        j = i+n
        k = j+n
        
        
        if k>=a:
        	break

        if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
                riron_sum-=1

print(riron_sum)

Solution (Python):
import bisect
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)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(B))
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(G))
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & set(B))
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & set(G))
print(res)

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

nr=s.count('R')
ng=s.count('G')
nb=s.count('B')
ans=nr*ng*nb
cnt=0

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

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

Solution (Python):
N = int(input())
S = list(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 * B * G - f(S)
print(ans)

Solution (Python):
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(map(str, input().split()))
def pl(a): print(" ".join(list(map(str, a))))
def ma():return map(int,input().split())
"""
k=ri()

n,k=ma()
for i in range(n):
    for

n=int(input())
s=input()
r=[0]*n
b=[0]*n
g=[0]*n

for i in range(n):
    if s[i]=="R":
"""

"""
from itertools import accumulate as acc
import operator as op
import copy as cp
import math; import numpy
from bisect import bisect_left as bisl
from bisect import bisect_right as bisr

"""



"""
import math
k=int(input())

cnt=0
for a in range(1,k+1):
    for b in range(1,k+1):
        ct=math.gcd(a,b)
        for c in range(1,k+1):
            cnt+=math.gcd(ct,c)

print(cnt)
"""
"""
n,k=map(int,input().split())
mod=10**9+7

import math
dj0=[[i for i in range(1,k+1)] for j in range(k)]
dj1=[i for i in range(1,k+1)]
dj2=[0]*k
for i in dj1:
    for j in range(1,k):
        dj0[i-1][j-1]=math.gcd(i,j)
        dj2[math.gcd(i,j)]+=1
"""
n=ri()
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
        ans-=s[k]!=s[i]!=s[j]!=s[k]

print(ans)

Solution (Python):
n=int(input())
s=[ord(c)%3for c in input()]
d=[0]*3
for i in s:d[i]+=1
a=0
for i in range(n):
    d[s[i]]-=1
    b=d.copy()
    for j in range(i+1,n):
        b[s[j]]-=1
        if s[i]==s[j]:continue
        c=(0,2,1,0)[s[i]+s[j]]
        a+=b[c]
        k=j+j-i
        if k<n and s[k]==c:
            a-=1
print(a)

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):
        d = j - i
        k = j + d
        if k > N-1:
            break
        if {S[i], S[j], S[k]} == {'R', 'G', '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):
  for j in range(i+1,n):
    k = 2*j-i
    if k < n:
      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:
            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()
ans = S.count("R") * S.count("G") * S.count("B")
for i in range(N):
    for j in range(i + 1, N):
        d = j - i
        k = j + d
        if k > N-1:
            break
        if {S[i], S[j], S[k]} == {'R', 'G', 'B'}:
            ans -=1
print(ans)

Solution (Python):
a=int(input())
b=str(input())
sum=0

riron_sum=b.count('R')*b.count('G')*b.count('B')

for i in range(0,a):
    for n in range(0,a+1):
        j = i+n
        k = j+n
        
        
        if k>=a:
        	break

        if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
                riron_sum-=1

print(riron_sum)

Solution (Python):
import numpy as np
N = int(input())
S = input()

l_r = []
l_g = []
l_b = []
for i in range(N):
  if S[i]=='R':
    l_r.append(i)
  if S[i]=='G':
    l_g.append(i)
  if S[i]=='B':
    l_b.append(i)
    
l_r = set(l_r)
l_g = set(l_g)
l_b = set(l_b)

ans = len(l_r)*len(l_g)*len(l_b)

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Solution (Python):
N=int(input())
S=list(map(str,input()))
L=[0,0,0]
for i in range(N):
  if S[i]=="R":
    L[0]+=1
  elif S[i]=="G":
    L[1]+=1
  elif S[i]=="B":
    L[2]+=1
ans=L[0]*L[1]*L[2]

for i in range(N-2):
  for j in range(int((N-i-1)/2)):
    if S[i]!=S[i+j+1] and S[i]!=S[i+2*j+2] and S[i+j+1]!=S[i+2*j+2]:
      ans-=1

print(str(ans))

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

ans = 0
for j in range(1, N-1):
    left_S = S[0:j]
    right_S = S[j+1:N]
    temp_left = [0, 0, 0]
    temp_right = [0, 0, 0]
    for i in range(len(left_S)):
        if left_S[i] == 'G':
            temp_left[0] += 1
        elif left_S[i] == 'B':
            temp_left[1] += 1
        else:
            temp_left[2] += 1
    for i in range(len(right_S)):        
        if right_S[i] == 'G':
            temp_right[0] += 1
        elif right_S[i] == 'B':
            temp_right[1] += 1
        else:
            temp_right[2] += 1
    if S[j] == 'G':
        ans += temp_left[1] * temp_right[2]
        ans += temp_left[2] * temp_right[1]
    elif S[j] == 'B':
        ans += temp_left[0] * temp_right[2]
        ans += temp_left[2] * temp_right[0]
    else:
        ans += temp_left[0] * temp_right[1]
        ans += temp_left[1] * temp_right[0]

for j in range(1, N-1):
    for i in range(1, min(len(S[0:j+1]), len(S[j:N]))):
        if S[j-i] != S[j] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

Solution (Python):
n,s=open(0);n=int(n);c=s.count;print(c('R')*c('G')*c('B')-sum(s[i]!=s[j]!=s[i+j>>1]!=s[i]for i in range(n)for j in range(i+2,n,2)))

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[j] != s[k]:
            ans -= 1
print(ans)

Solution (Python):
n=int(input())
s=input()
r=0
g=0
b=0
for i in range(n):
  if s[i]=="R":
    r+=1
  if s[i]=="G":
    g+=1 
  if s[i]=="B":
    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:
      continue
    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()



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


for i in range(N):
    for j in range(1,N):
        a = i + j
        b = i + j + j
        if (b >= N):
            break
        else:
            l = S[i]
            m = S[a]
            r = S[b]
            if l!=m and m!=r and r!=l:
                ans -= 1
        
        
        
        
        
        
        
print (ans)

Solution (Python):


n = int(input())
s = input()
RL = []
GL = []
BL = []
for i in range(1, n + 1):
    word = s[i - 1]
    if word == "R":
        RL.append(i)
    elif word == "G":
        GL.append(i)
    else:
        BL.append(i)
nagasa_r = len(RL)
nagasa_g = len(GL)
nagasa_b = len(BL)
SBL = set(BL)
ans = 0
if nagasa_b == 0 or nagasa_g == 0 or nagasa_r == 0:
    ans = 0
else:
    out_count = 0
    for r in RL:
        for g in GL:
            bigger = max(r, g)
            smaller = min(r, g)
            delta = bigger - smaller
            if (r + g) % 2 == 0:
                outnumber = [bigger + delta, smaller - delta, (r + g) // 2]
            else:
                outnumber = [bigger + delta, smaller - delta]
            for num in outnumber:
                if num in SBL:
                    out_count += 1
            ans += nagasa_b
    ans -= out_count
print(ans)

Solution (Python):
from itertools import accumulate

def solve():
    N = int(input())
    Ss = input().rstrip()

    Rs, Gs, Bs = [], [], []
    numRs = [0] * N
    numGs = [0] * N
    numBs = [0] * N
    for i in range(N):
        S = Ss[i]
        if S == 'R':
            Rs.append(i)
            numRs[i] = 1
        elif S == 'G':
            Gs.append(i)
            numGs[i] = 1
        else:
            Bs.append(i)
            numBs[i] = 1

    lenR, lenG, lenB = len(Rs), len(Gs), len(Bs)
    setR = set(Rs)
    setG = set(Gs)
    setB = set(Bs)

    accNumRevRs = list(accumulate(numRs[::-1]))[::-1]
    accNumRevGs = list(accumulate(numGs[::-1]))[::-1]
    accNumRevBs = list(accumulate(numBs[::-1]))[::-1]

    ans = 0
    for j in range(N):
        S = Ss[j]
        if S == 'R':
            for i in Gs:
                if i > j:
                    break
                ans += accNumRevBs[j]
                k = 2*j-i
                if k in setB:
                    ans -= 1
            for i in Bs:
                if i > j:
                    break
                ans += accNumRevGs[j]
                k = 2*j-i
                if k in setG:
                    ans -= 1
        elif S == 'G':
            for i in Rs:
                if i > j:
                    break
                ans += accNumRevBs[j]
                k = 2*j-i
                if k in setB:
                    ans -= 1
            for i in Bs:
                if i > j:
                    break
                ans += accNumRevRs[j]
                k = 2*j-i
                if k in setR:
                    ans -= 1
        else:
            for i in Rs:
                if i > j:
                    break
                ans += accNumRevGs[j]
                k = 2*j-i
                if k in setG:
                    ans -= 1
            for i in Gs:
                if i > j:
                    break
                ans += accNumRevRs[j]
                k = 2*j-i
                if k in setR:
                    ans -= 1

    print(ans)


solve()

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

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

for i in range(N):
    for d in range(N):
        j = i + d
        k = i + 2 * 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 = str(input())

r = len([i for i, x in enumerate(s) if x=='R'])
g = len([i for i, x in enumerate(s) if x=='G'])
b = len([i for i, x in enumerate(s) if x=='B'])

ans = r * g * b

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)