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()
a,b = '',''
r = S.count('R')
g = S.count('G')
b = len(S)-r-g
rgb = r*g*b
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,(N+i-1)//2+1):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if S[k]!=b and S[k]!=a:
        expt += 1
print(rgb-expt)

Solution (Python):
n=int(input())
ss=list(input())
res= ss.count('R') * ss.count('G') * ss.count('B')
for i in range(n):
  for j in range(i+1,n):
    k = 2*j-i
    if k >= n:
      continue
    if all([ss[i] != ss[j],ss[j] != ss[k],ss[k] != ss[i]]):
      res-=1
print(res)

Solution (Python):
from collections import Counter
n=int(input())
stri=list(input())
cc=Counter(stri)

count=cc['R']*cc['G']*cc['B']


for i in range(n-2):
  for j in range(i+1,n-1):
    if stri[j]==stri[i]:continue
      
    lib=['R','G','B']
    lib.remove(stri[i])
    lib.remove(stri[j])
    t=lib.pop()
    
    skip_k=2*j-i
    
    if skip_k>n-1:continue
    if stri[skip_k]==t:
      count-=1
print(count)

Solution (Python):
n = int(input())
s = list(input())
r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    if c == "G":
        g += 1
    if c == "B":
        b += 1
ans = r*g*b

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

print(ans)

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

r = s.count('R')
g = s.count('G')
b = n - r - g
ans = r*g*b

