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):
from sys import stdin


def main():
    n = int(stdin.readline())
    s = stdin.readline()[:-1]

    nr = 0
    ng = 0
    nb = 0
    nn = 0

    for i in range(n):
        if s[i] == 'R':
            nr += 1
        elif s[i] == 'G':
            ng += 1
        elif s[i] == 'B':
            nb += 1
        for j in range(i + 1, n):
            k = j + j - i
            if 0 <= k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                nn += 1

    print(nr * ng * nb - nn)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import os

def file_input():
    f = open('Beginner_Contest_162/input.txt', 'r')
    sys.stdin = f

def main():
    
    N=int(input())
    S=input()
    
    R=S.count('R')
    G=S.count('G')
    B=S.count('B')

    sum=R*G*B
    
    for i in range(N-2):
        
        for j in range(i+1,-(-(N+i)//2)):
            k=2*j-i
            
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                sum-=1
                


    print(sum)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=list(input())
import collections
c=collections.Counter(S)
if len(c)==3:
  
  u=1
  for i in c.values():
    u=u*i 
  for i in range(N-2):
    for j in range(i+1,int((N-1+i)/2)+1):
      
        if set([S[i],S[j],S[2*j-i]])=={'B','R','G'} :
          u=u-1
  print(u)       
else:
  print(0)

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

v=0
rgb=[0,0,0]
for x in range(n):
  if s[x]=="R":
    rgb[0]+=1
  elif s[x]=="G":
    rgb[1]+=1
  else:
    rgb[2]+=1
  
base=rgb[0]*rgb[1]*rgb[2]
  
for a in range(n):
  for b in range(a+1,n):
    c=2*b-a
    if c>=n:
      break
    else:
      if s[a]!=s[b] and s[a]!=s[c] and s[b]!=s[c] and b*2==c+a:
        base-=1
          
print(base)

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

cnt=0
def excludeRGB(c1,c2,c3):
  cnt=0
  for a in range(len(s)-2):
    if s[a] != c1:
      continue
    for b in range(1,len(s)):
      if a+b*2 >= n or a+b >= n:
        break
      if s[a+b] == c2 and s[a+b*2] == c3:
        cnt+=1
  return cnt

ans=0
def rgb(c1,c2,c3):
  ans,c1num,c2num=0,0,0
  for i in s[::-1]:
    if i == c1:
      c1num+=1
    elif i == c2:
      c2num+=c1num
    elif i == c3:
      ans+=c2num
  return ans
cnt+=excludeRGB("R","G","B")
cnt+=excludeRGB("R","B","G")
cnt+=excludeRGB("B","G","R")
cnt+=excludeRGB("B","R","G")
cnt+=excludeRGB("G","B","R")
cnt+=excludeRGB("G","R","B")

ans+=rgb("R","G","B")
ans+=rgb("R","B","G")
ans+=rgb("B","G","R")
ans+=rgb("B","R","G")
ans+=rgb("G","B","R")
ans+=rgb("G","R","B")
print(ans-cnt)

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

r = [0]*(n+1)
r2 = []
g = [0]*(n+1)
g2 = []
b = [0]*(n+1)
b2 = []

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

ans = len(r2) * len(g2) * len(b2)
minC = r2
minC2 = g2
maxC = b

for i in minC:
  for j in minC2:
    id = max(i,j) + abs(i-j)
    if id <= n and maxC[id] == 1:
      ans -= 1
    id = min(i,j) - abs(i-j)
    if id >= 1 and maxC[id] == 1:
      ans -= 1
    id = i+j
    if id%2 == 0 and maxC[id//2] == 1:
      ans -= 1

print(ans)

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




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

count=0

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

print(r*g*b-count)

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

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

ans = R * G * B

for i in range(n):
    for d in range(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 = S.count('R')
G = S.count('G')
B = S.count('B')

ans = R * G * B

for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

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

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

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

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

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

one = r*g*b
not_two = 0

for i in range(0,n-1,1):
    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[i] != s[k]:
            not_two += 1

ans = one - not_two
print(str(ans))

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

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    s = [ord(c) for c in S()]
    c = collections.defaultdict(int)
    r = 0
    c[s[0]] += 1
    m = sum(ord(c) for c in "RGB")
    for j in range(1,n):
        t = s[j]
        for k in range(j+1,n):
            u = s[k]
            if t == u:
                continue
            r += c[m-t-u]
            i = j - (k-j)
            if i >= 0 and s[i] == m-t-u:
                r -= 1

        c[t] += 1

    return r


print(main())

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

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

def counting(x):
  count = 0
  for i in range(1, math.ceil(x/2)):
    if s[x-1]!=s[x-1-i] and s[x-1-i]!=s[x-1-2*i] and s[x-1-2*i]!=s[x-1]:
      count += 1
    else:
      count += 0
  return count

if n>=3:
  minus = [0]*(n+1)
  if s[0]!=s[1] and s[1]!=s[2] and s[2]!=s[0]:
    minus [3] = 1
  else:
    minus [3] = 0
  for i in range(3,n):
    minus[i+1] = minus[i]+counting(i+1)
  print(int(upper-minus[n]))
else:
  print(int(0))

Solution (Python):
N = int(input())
S = str(input())
S = list(S)
R = S.count("R")
G = S.count("G")
B = S.count("B")
count = R*G*B
double = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j-i
        if k < N:
            s = set([S[i],S[j],S[k]])
            if len(s) == 3:
                count -= 1
print(count)

Solution (Python):
import itertools
import bisect

def main():
    N = int(input())
    S = input()
    d = {'R': [], 'G': [], 'B': []}
    for i, c in enumerate(S):
        d[c].append(i)
    
    
    cnt = 0
    for xx, yy, zz in itertools.permutations(('R', 'G', 'B'), 3):
        x, y, z = d[xx], d[yy], d[zz]
        for i in x:
            y_start = bisect.bisect_left(y, i)
            for jj in range(y_start, len(y)):
                j = y[jj]
                assert i < j
                z_start = bisect.bisect_left(z, j)
                z_point = bisect.bisect_left(z, 2*j-i)
                
                if (len(z) <= z_point) or (z[z_point] - j != j - i):
                    cnt += len(z) - z_start
                else:
                    cnt += len(z) - z_start - 1
                
                
                
                
                
                
                
                
                
                
                
                
                
                
    print(cnt)
    
    

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
r_num = s.count('R')
g_num = s.count('G')
b_num = s.count('B')
tot = r_num*g_num*b_num
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[i]!=s[k] and s[j] != s[k]:
                tot -= 1

print(tot)

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

X = set()
Y = set()
Z = set()


for index in range(len(S)):
    if S[index] == 'R':
        X.add(index)
    elif S[index] == 'G':
        Y.add(index)
    elif S[index] == 'B':
        Z.add(index)

all_combination_num = len(X) * len(Y) * len(Z)
exclude_num = 0

rgbcombination = list(itertools.permutations('RGB', 3))
for i in range(1, N):
    for j in range(N - i * 2):
        if (S[j], S[i + j], S[j + i * 2]) in rgbcombination:
            exclude_num += 1

print(all_combination_num - exclude_num)

Solution (Python):
n = int(input())
s = input()
d = [0,0,0]
for i in s:
    if(i == 'R'):d[0] += 1
    elif(i == 'G'):d[1] += 1
    elif(i == 'B'):d[2] += 1
c = d[0] * d[1] * d[2]

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]):continue
        if(s[j] == s[k]):continue
        if(s[i] == s[k]):continue
        c -= 1

print(c)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n = II()
    S = list(input())

    R = []
    G = []
    B = []
    for idx, s in enumerate(S):
        if s == 'R':
            R.append(idx)
        elif s == 'G':
            G.append(idx)
        else:
            B.append(idx)
    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__':
    solve()

Solution (Python):
import collections

N = int(input())
S = input()
tonum = {"R": 0, "G": 1, "B": 2}
ans = 0
for i in range(1, N - 1):
    d = tonum[S[i]]
    c2 = collections.Counter()
    for j in range(i + 1, N):
        c2[tonum[S[j]]] += 1
    for j in range(i):
        dj = tonum[S[j]]
        if dj == d:
            continue
        dk = 3 - d - dj
        ans += c2[dk]
        kk = i + (i - j)
        if kk < N and tonum[S[kk]] == dk:
            ans -= 1
print(ans)

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


import math
count=0
counter={'R':0,'G':0,'B':0}
for i in S:
    counter[i]+=1
s=counter['R']*counter['G']*counter['B']
for i in range(1,N-1):
    for j in range(i+1,N):
        k=2*j-i
        if k<=N and (S[i-1]!=S[j-1] and S[i-1]!=S[k-1] and S[j-1]!=S[k-1]):
                count+=1
print(s-count)

Solution (Python):
"""
j−i != k−j
の条件がなければ、Rの数*Gの数*Bの数(*1)が答え

そこでまず*1を求めて、次に
j-i == k-j
となるような場合の数を求め、*1から引いたものが答え
"""

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n = int(readline())
s = readline()

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

cnt = [0]*3
for i in range(n):
    cnt[a[i]] += 1
    
ans = 1
for i in range(3):
    ans *= cnt[i]

for j in range(n):
    for i in range(j):
        k = j + (j-i)
        if k < n:
            if a[i] == a[j]: continue
            if a[i] == a[k]: continue
            if a[k] == a[j]: continue
            ans -= 1

print(ans)

Solution (Python):
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for i in range(N):
	for j in range(i+1,N):
		k = 2*j-i
		if k>=N:
			continue
		if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
			s+=1

ans=c-s
print(ans)

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

m = {'R': 0, 'G': 0, 'B': 0}

for i, c in enumerate(s):
  if c == 'R':
    m['R'] += 1
  elif c == 'G':
    m['G'] += 1
  else:
    m['B'] += 1

result = m['R'] * m['G'] * m['B']

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

    if k > n or not (s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]):
      continue
    result -= 1

print(result)

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

count=0
rgb={'R':0, 'G':0, 'B':0}
for i in range(n):
  rgb[s[i]]+=1

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

if n<=3:
  print(0)
  exit()

for sa in range(1,(n-1)//2+1):
  for i in range(n-2*sa):
    j=i+sa
    k=j+sa
    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()

total = s.count("R") * s.count("G") * s.count("B")
for i in range(n-2):
    for j in range(i+1, n-1):
        if j * 2 - i >= n:
            continue
        k = j * 2 - i
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            total -= 1

print(total)

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

Solution (Python):
N = int(input())
S = list(input())
ans = S.count('R')*S.count('G')*S.count('B') 
for i in range(N): 
   if S[i]=='R':
       S[i] =1
   elif S[i]=='G':
       S[i] =2
   elif S[i]=='B':
       S[i] =4
for i in range(N): 
   for j in range(i+1,N):
     k=2*j-i
     if k>=N : 
       continue  
     if S[i]+S[j]+S[k]==7 : 
         ans-=1 
print(ans)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
s = str(stdin.readline().rstrip())




rnum = s.count("R")
gnum = s.count("G")
bnum = s.count("B")

ans = rnum * gnum * bnum

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[j]!=s[k] and s[i]!=s[k]:
                ans -= 1

print(ans)

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

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

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

Solution (Python):
n=int(input())
s=input()
list(s)
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b

for j in range(n):
    m=min(j,n-j-1)
    if m==0:
        continue
    for ik in range(m):
        ii=s[j-ik-1]
        jj=s[j]
        kk=s[j+ik+1]
        if (ii!=jj)&(jj!=kk)&(kk!=ii):ans-=1
print(ans)

Solution (Python):


N = int(input())
S = input()
cnt_R = S.count('R')
cnt_G = S.count('G')
cnt_B = S.count('B')
total = cnt_R * cnt_G * cnt_B

expt = 0
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[i] != S[k]):
            expt += 1

print(total - expt)

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

for i in range(N-1):
  for j in range(i,N):
    if 0 <= 2*i-j < N:
      if S[2*i-j] != S[i] != S[j] != S[2*i-j]:
        all -= 1
print(all)

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

for i in range(m-2):
  
  for j in range(i+1,int((m+i-1)/2)+1):
    k=2*j-i
    
    if N[k] != N[j] and N[j] != N[i] and N[i] != N[k]:
      
      cnt-=1
print(cnt)

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

for i in range(m-2):
  
  for j in range(i+1,int((m+i-1)/2)+1):
    k=2*j-i
    
    if N[k] != N[j] and N[j] != N[i] and N[i] != N[k]:
      
      cnt-=1
print(cnt)

Solution (Python):
from collections import Counter
from copy import copy

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

total_cnt = dict(Counter(s))
if len(total_cnt) < 3:
    print(0)
    exit()

cnt = [total_cnt]
for i, c in enumerate(s):
    curr_cnt = copy(cnt[-1])
    curr_cnt[c] -= 1
    cnt.append(curr_cnt)

res = 0
for i, c1 in enumerate(s):
    for j in range(i + 1, n - 1):
        c2 = s[j]
        if c2 == c1:
            continue
        for c3 in "RGB":
            if c3 == c1 or c3 == c2:
                continue
            res += cnt[j + 1][c3]
            k = 2 * j - i
            if k >= n:
                continue
            if s[k] == c3:
                res -= 1
print(res)

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

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

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 N > k and 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=0
for i in s:
  if i=='R':
    r-=1
    ans+=g*b
  elif i=='G':
    g-=1
    ans+=r*b
  elif i=='B':
    b-=1
    ans+=g*r
for i in range(n-2):
  for j in range(i+2,n):
    if (i+j)%2!=0:
      pass
    else:
      k=(i+j)//2
      if (s[i]!=s[j])and(s[i]!=s[k])and(s[j]!=s[k]):
        ans-=1
      else:
        pass
print(ans)

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

cnts = [0,0,0]
for c in S:
	if c == 'R':
		cnts[0] = cnts[0] + 1 
	if c == 'G':
		cnts[1] = cnts[1] + 1 
	if c == 'B':
		cnts[2] = cnts[2] + 1 

ans = cnts[0] * cnts[1] * cnts[2]

for j in range(1,N-1):
	for i in range(0,j):
		k = j + j - i
		if k >= N:
			pass
		elif S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
			ans = ans - 1

print(ans)

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

    ans = 1
    for c in 'RGB':
        ans *= s.count(c)

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

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

c1 = R*G*B

c1_2bar = 0

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

print(c1 - c1_2bar)

Solution (Python):
N=int(input())
S=input()
ans=0
RGB_dict={'R':0,'G':0,'B':0}
for s in S:
  RGB_dict[s]+=1
ans=RGB_dict['R']*RGB_dict['G']*RGB_dict['B']

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

Solution (Python):
from collections import defaultdict


def main():
    N = int(input())
    S = input()
    if N < 4:
        print(0)
        exit(0)
    rgb_cnt = defaultdict(int)
    for c in S:
        rgb_cnt[c] += 1
    
    
    cnt = 0
    for center in range(N):
        cc = S[center]
        for w in range(1, N + 1):
            left = center - w
            right = center + w
            if left < 0 or right > N - 1:
                continue
            cl = S[left]
            cr = S[right]
            if cl != cc and cc != cr and cr != cl:
                cnt += 1
    answer = rgb_cnt['R'] * rgb_cnt['G'] * rgb_cnt['B'] - cnt
    print(answer)


if __name__ == '__main__':
    main()

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(1,(N-1)//2 +1):
    for j in zip(S,S[i:],S[2*i:]):
        if len(set(j))==3:
            ans-=1
print(ans)

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

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

result = r * g * b
count = 0

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

print(result-count)

Solution (Python):
import math
import collections

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

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

    count = 0
    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]):
                    count += 1

    print(c - count)

    return 0

