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

import sys
import collections

def solve(N: int, S: str):
    counter = collections.defaultdict(int)
    for s in S:
        counter[s] += 1
    result = 1
    for k, v in counter.items():
        result *= v
    if len(counter) < 3:
        result = 0

    notcounter = 0
    for i in range(N):
        for j in range(i+1, N):
            diff = j - i
            if j + diff >= N:
                break
            if (S[i]!=S[j]) and (S[j]!=S[j+diff]) and (S[i]!=S[j+diff]):
                notcounter += 1
    print(result - notcounter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

r = str.count('R')
g = str.count('G')
b = str.count('B')
sum = 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 str[i] != str[j] and str[i] != str[k] and str[j] != str[k] :
        sum -= 1
    
print(sum)

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

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 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 = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3: 
            cut+=1
d=Counter(s) ; mul=1
for i in d.values():
    mul*=i
if len(d)<3: print(0)
else: print(mul-cut)

Solution (Python):
def main():
    N = int(input())
    S = input()
    cnt = {
        'R': [0] * N,
        'G': [0] * N,
        'B': [0] * N
    }

    for i in range(N):
        if i >= 1:
            for c in cnt:
                cnt[c][i] = cnt[c][i-1]
        c = S[i]
        cnt[c][i] += 1
    ans = 0
    for i in range(N):
        for k in range(i+2, N):
            if S[i] != S[k]:
                c1 = S[i]
                c2 = S[k]
                ans += (k-i) - (cnt[c1][k] - cnt[c1][i]) \
                             - (cnt[c2][k] - cnt[c2][i])
                if (k-i) % 2 == 0:
                    j = i + (k-i) // 2
                    if S[i] != S[j] and S[j] != S[k]:
                        ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

print(max - count)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if not(s[i]==s[j] or s[i]==s[2*j-i] or s[j]==s[2*j-i]): 
            cut+=1
d=Counter(s) ; mul=1
for i in d.values():
    mul*=i
if len(d)<3: print(0)
else: print(mul-cut)

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

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

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter
from numpy import prod

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3:
            cut+=1
d=Counter(s)
if len(d)<3: print(0)
else: print(prod(list(d.values()))-cut)

Solution (Python):
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    cnt = 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 and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]



PARA = {"R": (1, 0, 0), "G": (0, 1, 0), "B": (0, 0, 1)}
PARA2 = {"R": 0, "G": 1, "B": 2}


def main():
    N, S = input(int, str)
    count = [(0, 0, 0)]
    for s in reversed(S):
        left = count[-1]
        count.append(((left[0] + PARA[s][0]),
                      (left[1] + PARA[s][1]),
                      (left[2] + PARA[s][2])))
    count.reverse()
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            last = ({"R", "G", "B"} - {S[i], S[j]}).pop()
            ans += count[j + 1][PARA2[last]]
            if ((j * 2 - i < N) and S[j * 2 - i] == last):
                ans -= 1
    print(ans)

    return


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

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

sum_R = 0
sum_B = 0
sum_G = 0
sum_A = 0

for i in range(N):
    if S[i] == "R":
        sum_R = sum_R +1
    if S[i] == "G":
        sum_G = sum_G+1
    if S[i] == "B":
        sum_B = sum_B +1
     
sum_A = sum_R*sum_G*sum_B

M = int((N-1)/2)

for j in range(1,M+1):
    for i in range(j):
        if S[i]!=S[j] and S[j]!=S[j+(j-i)] and S[i]!=S[j+(j-i)]:
            sum_A = sum_A -1

for j in range(M+1,N):
    for i in range(j+1,N):
        if S[i]!=S[j] and S[j]!=S[j+(j-i)] and S[i]!=S[j+(j-i)]:
            sum_A = sum_A -1
    
print(sum_A)

Solution (Python):
N = int(input())
S = input()
 
 
ans = S.count('R') * S.count('G') * S.count('B')
 
SN = []
 
for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)
 
rem2 = ['RRR', 'GGG', 'BBB']
 
count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1
 
print(ans-count)

Solution (Python):


MOD = 10**9
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)
    return divisors

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


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

SN = []

for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)

rem2 = ['RRR', 'GGG', 'BBB']

count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1

print(ans-count)

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

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

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

print(RGB_cnt[0]*RGB_cnt[1]*RGB_cnt[2]-cnt)

