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()
afters={'R':0, 'B':0, 'G':0}
for i in range(n):
    afters[s[i]] += 1

befores = {'R':0, 'B':0, 'G':0}
befores[s[0]]+=1
afters[s[0]]-=1

ans=0
for i in range(1,n-1):
    head=s[0:i]
    last=s[i+1::]
    afters[s[i]]-=1
    if s[i]=='R':
        ans += befores['G'] * afters['B'] + befores['B'] * afters['G'] 
        bad =[['G','B'],['B','G']]
    elif s[i]=='B':
        ans += befores['G'] * afters['R'] + befores['R'] * afters['G'] 
        bad = [['G','R'],['R','G']]
    else:
        ans += befores['B'] * afters['R'] + befores['R'] * afters['B'] 
        bad = [['B','R'],['R','B']]
    for j in range(min(i-0, n-i-1)):
        if [s[i-j-1], s[i+j+1]] in bad:
            ans -= 1
    befores[s[i]]+=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)
        
ans = len(R) * len(G) * len(B)
for i in R:
    for j in G:
        if abs(j-i) % 2 == 1:
            k1 = min(j, i) - abs(j-i)
            k2 = max(j, i) + abs(j-i)
            if k1 >= 0:
                if S[k1] == "B":
                    ans -= 1
            if k2 <= N-1:
                if S[k2] == "B":
                    ans -= 1
        else:
            k1 = min(j, i) - abs(j-i)
            k2 = max(j, i) + abs(j-i)
            k3 = min(j, i) + abs(j-i)//2
            if k1 >= 0:
                if S[k1] == "B":
                    ans -= 1
            if k2 <= N-1:
                if S[k2] == "B":
                    ans -= 1
            if S[k3] == "B":
                ans -= 1
            
print(ans)

Solution (Python):
from itertools import permutations
N = int(input())
S = input()
ans = 0
memo = {
    "R": [0] * (N+1),
    "G": [0] * (N+1),
    "B": [0] * (N+1),
}
for i in range (N-1, -1, -1):
    memo["R"][i] = memo["R"][i+1] + int(S[i] == "R")
    memo["G"][i] = memo["G"][i+1] + int(S[i] == "G")
    memo["B"][i] = memo["B"][i+1] + int(S[i] == "B")
for a, b, c in permutations(['R', 'G', 'B']):
    i = -1
    while i < N-3:
        i += 1
        if S[i] != a: continue
        j = i
        while j < N-2:
            j += 1
            if S[j] != b: continue
            ans += memo[c][j]
            if j + (j-i) < N: ans -= int(S[j+(j-i)] == c)
print(ans)

Solution (Python):
from time import time

def solve(N, S):
    answer = 0
    Cs = [[], [], []]
    for i in range(N):
        c = S[i]
        if c == 'R':
            Cs[0].append(i)
        elif c == 'G':
            Cs[1].append(i)
        elif c == 'B':
            Cs[2].append(i)
    answer = len(Cs[0]) * len(Cs[1]) * len(Cs[2])
    for i in range(N):
        for d in range(1, N-i):
            if i + 2*d < N and not(S[i] == S[i+d] or S[i] == S[i+2*d] or S[i+d*2] == S[i+d]):
                answer -= 1
    print(answer)