if __name__ == '__main__':
    main()

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

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

for i in range(n):
    for j in range(i+1,n):
        if s[i] != s[j]:
            k = j + (j - i)
            if k >= n or j >= n:
                break
            if s[i] != s[j] != s[k] != s[i]:
                
                ans -= 1

print(ans)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

from collections import defaultdict
d = defaultdict(int)

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

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 or j >= n:
            break
        if s[i] != s[j] != s[k] != s[i]:
                ans -= 1

print(ans)

Solution (Python):
import statistics as st
n = int(input())
s = input()
NGs = 0
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)
for i in range(n-2):
  for d in range((n-i-1)//2+1):
    j = i+d
    k = j+d
    if i in r and j in g and k in b:
      NGs += 1
    elif i in r and j in b and k in g:
      NGs += 1
    elif i in g and j in r and k in b:
      NGs += 1
    elif i in g and j in b and k in r:
      NGs += 1
    elif i in b and j in r and k in g:
      NGs += 1
    elif i in b and j in g and k in r:
      NGs += 1
print(len(r)*len(g)*len(b)-NGs)

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

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

ans = 0


all_1 = r * g * b


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

s = all_1 - ans

print(s)

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

ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
    for j in range(i+1, n):
        k = j + (j - i)
        if k < n:
            if s[i] != s[j]:
                if s[i] != s[k]:
                    if s[j] != s[k]:
                        ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = [i for i in input()]

r = 0
g = 0
b = 0
no = 0

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

for i in range(N):
    for j in range(i+1, N):
        k = j*2 - i
        if k >= N:
            break
        elif S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            no += 1
 
print(r*g*b - no)

Solution (Python):
from collections import Counter
N = int(input())
S = [i for i in input()]
count = 0
s = Counter(S)
for i in range(N):
    for j in range(i,N):
        if (i+j)%2==0 and S[(i+j)//2]!=S[i] and S[i]!=S[j] and S[(i+j)//2]!=S[j]:
            count += 1

print((s['R'] * s['B'] * s['G'])-count)