Solution (Python):
n = int(input())
rgb = input()
r = set()
g = set()
b = set()
for i in range(len(rgb)):
    if rgb[i] == 'R':
        r.add(i + 1)
    if rgb[i] == 'G':
        g.add(i + 1)
    if rgb[i] == 'B':
        b.add(i + 1)
otv = 0
for i in r:
    for j in g:
        lb = len(b)
        if (i + j) % 2 == 0 and (i + j) // 2 in b:
            lb -= 1
        if 2 * j - i in b:
            lb -= 1
        if 2 * i - j in b:
            lb -= 1
        otv += lb
print(otv)

Solution (Python):
N=int(input())
S=input()
nums=[[0]*N for _ in range(3)]
for i in range(N):
  if S[i]=='R':
    nums[0][i]+=1
  elif S[i]=='G':
    nums[1][i]+=1
  else:
    nums[2][i]+=1
    
for i in range(1, N):
  nums[0][i]+=nums[0][i-1]
  nums[1][i]+=nums[1][i-1]
  nums[2][i]+=nums[2][i-1]
  
ans=0
for i in range(N-1, 1, -1):
  if S[i]=='R':
    ans+=nums[1][i-1]*nums[2][i-1]
  elif S[i]=='G':
    ans+=nums[0][i-1]*nums[2][i-1]
  else:
    ans+=nums[0][i-1]*nums[1][i-1]
    
tmp=0
for j in range(1, N-1):
  i=j-1
  k=j+1
  while i>=0 and k<=N-1:
    if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      ans-=1
    i-=1
    k+=1
print(ans-tmp)

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

Solution (Python):
A = int(input())
B = input()
count_r = B.count("R")
count_g = B.count("G")
count_b = B.count("B")
ans = count_b * count_g * count_r
for i in range(A):
    for j in range(A):
        d = i+j
        f = d+j
        if f >= A:
            break
        if B[i] != B[d] and B[d] != B[f] and B[i] != B[f]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = list(input())
rgb = s.count('R')*s.count('G')*s.count('B')
for r in (range(n-2)):
    for g in range(r+1,n-1):
        b = g-r+g
        if(b>=n):
            break
        if(s[r] != s[g] and s[r] != s[b] and s[g] != s[b]):
            rgb -= 1
print(rgb)

Solution (Python):

n = int(input())
s = input()
 
RGB = 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 ):
      break
    if (s[i] != s[j]) & (s[j] != s[k]) & (s[i] != s[k]):
      RGB -= 1
print(RGB)

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

cntr=0
cntg=0
cntb=0

for i in range(n):
    if s[i] == "R":
        cntr+=1
    elif s[i] == "G":
        cntg+=1
    else:
        cntb+=1

cntng = 0
cntng += cntr*(cntr-1)//2*(cntg+cntb)
cntng += cntg*(cntg-1)//2*(cntr+cntb)
cntng += cntb*(cntb-1)//2*(cntr+cntg)
cntng += cntr*(cntr-1)*(cntr-2)//6
cntng += cntg*(cntg-1)*(cntg-2)//6
cntng += cntb*(cntb-1)*(cntb-2)//6



for i in range(n):
    for sa in range(1,n):
        if i + 2*sa >= n:
            break
        if s[i] != s[i+sa] and s[i+sa] != s[i+2*sa] and s[i+2*sa] != s[i]:
            cntng+=1

print(n*(n-1)*(n-2)//6 - cntng)

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-1):
  for j in range(i+1,n):
    if j-i <= n-1-j:
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
        cnt+=1
print(R*G*B-cnt)

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

for i in range(N-2):
  for j in range(i+1,N-1):
      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):
import re
import sys
from itertools import product
from typing import Callable, Set, Tuple


scan: Callable[[], str] = sys.stdin.readline


def main():
    n: int = int(scan().rstrip())
    s: str = scan().rstrip()

    r_n: int = s.count("R")
    g_n: int = s.count("G")
    b_n: int = s.count("B")

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

    answer = r_n * g_n * b_n - exceptions
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    A=set(a1[0])
    B=set(a1[1])
    C=set(a1[2])
    for i in A:
        for j in B:
            if -abs(i-j)+min(i,j) in C:
                res-=1
            if abs(i-j)+max(i,j) in C:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in C:
                res-=1
    print(res)