def main(N, S):
    import sys
    start = time()
    from itertools import combinations
    Cs = [[], [], []]
    for i in range(N):
        c = S[i]
        if c == 'R':
            Cs[0].append(i)
        elif c == 'G':
            Cs[1].append(i)
        elif c == 'B':
            Cs[2].append(i)
    answer = 0
    for C in Cs:
        a = 0
        print(C, len(C))
        for Ci in range(len(C)-1):
            i = C[Ci]
            for Cj in range(Ci+1, len(C)):
                j = C[Cj]
                print("*", i, j)
                print(i)           
                print(j - i - 1)   
                print(N - 1 - j)   
                print(Ci)
                print(Cj - Ci-1)
                print(len(C) - 1 - Cj)
                a += i           
                a += N - 1 - j   
                a += j - i - 1   
                a -= Ci
                a -= Cj - Ci-1
                a -= len(C) - 1 - Cj
        print(S[C[0]], "expect", sum(S[C[0]] in (i, j, k) and (i == j or j == k or i == k) for i,j,k in combinations(S, 3)))
        print(S[C[0]], "actual", a)
        answer += a
    print("expect", sum(i == j or j == k or i == k for i,j,k in combinations(S, 3)))
    print("actual", answer)

    print(answer, time() - start, file=sys.stderr)
    for i in range(N):
        for d in range(1, N-i):
            if i + 2*d < N and not(S[i] == S[i+d] or S[i] == S[i+2*d]):
                answer += 1
    total = N * (N-1)*(N-2) // 6
    print(total, answer, file=sys.stderr)
    print(total - answer)
    print(time() - start, file=sys.stderr)


N = int(input())
S = input()
solve(N, S)

Solution (Python):

from copy import copy
  
def main():
    n = int(input())
    s = input()
    RGB = ['R', 'G', 'B']
    
    rgb_cnt = [0,0,0]
    for i in range(n-1, -1, -1):
        if s[i] == 'R':
            rgb_cnt[0] += 1
        if s[i] == 'G':
            rgb_cnt[1] += 1
        if s[i] == 'B':
            rgb_cnt[2] += 1
            
    sumv = 0
    rgb_cnt_cp = copy(rgb_cnt)
    for i in range(n-2):
        
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb_cnt_cp[rgb_idx] -= 1
        rgb_cnt_cp2 = copy(rgb_cnt_cp)
        
        
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb_idx2 = rgb.index(c2)
            rgb_cnt_cp2[rgb_idx2] -= 1
            
            
            rgb2 = copy(RGB)
            rgb2.pop(rgb2.index(c1))
            rgb2.pop(rgb2.index(c2))
            c3 = rgb2[0]
            
            c3idx = RGB.index(c3)
            
            sumv += rgb_cnt_cp2[c3idx]
            
            
        
        
    for i in range(n-1):
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb.pop(rgb.index(c1))
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb2 = copy(rgb)
            rgb2.pop(rgb.index(c2))
            c3 = rgb2[0]
            
            k = j + (j-i)
            
            if k > n - 1:
                break
            if c3 == s[k]:
                
                sumv -= 1
            
        
    print(sumv)
    
        
  
if __name__ == '__main__':
    main()

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

count = 0

r_idx = set([n for n, v in enumerate(s) if v == 'R'])
g_idx = set([n for n, v in enumerate(s) if v == 'G'])
b_idx = set([n for n, v in enumerate(s) if v == 'B'])



for r in r_idx:
	for g in g_idx:
		out = 0
		if max([r,g]) + abs(g-r) in b_idx:
			out += 1
		if min([r,g]) - abs(g-r) in b_idx:
			out += 1
		if (r+g) / 2 in b_idx:
			out += 1
		count += (len(b_idx) - out)



print(count)

Solution (Python):
n=int(input())
s=input()
col={'R','G','B'}
r,g,b=[0]*n,[0]*n,[0]*n
rc,gc,bc=0,0,0
ans=0
for i in range(n-1,-1,-1):
    if (s[i]=='R'): rc+=1
    elif (s[i]=='G'): gc+=1
    elif (s[i]=='B'): bc+=1
    r[i],g[i],b[i]=rc,gc,bc
for i in range(n):
    for j in range(i+1,n):
        if s[i]!='R' and s[j]!='R' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='R':
                ans+=r[j]-1
            else: ans+=r[j]
        elif s[i]!='G' and s[j]!='G' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='G':
                ans+=g[j]-1
            else: ans+=g[j]
        elif s[i]!='B' and s[j]!='B' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='B':
                ans+=b[j]-1
            else: ans+=b[j]
        