for i in range(n-2):
  for j in range(i+1,i+((n-i-1)//2)+1):
    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):
n = int(input())
s = list(input())

r = s.count('R')
g = s.count('G')
b = len(s) - r - g
ans = r*g*b

for i in range(n-2):
  for j in range(i+1,i+((n-i-1)//2)+1):
    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):
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(n-2):
  for j in range(i+1,i+((n-i-1)//2)+1):
    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):
n=int(input())
s=input()
dic={}
cntr=s.count("R")
cntg=s.count("G")
cntb=s.count("B")
for i in range(n):
    if s[i]=="R":
        dic[i]="R"
    if s[i]=="G":
        dic[i]="G"
    if s[i]=="B":
        dic[i]="B"

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if dic[i]!= dic[j] and dic[i]!=dic[2*j-i] and dic[j]!=dic[2*j-i]:
                cnt+=1
        else:
            break
print(cntr*cntg*cntb-cnt)

Solution (Python):
n=int(input())
s=input()
dic={}
cntr=0
cntg=0
cntb=0
for i in range(n):
    if s[i]=="R":
        dic[i]="R"
        cntr+=1
    if s[i]=="G":
        dic[i]="G"
        cntg+=1
    if s[i]=="B":
        dic[i]="B"
        cntb+=1

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1 and dic[i]!= dic[j]:
            if dic[i]!=dic[2*j-i] and dic[j]!=dic[2*j-i]:
                cnt+=1
print(cntr*cntg*cntb-cnt)

Solution (Python):
n = int(input())
s = input()
R = s.count('R')
G = s.count('G')
B = s.count('B')
ans = 0
if n <= 2:
    print(0)
    exit()
for i in range(1,(len(s)-1)//2+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+2*i] != s[j]:
            ans -=1
print(R*G*B + ans)

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

Solution (Python):
import itertools
N = int(input())
S = input()
ls = list(S)

nr = len([r for r in S if r == 'R'])
nb = len([b for b in S if b == 'B'])
ng = len([g for g in S if g == 'G'])

ALL = nr * nb * ng
count = 0

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      count += 1
      
print(ALL - count)

Solution (Python):
import sys
import math
input = sys.stdin.readline
n = int(input())
s = input().strip()
def idx(c):
	if c == 'R':
		return 0
	elif c == 'G':
		return 1
	else:
		return 2

cnt = [[0] * n, [0] * n, [0] * n]
for i in range(n):
	for c in range(3):
		if i > 0:
			cnt[c][i] = cnt[c][i - 1]
	cnt[idx(s[i])][i] += 1

ans = 0
for i in range(n):
	for j in range(i + 1, n):
		if s[i] != s[j]:
			rem = 3 - idx(s[i]) - idx(s[j])
			ans += cnt[rem][j - 1] - cnt[rem][i]
			if (i + j) % 2 == 0 and idx(s[(i + j) // 2]) == rem:
				ans -= 1
print(ans)

Solution (Python):

n=int(input())
s=input()
r=g=b=0
for c in s:
 if c=='R':
  r+=1
 elif c=='G':
  g+=1
 else:
  b+=1
all=r*g*b
sub=0
for i in range(n):
 for j in range(i+1,n):
  if s[i]==s[j]:
   continue
  k=j*2-i
  if k>=n or s[k]==s[i] or s[k]==s[j]:
   continue
  sub+=1
print(all-sub)

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

    for i in range(n):
        if (n+i)%2==0:
            j=np.arange(i+1,(n+i)//2)
            k=2*j-i
        else:
            j=np.arange(i+1,(n+i)//2+1)
            k=2*j-i

        if j.size==0:
            break
        i=np.full(j.size,i,dtype=np.int64)
        tmp=t[i]*t[j]*t[k]
        ans-=tmp[tmp==6].size

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    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
            x=s[i]
            y=s[j]
            z=s[k]
            if x!=y and y!=z and z!=x:
                ans-=1

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
S = '0'
S += input()
R_count = 0
G_count = 0
B_count = 0
for i in range(1, N + 1):
  if S[i] == 'R':
    R_count += 1
  if S[i] == 'G':
    G_count += 1
  if S[i] == 'B':
    B_count += 1
res = R_count * G_count * B_count
for i in range(1, N + 1):
  for j in range(i + 1, N + 1):
    if 2 * j - i > N:
      break
    temp = []
    temp += S[i] + S[j] + S[2 * j - i]
    temp.sort()
    if temp[0] == 'B' and temp[1] == 'G' and temp[2] == 'R':
      res -= 1
print(res)

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

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

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

print(r*g*b-c)

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

r_c = 0
g_c = 0
b_c = 0
for c in s:
    if c == 'R':
        r_c += 1
    if c == 'G':
        g_c += 1
    if c == 'B':
        b_c += 1
rgb_count = r_c * g_c * b_c

MAX = min(len(s) + 1, 4005)
omit_count = 0
for i in range(1, len(s) + 1):
    for j in range(i + 1, len(s) + 1):
        k = 2 * j - i
        if k >= len(s) + 1:
            continue
        if s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]:
            omit_count += 1
print(rgb_count - omit_count)

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [(0, 0, 0)]
for c in s:
    rgb = counter[-1]
    r, g, b = 0, 0, 0
    if c == 'R':
        r = 1
    if c == 'G':
        g = 1
    if c == 'B':
        b = 1
    new_rgb = (rgb[0] + r, rgb[1] + g, rgb[2] + b)
    counter.append(new_rgb)

ans = 0
MAX = 4002
x = []
for i in range(1, min(len(s) + 1, MAX)):
    for j in range(i + 1, min(len(s) + 1, MAX)):
        if s[i - 1] == s[j - 1]:
            continue
        x.append((i, j))

y = []
for p in x:
    i = p[0]
    j = p[1]
    rgb = {'R', 'G', 'B'}
    sub = {s[i - 1], s[j - 1]}

    k_s = rgb.difference(sub).pop()
    k_idx = 0
    if k_s == 'R':
        k_idx = 0
    if k_s == 'G':
        k_idx = 1
    if k_s == 'B':
        k_idx = 2

    j_cnt = counter[j]
    k_cnt = counter[len(s)]
    omit = 0
    if (2 * j - i) <= len(s):
        omit = counter[2* j - i][k_idx] - counter[2 * j - i - 1][k_idx]
    ans += (k_cnt[k_idx] - j_cnt[k_idx] - omit)

print(ans)

Solution (Python):

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

Solution (Python):
N=int(input())
S=input()
rgb={"R":0,"G":0,"B":0}
for s in S:
  rgb[s]+=1
ans=rgb["R"]*rgb["G"]*rgb["B"]
for dist in range(1,N//2+1):
  left=0
  while left+dist*2<N:
    if len(set([S[left],S[left+dist],S[left+dist*2]]))==3:
      ans-=1
    left+=1
print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
s = list(input())
c = defaultdict(int)

for i in s:
    if i=="R":
        c["R"]+=1
    elif i=="G":
        c["G"]+=1
    else:
        c["B"]+=1
ans = c["R"]*c["G"]*c["B"]
nans = 0
for i in range(n-2):
    j = 1
    while i+2*j<=n-1:
        
        if s[i] != s[i+j] and s[i] != s[i+j*2] and s[i+j] != s[i+j*2]:
            nans+=1
        j+=1
print(ans-nans)

Solution (Python):
N=int(input())
S=input()
r=b=g=0
for s in S:
    if s=='R':
        r+=1
    elif s=='B':
        b+=1
    else:
        g+=1
out=0
for i in range(1,N):
    for j in range(min(i+1,N-i)):
        if S[i]!=S[i+j] and S[i]!=S[i-j] and S[i+j]!=S[i-j]:
            out+=1
print(r*b*g-out)

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

c = collections.Counter(S)
R = c['R']
G = c['G']
B = c['B']
ans = R*G*B
delans = 0
for i in range(1,N-1):
    for l in range(min(i,N-i-1)):
        if S[i]!=S[i-l-1] and S[i]!=S[i+l+1] and S[i-l-1]!=S[i+l+1]:
            delans += 1
ans -= delans
print(ans)

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

from collections import Counter
rgb = dict(Counter(s))
if len(rgb) < 3:
    print('0')
else:
    p = rgb['R'] * rgb['G'] * rgb['B']
    for i in range(1, (len(s)-1)//2 + 1):
        for j in range(len(s) - 2*i):
            if {s[j], s[j + i], s[j + 2*i]} == {'R', 'G', 'B'}:
                p -= 1
    print(p)

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

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

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


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


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

print(sum_collection)

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

listr = []
listg = []
listb = []
listc = [0]*L
ans = 0

for i in range(L):
    if(S[i] == "R"):
        listr.append(i)
        listc[i] = 0
    elif(S[i] == "G"):
        listg.append(i)
        listc[i] = 1
    else:
        listb.append(i)
        listc[i] = 2
if(listr == [] or listg == [] or listb == []):
    print(0)
else:
    for i in listr:
        for j in listg:
            div = abs(j - i)
            ans += len(listb)
            mi = min(i,j) - div
            ma = max(i,j) + div
            if(div%2 == 0):
                k = min(i,j) + div//2
                if(listc[k] == 2):
                    ans -= 1
                if(mi >= 0):
                    if(listc[mi] == 2):
                        ans -= 1
                if(ma < L):
                    if(listc[ma] == 2):
                        ans -= 1
            else:
                if(mi >= 0):
                    if(listc[mi] == 2):
                        ans -= 1
                if(ma < L):
                    if(listc[ma] == 2):
                        ans -= 1
    print(ans)

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

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

ans = r*g*b

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

print(ans)

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

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

ans = r*g*b

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

print(ans)

Solution (Python):
from collections import Counter
n=int(input())
s_=input()
s=Counter(s_)
rgb = s['R'] * s['G'] * s['B']
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<n:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Solution (Python):

n=int(input())
s_=input()
r, g, b = s_.count("R"), s_.count("G"), s_.count("B")
rgb = r * g * b
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<n:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Solution (Python):

import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = sr()
counter = Counter(S)
r = counter['R']; g = counter['G']; b = counter['B']
answer = r * g * b
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        diff = j - i
        if j + diff >= N:
            break
        x = S[i]; y = S[j]
        z = S[j+diff]
        if x != z and y != z:
            answer -= 1

print(answer)

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)

cnt=len(R)*len(G)*len(B)
for i in R:
    for j in G:
        koho=set()
        if (j+i)%2==0:
            koho.add((j+i)//2)
        if 0<=2*i-j<N:
            koho.add(2*i-j)
        if 0<=2*j-i<N:
            koho.add(2*j-i)
        for k in koho:
            if k in B:
                cnt-=1
            
                
print(cnt)

Solution (Python):
import collections

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

CountRGB = collections.Counter(S)

ans = CountRGB["R"] * CountRGB["G"] * CountRGB["B"]

tmp = 0
for i in range(N-2):
    for j in range(i+1, N-1):

        if(2 * j - i > N - 1):
            break

        checker = []
        checker.append(S[i])
        checker.append(S[j])
        checker.append(S[2 * j - i])

        c = collections.Counter(checker)

        if(c["R"] == 1 and c["G"] == 1 and c["B"] == 1):
            tmp +=1

print(ans - tmp)

Solution (Python):
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):
        k = j + (j - i)
        if k >= n:
            break
        if s[i] != s[j] != s[k] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
CC = Counter(S)

if len(CC) < 3:
  print(0)
  exit(0)

ans = CC['R'] * CC['G'] * CC['B']
for i in range(N):
  for x in range(1,N):
    if i+2*x >= N:
      break
    if S[i] != S[i+x] and S[i+x] != S[i+2*x] and S[i+2*x] != S[i]:
      ans -= 1
      
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
CC = Counter(S)

if len(CC) < 3:
  print(0)
  exit(0)

ans = CC['R'] * CC['G'] * CC['B']
for i in range(N):
  for x in range(1,N):
    if i+2*x >= N:
      break
    if S[i] != S[i+x] and S[i+x] != S[i+2*x] and S[i+2*x] != S[i]:
      ans -= 1
      
print(ans)

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

cnt = [[0, 0, 0] for _ in range(n)]

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

res = 0
for i in range(1, n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        if s[i] != 'R' and s[j] != 'R':
            res += cnt[i - 1][0]
            if i + i - j >= 0: res -= s[i + i - j] == 'R'
        if s[i] != 'G' and s[j] != 'G':
            res += cnt[i - 1][1]
            if i + i - j >= 0: res -= s[i + i - j] == 'G'
        if s[i] != 'B' and s[j] != 'B':
            res += cnt[i - 1][2]
            if i + i - j >= 0: res -= s[i + i - j] == 'B'

print(res)

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

    C={"R":0,"G":0,"B":0}

    for i in range(N):
        C[S[i]]+=1

    excepter=0

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

    res=C["R"]*C["G"]*C["B"]-excepter
    print(res)
if __name__=="__main__":
    main()

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

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

from collections import defaultdict

d = defaultdict(int)

for ix, c in enumerate(S):
    
    d[c] += 1


ans = d["R"] * d["G"] * d["B"]

for i in range(1, N+1):
    ci = S[i-1]
    for j in range(i+1, N+1):
        cj = S[j-1]

        if ci == cj:
            continue

        k = j + j - i
        if k <= N:
            ck = S[k-1]
            if ci != ck and cj != ck:
                ans -= 1
        else:
            break
print(ans)

Solution (Python):
import collections
n = int(input())
s = [''] + list(input())
cnt_1 = collections.Counter(s)
 
sub = 0
for j in range(1, (n+1)//2):
    for i in range(1, n-2*j+1):
        tmp = s[i] + s[i+j] + s[i+j*2]
        cnt_2 = collections.Counter(tmp)
        if max(cnt_2.values()) == 1:
            sub += 1
 
print((cnt_1['R']*cnt_1['G']*cnt_1['B'])-sub)

Solution (Python):
import collections
n = int(input())
s = [''] + list(input())
cnt_1 = collections.Counter(s)

sub = 0
for j in range(1, (n+1)//2):
    for i in range(1, n-2*j+1):
        tmp = s[i] + s[i+j] + s[i+j*2]
        cnt_2 = collections.Counter(tmp)
        if cnt_2['R'] == 1 and cnt_2['G'] == 1 and cnt_2['B'] == 1:
            sub += 1

print((cnt_1['R']*cnt_1['G']*cnt_1['B'])-sub)

Solution (Python):
n=int(input())
s=input()
r=[0]*n
g=[0]*n
b=[0]*n
R=0; G=0; B=0
for i in range(n):
    if s[i]=="R":
        r[i]=1
        R+=1
    elif s[i]=="G":
        g[i]=1
        G+=1
    else:
        b[i]=1
        B+=1
s=0
for j in range(n):
    for k in range(n):
        if r[j]==1 and g[k]==1:
            if j!=k:
                j1,k1=(max(j,k),min(j,k))
                if (j1-k1)+j1<n:
                    if b[(j1-k1)+j1]==1:
                        s+=1
                if k1-(j1-k1)>=0:
                    if b[k1-(j1-k1)]==1:
                        s+=1
                if (j1-k1)>1 and (j1-k1)%2==0:
                    if b[k1+(j1-k1)//2]==1:
                        s+=1
print(R*G*B-s)

Solution (Python):

import sys
from collections import deque

def solve(N: int, S: str):
    rp = 0
    gp = 0
    bp = 0
    rsum = [0]*(N+1)
    bsum = [0]*(N+1)
    gsum = [0]*(N+1)

    for i, x in enumerate(S[::-1]):
        if x ==  "R": rp = 1
        elif x =="G": gp = 1
        else: bp = 1
        rsum[N-1-i] = rsum[N-1-i+1] + rp
        gsum[N-1-i] = gsum[N-1-i+1] + gp
        bsum[N-1-i] = bsum[N-1-i+1] + bp
        rp = 0
        gp = 0
        bp = 0
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] != S[j]:
                if j+j-i<N: k = S[j+j-i]
                else: k = ""
                if (S[i] == "R" and S[j] == "B") or (S[j] == "R" and S[i] == "B"):
                    ans += gsum[j] - (k=="G")*1
                elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                    ans += bsum[j] - (k=="B")*1
                elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                    ans += rsum[j] - (k=="R")*1

    print(ans)
    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):

import sys
from collections import deque

def solve(N: int, S: str):
    r = []
    g = []
    b = []
    rp = 0
    gp = 0
    bp = 0

    rsum = [0]*(N+1)
    bsum = [0]*(N+1)
    gsum = [0]*(N+1)

    for i, x in enumerate(S[::-1]):
        if x ==  "R":
            r.append(i)
            rp = 1
        elif x =="G":
            g.append(i)
            gp = 1
        else:
            b.append(i)
            bp = 1
        rsum[N-1-i] = rsum[N-1-i+1] + rp
        gsum[N-1-i] = gsum[N-1-i+1] + gp
        bsum[N-1-i] = bsum[N-1-i+1] + bp
        rp = 0
        gp = 0
        bp = 0
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] != S[j]:
                if j+j-i<N: k = S[j+j-i]
                else: k = ""
                if (S[i] == "R" and S[j] == "B") or (S[j] == "R" and S[i] == "B"):
                    ans += gsum[j] - (k=="G")*1
                elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                    ans += bsum[j] - (k=="B")*1
                elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                    ans += rsum[j] - (k=="R")*1
    

    print(ans)
  
    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):
import sys
from io import StringIO
import unittest


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

    
    s = list(input())

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

    
    s_len = len(s)
    minus = 0

    
    for i in range(s_len-2):
        first = s[i]
        for j in range(1, 9999):

            
            if i+j+j > s_len - 1:
                break

            
            if first is not s[i+j] and first is not s[i+j+j] and s[i+j] is not s[i+j+j]:
                minus += 1

    print(ans - minus)




    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)







if __name__ == "__main__":
    
	resolve()

Solution (Python):
N=int(input())
S=input()
ans=0
RGB=[0,0,0]
for i in range(N):
  if S[i]=="R":
    RGB[0]+=1
  if S[i]=="G":
    RGB[1]+=1
  if S[i]=="B":
    RGB[2]+=1
ans=RGB[0]*RGB[1]*RGB[2]    
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]
    t=k+k-i
    if s1==s2:
      continue
    if t<=N-1:
      s3=S[t]
      if s3!=s1 and s3!=s2:
        ans-=1  
    else:
      break
print(ans)

Solution (Python):
N=int(input())
S=input()
ans=0
RGB=[0,0,0]
for i in range(N):
  if S[i]=="R":
    RGB[0]+=1
  if S[i]=="G":
    RGB[1]+=1
  if S[i]=="B":
    RGB[2]+=1
ans=RGB[0]*RGB[1]*RGB[2]    
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]
    t=k+k-i
    if s1==s2:
      continue
    if t<=N-1:
      s3=S[t]
      if s3!=s1 and s3!=s2:
        ans-=1      
print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cnt = 0
cnt += S.count("R") * S.count("G") * S.count("B")









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

print(cnt)

Solution (Python):
N = int(input())
S = input()
R = 0
G = 0
B = 0
for i in range(N):
    s = S[i]
    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):
    for j in range(i+1,N-1):
        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):
def main():
    rc = S.count("R")
    gc = S.count("G")
    bc = S.count("B")
    ns = 0
    for i in range(N-2):
        for k in range(i+2, N):
            if (i+k)%2 == 0 and S[i] != S[k]:
                j = (i+k)//2
                ns += 1 if S[j] != S[i] and S[j] != S[k] else 0
    print(rc*gc*bc - ns)


if __name__ == "__main__":
    N = int(input())
    S = input()
    main()

Solution (Python):
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
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,N):
        k = 2*j - i
        if k >= N:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            ans -= 1


print(ans)

Solution (Python):
n=int(input())
s=str(input())
t=len(s)

r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        if 2*ri-gi in b:
            d-=1
        if 2*gi-ri in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Solution (Python):
n=int(input())
s=str(input())
t=len(s)

r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        rg=gi-ri
        if rg+gi in b:
            d-=1
        if  ri-rg in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

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

k += '.'*n

R = [0] * (n*3)
G = [0] * (n*3)
B = [0] * (n*3)

dic = { 'R' : 0, 'G' : 0, 'B' : 0 };
for i in range(n-1, -1, -1):
    dic[k[i]] += 1
    R[i] = dic['R']
    G[i] = dic['G']
    B[i] = dic['B']

ans = 0
for i in range(n):
    for j in range(i+1, n):
        if k[i] != k[j]:
            charset = ['R', 'G', 'B'];
            charset.remove(k[i]);
            charset.remove(k[j]);
            
            if charset[0] == 'R':
                ans += R[j + 1]
                ans -= (1 if k[j + (j - i)] == 'R' else 0)
            if charset[0] == 'G':
                ans += G[j + 1]
                ans -= (1 if k[j + (j - i)] == 'G' else 0)
            if charset[0] == 'B':
                ans += B[j + 1]
                ans -= (1 if k[j + (j - i)] == 'B' else 0)

print(ans)

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

score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1

print(score)