Solution (Python):
N = int(input())
S = input()
total = S.count('R') * S.count('G') * S.count('B')
cnt = 0

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

Solution (Python):
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    A=set(a1[0])
    B=set(a1[1])
    C=set(a1[2])
    for i in A:
        for j in B:
            if -abs(i-j)+min(i,j) in C:
                res-=1
            if abs(i-j)+max(i,j) in C:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in C:
                res-=1
    print(res)

Solution (Python):
a=int(input())
moji=input()
r=set()
g=set()
b=set()
for i in range(a):
    if moji[i]=="R":
        r.add(i)
    elif moji[i]=="G":
        g.add(i)
    else:
        b.add(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    for i in a1[0]:
      for j in a1[1]:
        if -abs(i-j)+min(i,j) in a1[2]:
          res-=1
        if abs(i-j)+max(i,j) in a1[2]:
          res-=1
        if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a1[2]:
          res-=1
    print(res)

Solution (Python):
a=int(input())
moji=input()
r=set()
g=set()
b=set()
for i in range(a):
    if moji[i]=="R":
        r.add(i)
    elif moji[i]=="G":
        g.add(i)
    else:
        b.add(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    for i in a1[0]:
      for j in a1[1]:
        if -abs(i-j)+min(i,j) in a1[2]:
          res-=1
        if abs(i-j)+max(i,j) in a1[2]:
          res-=1
        if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a1[2]:
          res-=1
    print(res)

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


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


for l in range(N): 
  for r in range(l+1, N): 
    c = (r - l) // 2 + l 
    if c - l == r - c: 
      if len(set([S[l], S[c], S[r]])) == 3: 
        ans -= 1

print(ans)

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

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

Solution (Python):
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    for i in range(0,a-2):
      for j in range(i+1,a-1):
        if 2*j-i<a and moji[i]!=moji[j] and moji[j]!=moji[2*j-i] and moji[2*j-i]!=moji[i]:
          res-=1
    print(res)

Solution (Python):
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    for i in range(0,a-2):
      for j in range(i+1,a-1):
        if 2*j-i<a and moji[i]!=moji[j] and moji[j]!=moji[2*j-i] and moji[2*j-i]!=moji[i]:
          res-=1
    print(res)

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

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

ans = r * g * b

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

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]:
            continue
        k = j - i + j
        if k > n-1:
            break
        if s[i] == s[k] or s[j] == s[k]:
            continue
        cnt -= 1
print(cnt)

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

RGB = 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 ):
      break
    if (S[i] != S[j]) & (S[j] != S[k]) & (S[i] != S[k]):
      RGB -= 1


print(RGB)

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





r,g,b=0,0,0

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

ng=0

all=r*g*b


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

        if s[j]!=s[j*2-i] and s[j*2-i]!=s[i]:
          ng+=1
print(all-ng)

Solution (Python):
n = int(input())
s = input()
colors = {'R': 1, 'G' : 2, 'B' : 3}

def dkey(dict, val):
    for e in dict:
        if dict[e] == val:
            return e

sum = [[0] * (n+1) for i in range (3)]
for i in range (3):
    for j in range (n):
        if(s[j] == dkey(colors,i + 1)):
            sum[i][j + 1] = sum[i][j] + 1
        else:
            sum[i][j + 1] = sum[i][j]

ans = 0
for i in range (n-2):
    for j in range (i+1,n-1):
        if s[i] == s[j]:
            continue
        k = 6//(colors[s[i]]*colors[s[j]])
        ans += sum[k-1][n] - sum[k-1][j+1]
        if 2*j-i < n and s[2*j-i] == dkey(colors,k):
            ans -= 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
def main():
    N = int(input())
    S = input().rstrip("\r\n")
    R = []
    G = []
    B = []
    for i, s in enumerate(S):
        if s == "R":
            R.append(i)
        elif s == "G":
            G.append(i)
        else:
            B.append(i)
    ans = 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 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()

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

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

print(count)

Solution (Python):
from itertools import product
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i+1, n):
        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
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n = int(sys.stdin.readline())
    s = input()
    R, G, B = set(), set(), set()
    
    for i in range(n):
        if s[i] == "R":
            R.add(i + 1)
        if s[i] == "G":
            G.add(i + 1)
        if s[i] == "B":
            B.add(i + 1)
    tot = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            l = g - r
            if g + l in B:
                tot -= 1
            if r - l in B:
                tot -= 1
            if l % 2 == 0 and l // 2 + r in B:
                tot -= 1
    print(tot)

    


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Solution (Python):
N=int(input())
S=input()
 
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g
 