print(ans)

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

s = input()



rs = 0
gs = 0
bs = 0
for x in s:
    if x == 'R':
        rs += 1
    if x == 'G':
        gs += 1
    if x == 'B':
        bs += 1

i = 0
d = 1
count = 0
for i in range(0, n):
    d = 1
    while (i + 2*d < n):
        ok = s[i] != s[i+d] and s[i+d] != s[i+2*d] and s[i] != s[i+2*d]
        if ok:
            count += 1
        d += 1


print(rs * gs * bs - count)

Solution (Python):
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items(): a *= 1 if k==s else v
    b = sum([len(set(s+S[:i+1][::-(j+1)][:3]))//3 for j in range(i//2)])
    ans += a-b
    memo[s] += 1
print(ans)

Solution (Python):
n=int(input())
s=input()
a = [str(c) for c in s]
ans=0
r=0
g=0
b=0
er=0
for i in range(n):
    if a[i]=='R':
        r+=1
        ans=ans+g*b
    elif a[i]=='G':
        g+=1
        ans=ans+r*b
    else:
        b+=1
        ans=ans+g*r
for i in range(n-1):
    for j in range(i+1,n):
        k=j+(j-i)
        if k<n:
            if a[i]!=a[j] and a[j]!=a[k] and a[k]!=a[i]:
                er+=1
print(ans-er)

Solution (Python):




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


acmsum = [[0] * (n+1) for i in range(3)]

for i in range(n):
    if s[i] == 'R':
        acmsum[0][i+1] = acmsum[0][i] + 1
        acmsum[1][i+1] = acmsum[1][i]
        acmsum[2][i+1] = acmsum[2][i]
    elif s[i] == 'G':
        acmsum[0][i+1] = acmsum[0][i]
        acmsum[1][i+1] = acmsum[1][i] + 1
        acmsum[2][i+1] = acmsum[2][i]
    else:
        acmsum[0][i+1] = acmsum[0][i]
        acmsum[1][i+1] = acmsum[1][i]
        acmsum[2][i+1] = acmsum[2][i] + 1

ans = 0
for i in range(1, n-1):
    j = 1
    x = 0
    while(i+j < n and i-j >= 0):
        if s[i-j] != s[i] and s[i-j] != s[i+j] and s[i] != s[i+j]:
            x = x + 1
        j = j + 1
    
    if s[i] == 'R':
        ans = ans + (acmsum[1][i]) * (acmsum[2][n] - acmsum[2][i]) - x
        ans = ans + (acmsum[2][i]) * (acmsum[1][n] - acmsum[1][i])
        
    elif s[i] == 'G':
        ans = ans + (acmsum[0][i]) * (acmsum[2][n] - acmsum[2][i]) - x
        ans = ans + (acmsum[2][i]) * (acmsum[0][n] - acmsum[0][i])
        
    else:
        ans = ans + (acmsum[1][i]) * (acmsum[0][n] - acmsum[0][i]) - x
        ans = ans + (acmsum[0][i]) * (acmsum[1][n] - acmsum[1][i])


print(ans)

Solution (Python):
import sys 
try:
	sys.stdin = open('input.txt', 'r') 
	sys.stdout = open('output.txt', 'w')
except:
	pass
n = int(input())
s = input()
red = []
blue = []
green = []
for i in range(n):
	if s[i] == 'R':
		red.append(i)
	elif s[i] == "G":
		green.append(i)
	else:
		blue.append(i)
temp = [(len(red),'R'), (len(blue),'B'), (len(green),'G')]
temp.sort()

if temp[0][1] == 'R':
	first = list(red)
elif temp[0][1] == 'G':
	first = list(green)
else:
	first = list(blue)

if temp[1][1] == 'R':
	second = list(red)
elif temp[1][1] == 'G':
	second = list(green)
else:
	second = list(blue)

if temp[2][1] == 'R':
	third = list(red)
elif temp[2][1] == 'G':
	third = list(green)
else:
	third = list(blue)

final = []
for i in first:
	for j in second:
		gg = max(i,j)
		ss = min(i,j)
		diff = gg-ss
		check = []
		if (gg+ss)%2==0:
			check.append((gg+ss)//2)
		if gg+diff < n:
			check.append(gg+diff)
		if ss-diff >= 0:
			check.append(ss-diff)
		final.append(check)


ll = len(third)
d_third = {i:1 for i in third}
		
ans = 0

for item in final:
	sub = 0
	for i in item:
		if i in d_third:
			sub += 1
	ans += ll - sub
print(ans)

Solution (Python):
n=int(input())
s=input()
count = 0
i = 0
j = 1
while (i) < n:
    j = 1
    while (i+2*j) < n:
        if s[i] != s[i+j] and s[i+2*j] not in [s[i],s[i+j]]:
            count += 1
        j += 1
    i += 1
r,g,b = 0,0,0
for p in s:
    if p == 'R':
        r += 1
    elif p == 'G':
        g += 1
    else:
        b += 1
print(r*g*b-count)

Solution (Python):
import collections
n=int(input())
s=list(input())
c=collections.Counter(s)
d=c["R"]*c["G"]*c["B"]
ln=n//3
cnt=0
for y in range(1,n+1):
    for x in range(0,n+1):
        if x+2*y>=n:break
        g0=s[x]
        g1=s[x+y]
        g2=s[x+2*y]
        if g0!=g1 and g1!=g2 and g0!=g2:cnt+=1
print(d-cnt)

Solution (Python):
from collections import Counter

n = int(input())
s = input()
c = Counter(s)
cnt = c['R'] * c['G'] * c['B']

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

print(cnt)

Solution (Python):
n=int(input())
s=input()
r=[]
g=[]
b=[]
res=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  elif s[i]=="B":
    b.append(i)

res=len(g)*len(b)*len(r)
r=set(r)
g=set(g)
b=set(b)
for i in r:
  for j in g:
    if (i+j)%2==0 and int((i+j)/2) in b:
      res-=1
    if abs(i-j)+max(i,j) in b:
      res-=1
    if min(i,j)-abs(i-j) in b:
      res-=1
print(res)

Solution (Python):
n = int(input())
s = input()
"""
def f(char, start, end):
    global rgb
    return rgb[char][end] - rgb[char][start]

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

print(rgb['R'])
print(rgb['G'])
print(rgb['B'])

print(f('R', 0,0) )
print(f('R', 0,1) )
print(f('R', 0,2) )
print(f('R', 0,3) )
print(rgb['R'][-1] * rgb['G'][-1] * rgb['B'][-1])
ans = rgb['R'][-1] * rgb['G'][-1] * rgb['B'][-1]
"""
rgb = {'R':0, 'G':0,'B':0}
for i in range(1, n+1):
    j = s[i-1]
    rgb[j] += 1
ans = rgb['R'] * rgb['G'] * rgb['B']
for j in range(1, n):
    for i in range(j):
        if j-i>(n-1)-j: continue
        if s[j]!=s[i] and s[j+(j-i)]!=s[j] and s[j+(j-i)]!=s[i] :
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
string = input()
red = 0
gre = 0
blu = 0
for i in range(n):
    if(string[i] == 'R'):
        red+=1
    elif(string[i] == 'G'):
        gre += 1
    else:
        blu += 1
sum = red * gre * blu
for i in range(n):
    if(i > n - i - 1):
        num = n - i - 1
    else:
        num = i
    for l in range(1,num + 1):
        if(string[i - l] != string[i] and string[i - l] != string[i + l] and string[i + l] != string[i]):
            sum -= 1
print(sum)

Solution (Python):
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)

Solution (Python):
n=int(input())
s=list(input())
from collections import Counter
myc= Counter(s)
val=list(myc.values())
if len(val)!=3:
    print(0)
else:
    ans=val[0]*val[1]*val[2]
    for i in range(n-2):
        for j in range(i+1,n-1):
            if 2*j-i < n:
                if len(set(list((s[i],s[j],s[2*j-i]))))==3:
                    ans-=1
    print(ans)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
count = Counter(S)


ans = 0
R = count["R"]
G = count["G"]
B = count["B"]
ans = R * G * B










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

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

ans = 0
jyogai = 0

for i in range(N-1):
    nS = S[i+1:]
    kouho = []
    for w in ["R", "G", "B"]:
        if S[i] != w:
            kouho.append(nS.count(w))
    temp = i
    while True:
        temp += 1
        temp2 = temp - i
        if i + 2*temp2 > N-1:
            break
        b = [S[i], S[i + temp2], S[i + 2*temp2]]
        if len(set(b)) == 3:
            jyogai += 1

    ans += kouho[0] * kouho[1]

print(ans - jyogai)

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

def b_search(A,key):
    left,right,diff = 0,len(A),0
    output = False
    count = 0
    while left < right and count < 15:
        mid = (left+right)//2
        if A[mid] == key:
            return True
        elif A[mid] < key:
            left = mid
        else:
            right = mid
        count += 1
        

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

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

if len(R) <= len(G) and len(B) <= len(G):
    G,B = B,G
elif len(G) <= len(R) and len(B) <= len(R):
    R,B = B,R


ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        tmp = 0
        if R[i] < G[j]:
            
            if b_search(B,R[i]-(G[j]-R[i])):
                tmp += 1
            if (G[j]-R[i]) % 2 == 0 and b_search(B,(G[j]+R[i])//2):
                tmp += 1
            if b_search(B,G[j]+(G[j]-R[i])):
                tmp += 1
            ans += len(B)-tmp
        else:
            
            if b_search(B,G[j]-(R[i]-G[j])):
                tmp += 1
            if (R[i]-G[j]) % 2 == 0 and b_search(B,(R[i]+G[j])//2):
                tmp += 1
            if b_search(B,R[i]+(R[i]-G[j])):
                tmp += 1
            ans += len(B)-tmp

print(ans)

Solution (Python):
import math

n = int(input())
s = input()
d = 0
for i in range(1, math.ceil(n / 2)):
    for j in range(i, n - i):
        if s[j - i] != s[j] and s[j] != s[j + i] and s[j - i] != s[j + i]:
            d += 1
print(s.count('R') * s.count('G') * s.count('B') - d)

Solution (Python):
N = int(input())
S = input()
rn = [0 for _ in range(N)]
gn = [0 for _ in range(N)]
bn = [0 for _ in range(N)]
rgn = [0 for _ in range(N)]
gbn = [0 for _ in range(N)]
brn = [0 for _ in range(N)]
for i, s in enumerate(S):
    if i == 0:
        if s == "R":
            rn[i] = 1
            rgn[i] = 1
            brn[i] = 1
        if s == "G":
            gn[i] = 1
            rgn[i] = 1
            gbn[i] = 1
        if s == "B":
            bn[i] = 1
            gbn[i] = 1
            brn[i] = 1
    else:
        if s == "R":
            rn[i] = rn[i-1] + 1
            gn[i] = gn[i-1]
            bn[i] = bn[i-1]
            rgn[i] = rgn[i-1] + 1
            gbn[i] = gbn[i-1]
            brn[i] = brn[i-1] + 1
        if s == "G":
            rn[i] = rn[i-1]
            gn[i] = gn[i-1] + 1
            bn[i] = bn[i-1]
            rgn[i] = rgn[i-1] + 1
            gbn[i] = gbn[i-1] + 1    
            brn[i] = brn[i-1]       
        if s == "B":
            rn[i] = rn[i-1]
            gn[i] = gn[i-1]
            bn[i] = bn[i-1] + 1
            rgn[i] = rgn[i-1]
            gbn[i] = gbn[i-1] + 1
            brn[i] = brn[i-1] + 1
ans = 0
for i, s in enumerate(S):
    if i == 0 or i == N-1:
        continue
    if s == "R":
        ans += gn[i-1] * (bn[-1] - bn[i])
        ans += bn[i-1] * (gn[-1] - gn[i])
    if s == "G":
        ans += rn[i-1] * (bn[-1] - bn[i])
        ans += bn[i-1] * (rn[-1] - rn[i])
    if s == "B":
        ans += rn[i-1] * (gn[-1] - gn[i])
        ans += gn[i-1] * (rn[-1] - rn[i])
    for j in range(min(i, N - i - 1)):
        if S[i-j-1] != S[i+j+1] and S[i-j-1] != s and S[i+j+1] != s:
            ans -= 1
print(ans)

Solution (Python):
N=int(input())
S=list(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=G*B*R
for a in range(N-1):
	for d in range(1,(N-a-1)//2+1):
		if S[a]!=S[a+d] and S[a+d]!=S[a+d*2] and S[a]!=S[a+d*2]:
			ans-=1
print(ans)

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq










N = int(input())
S = str(input())
dp = [[0 for j in range(3)] for i in range(N)]
ans = 0
temp = temp2 = temp3 = 0

RGB = [["R", "G"], ["R", "B"], ["B", "R"], ["B", "G"], ["G", "R"], ["G", "B"]]


for j in range(6):
  for i in range(N):
    if(S[i]== RGB[j][0]):
      temp += 1
    elif(S[i]==RGB[j][1]):
      temp2 += temp
    else:
      temp3 += temp2
  ans += temp3
  temp = temp2 = temp3 = 0

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


print(ans)

Solution (Python):
def main():
    n = int(input())
    S = input()
    r_inds = []
    g_inds = []
    b_inds = []
    for i in range(len(S)):
        if S[i] == "R":
            r_inds.append(i)
        elif S[i] == "G":
            g_inds.append(i)
        else:
            b_inds.append(i)

    total_comb = len(r_inds)*len(g_inds)*len(b_inds)
    ded = 0
    for i in r_inds:
        for j in g_inds:
            s_ind = min(i, j)
            b_ind = max(i, j)
            diff = b_ind - s_ind
            bigger_ind = b_ind + diff
            smaller_ind = s_ind - diff
            if bigger_ind < len(S) and bigger_ind in b_inds:
                ded += 1
            if smaller_ind >= 0 and smaller_ind in b_inds:
                ded += 1
            if diff % 2 == 0:
                mid_diff = diff // 2
                mid_ind = s_ind + mid_diff
                if mid_ind in b_inds:
                    ded += 1
    print(total_comb - ded)

            
if __name__ == "__main__":
    main()

Solution (Python):

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

r = s.count("R")
g = s.count("G")
ans += r*g*(N-r-g)
print(ans)

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

kv = {'R':[],'G':[],'B':[]}
for i in range(N):
    kv[S[i]].append(i)

a = sorted([[len(kv['R']),'R'],[len(kv['G']),'G'],[len(kv['B']),'B']], key=lambda x:-x[0])
a1, a2, a3 = a[0][1],a[1][1],a[2][1]

ans = a[0][0]*a[1][0]*a[2][0]
set_a3 = set(kv[a3])
for one in kv[a1]:
    for t in kv[a2]:
        ans-=3-len(set([2*t-one,2*one-t,(t+one)/2])-set_a3)
print(ans)

Solution (Python):
N = int(input())
S = input()
sum = 0
for i in range(N):
  if S[i] == "R":
    sum += S[i+1:].count("G")*S[i+1:].count("B")
  elif S[i] == "G":
    sum += S[i+1:].count("R")*S[i+1:].count("B")
  elif S[i] == "B":
    sum += S[i+1:].count("G")*S[i+1:].count("R")
for a in range(N):
  for b in range(a+1, N):
    if S[a] == S[b] or 2*b-a >= N:
      pass
    elif S[b] != S[2*b-a] and S[2*b-a] != S[a]:
      sum -= 1
print(sum)

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

Solution (Python):
N = int(input())
S = input()
r,g,b = 0,0,0
for i in S:
    if i == 'R':
        r+=1
    if i == 'G':
        g+=1       
    if i == 'B':
        b+=1 

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

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

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

    ar = []
    ab = []
    ag = []
    for i, si in enumerate(S):
        if si == "R":
            ar.append(i)
        elif si == "G":
            ag.append(i)
        else:
            ab.append(i)

    aa = [(ar, "R"), (ab, "B"), (ag, "G")]
    aa.sort(key=lambda x: len(x[0]))
    ans = 0
    ck = aa[2][1]
    ans = len(ar)*len(ab)*len(ag)
    for si in aa[0][0]:
        for sj in aa[1][0]:
            for k in [2*si-sj, 2*sj-si]:
                if 0 <= k < N and S[k] == ck:
                    ans -= 1
            k = si + sj
            if k & 1:
                continue
            k //=2
            if k < N and S[k] == ck:
                ans -= 1
    print(ans)





if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
S = input()
moji = {'R', 'G', 'B'}

count = 0
for j in range(1, N-1):
    sj = S[j]
    diff_m = moji - {sj}
    rem_moji = list(diff_m)
    for mi, mj in zip(rem_moji, rem_moji[::-1]):
        ci = S.count(mi, 0, j)
        ck = S.count(mj, j+1, N)
        count += ci * ck

    l = min([j+1, N - j])
    for d in range(1, l):
        si = S[j-d]
        sk = S[j+d]
        if si != sj and sj != sk and sk != si:
            count -= 1

print(count)

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

def main():
	n = int(input())
	s = tuple(input())

	ans = 0
	r = []
	g = []
	b = []
	for i in range(3):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

	for i in range(3,n):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

		for j in range(i-2):
			if (j-i) % 2 == 0:
				if s[i] == s[j]:
					continue
				elif 'R' not in [s[i],s[j]]:
					ans += bisect.bisect_left(r,i) - bisect.bisect_left(r,j)
					index = bisect.bisect_left(r,j + (i-j)//2 )
					if index < len(r):
						if r[index] == j + (i-j)//2:
							ans -= 1
				elif 'G' not in [s[i],s[j]]:
					ans += bisect.bisect_left(g,i) - bisect.bisect_left(g,j)
					index = bisect.bisect_left(g,j + (i-j)//2 )
					if index < len(g):
						if g[index] == j + (i-j)//2:
							ans -= 1
				elif 'B' not in [s[i],s[j]]:
					ans += bisect.bisect_left(b,i) - bisect.bisect_left(b,j)
					index = bisect.bisect_left(b,j + (i-j)//2 )
					if index < len(b):
						if b[index] == j + (i-j)//2:
							ans -= 1
			else:
				if s[i] == s[j]:
					continue
				elif 'R' not in [s[i],s[j]]:
					ans += bisect.bisect_left(r,i) - bisect.bisect_left(r,j)

				elif 'G' not in [s[i],s[j]]:
					ans += bisect.bisect_left(g,i) - bisect.bisect_left(g,j)

				elif 'B' not in [s[i],s[j]]:
					ans += bisect.bisect_left(b,i) - bisect.bisect_left(b,j)


	print(ans)

if __name__ == '__main__':
	main()

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

ret = 0
for i in range(1,N):
    mid = S[i]
    rest = list(set(RGB)-set([mid]))
    left0, right0 = S[:i].count(rest[0]), S[i+1:].count(rest[0])
    left1, right1 = S[:i].count(rest[1]), S[i+1:].count(rest[1])
    tmp = left0*right1 + left1*right0
    for j in range(min(i,N-i-1)):
        ss = [S[i-j-1],S[i+j+1]]
        if ss == rest or ss == rest[::-1]:
            tmp -= 1

    ret += tmp
print(ret)

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

R = [0]*n 
G = [0]*n
B = [0]*n


for i in range(n-2, -1, -1):
    if s[i+1] == 'R':
        R[i] = R[i+1] + 1
    else:
        R[i] = R[i+1]

for i in range(n-2, -1, -1):
    if s[i+1] == 'G':
        G[i] = G[i+1] + 1
    else:
        G[i] = G[i+1]

for i in range(n-2, -1, -1):
    if s[i+1] == 'B':
        B[i] = B[i+1] + 1
    else:
        B[i] = B[i+1]

cnt = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] != s[j]:
            Set = set((s[i], s[j]))
            if Set == set(('R', 'G')):
                cnt += B[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'B':
                        cnt -= 1
            elif Set == set(('G', 'B')):
                cnt += R[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'R':
                        cnt -= 1
            else:
                cnt += G[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'G':
                        cnt -= 1
print(cnt)

Solution (Python):
import bisect
import itertools

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

RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
RGB.sort(key=lambda x:len(x))
len_B=len(RGB[2])
max_B=max(RGB[2])
min_B=min(RGB[2])


bool_B=[False for i in range(n)]
for i in RGB[2]:
    bool_B[i]=True

for rg in itertools.product(RGB[0],RGB[1]):
    r=rg[0]
    g=rg[1]
    dif = abs(r - g)

    up = max(r,g)+dif
    down = min(r,g)-dif

    if max_B >=up:
        if bool_B[up]:
            total-=1
    if min_B <= down and max_B >= down:
        if bool_B[down]:
            total-=1
    if dif % 2 == 0:
        center = (r+g)//2
        if min_B<=center and max_B >= center:
            if bool_B[center]:
                total-=1
print(total)

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

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

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

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

s=input()

s=list(s)

Rcheck=[0]*n

Bcheck=[0]*n

Gcheck=[0]*n

for i in range(n):
    if s[i] == 'R':
        Rcheck[i] += 1
    if s[i] == 'B':
        Bcheck[i] += 1
    if s[i] == 'G':
        Gcheck[i] += 1

for i in range(1,n):
    Rcheck[i] += Rcheck[i-1]
    Bcheck[i] += Bcheck[i-1]
    Gcheck[i] += Gcheck[i-1]

ans = 0

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

        if 'R' not in memo:
            ans += Rcheck[-1] - Rcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'R':
                ans -= 1
        elif 'B' not in memo:
            ans += Bcheck[-1] - Bcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'B':
                ans -= 1
        elif 'G' not in memo:
            ans += Gcheck[-1] - Gcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'G':
                ans -= 1

print(ans)

Solution (Python):

n = int(input())
s = input()
rs, gs, bs = [0] * n, [0] * n, [0] * n
if s[0] == "R":
    rs[0] = 1
elif s[0] == "G":
    gs[0] = 1
elif s[0] == "B":
    bs[0] = 1
for i in range(1, n):
    rs[i] = 1 + rs[i - 1] if s[i] == "R" else rs[i - 1]
    gs[i] = 1 + gs[i - 1] if s[i] == "G" else gs[i - 1]
    bs[i] = 1 + bs[i - 1] if s[i] == "B" else bs[i - 1]
ans = 0
for i in range(0, n):
    for j in range(i + 2, n):
        if (s[i] == "R" and s[j] == "B") or (s[i] == "B" and s[j] == "R"):
            ans += gs[j]
            ans -= gs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "G":
                ans -= 1
        elif (s[i] == "R" and s[j] == "G") or (s[i] == "G" and s[j] == "R"):
            ans += bs[j]
            ans -= bs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "B":
                ans -= 1
        elif (s[i] == "B" and s[j] == "G") or (s[i] == "G" and s[j] == "B"):
            ans += rs[j]
            ans -= rs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "R":
                ans -= 1
print(ans)