ans = r * g
ans = ans * b
 
for i in range( N ):
    for j in range(i, N):
        k = 2 * j - i
        if (i < j) & (j < k) & (k < N) :
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1
 
print(ans)

Solution (Python):
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i+1, int((N+i)/2 +1 )):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
num=[0]*3

for i,co in enumerate(('R','G','B')):
  for c in s:
    if c == co:
      num[i] +=1
      
all = num[0]*num[1]*num[2]

ot = 0
i=1
while i*2 +1 <= len(s):
  idx = 0
  
  while idx + i*2 < len(s):
    
    wk = s[idx]+s[idx+i]+s[idx+2*i]
    
    if ('R' in wk) and ('G' in wk) and ('B' in wk):
      ot +=1
    idx +=1
  i += 1
  
print(all-ot)

Solution (Python):
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1

print(ans)

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):
        k=2*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 = s.count('R')
g = s.count('G')
b = s.count('B')

ret = r*g*b

for i in range(1,int((n+1)/2)):
	for j in range(0,n-2*i):
		if(s[j] != s[j+i]) and (s[j+i] != s[j+2*i]) and (s[j+2*i] != s[j]):
			ret -= 1




print(ret)

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

rcount = S.count("R")
gcount = S.count("G")
bcount = N-rcount-gcount



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

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




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

total_comb = r*g*b

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

print(total_comb - count)

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

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

tmp=0

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

ans = ans - tmp
print(ans)

Solution (Python):


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


R_count = 0
G_count = 0
B_count = 0
for s in list(S):
    if s == 'R':
        R_count += 1
    elif s == 'G':
        G_count += 1
    elif s == 'B':
        B_count += 1


ans = R_count * G_count * B_count


for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (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):
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

len_b = len(b_set)

for r in r_set:
    for g in g_set:
        diff = abs(r - g)
        minus = set()
        if r < g:
            if g + diff in b_set:
                minus.add(g+diff)
            if r - diff in b_set:
                minus.add(r-diff)
        else:
            if r + diff in b_set:
                minus.add(r+diff)
            if g - diff in b_set:
                minus.add(g-diff)
        if (r + g) / 2 in b_set:
            minus.add((r+g)/2)
        ans += len_b - len(minus)

print(ans)

Solution (Python):
def main():
    n=int(input())
    s=input()
    ans = 1

    r_cnt = s.count("R")
    g_cnt = s.count("G")

    ans = r_cnt*g_cnt*(n-r_cnt-g_cnt)
 
    for i in range(n):
        for j in range(i,n):
            s1 = s[i]
            s2 = s[j]
            if s1 != s2:
                t = (s1,s2)
                for s3 in "RGB":
                    if s3 not in t:
                        if 2*j-i < n and s[2*j-i] == s3:
                            ans -= 1
                        continue

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):




N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g
 
ans = r * g * b
 
is_break = False
for i in range(N):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                    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)
    elif s[i]=="G":
        g.append(i)
    else:
        b.append(i)
b=set(b)
lb=len(b)
ans=0
for i in r:
    for j in g:
        x,y=min(i,j),max(i,j)
        ans_=lb
        if x-(y-x) in b:
            ans_-=1
        if y+(y-x) in b:
            ans_-=1
        if (x%2==y%2) and (y-x)//2+x in b:
            ans_-=1
        ans+=ans_
print(ans)

Solution (Python):
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
        
    return CONV()



sys.setrecursionlimit(200005)


def main():
    N = iRaw()
    S = sRaw()
    nRGB = [sum([1 for c in S if c == C]) for C in "RGB"]
    zen = nRGB[0]*nRGB[1]*nRGB[2]

    ans = 0
    ho = 0
    for a in range(1,N):
        c2 = S[a]
        for r in range(1,a+1):
            if a+r >= N:
                continue
            c1 = S[a-r]
            c3 = S[a+r]
            if c1!=c2 and c2!=c3 and c1!=c3:
                ho+=1             
    return zen-ho

if __name__ == "__main__":
    print(main())