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()
X=[[0]*(N+1) for i in range(3)]
d={'R':0,'G':1,'B':2}
for i,s in enumerate(S):
  X[d[s]][i]+=1
  X[0][i+1]=X[0][i]
  X[1][i+1]=X[1][i]
  X[2][i+1]=X[2][i]
ans=0
for i,s in enumerate(S):
  _=1
  for j in range(3):
    if d[s]==j:
      continue
    _*=X[j][-1]-X[j][i]
  ans+=_
for i in range(1,(N+1)//2):
  j=0
  while j+2*i<N:
    if S[j]!=S[j+i] and S[j]!=S[j+2*i] and S[j+i]!=S[j+2*i]:
      ans-=1
    j+=1
print(ans)

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

Solution (Python):


from collections import defaultdict


def main():
    n = int(input().strip())
    s = input().strip()

    pos = defaultdict(set)
    for i, c in enumerate(s):
        pos[c].add(i)

    
    base = len(pos["R"]) * len(pos["G"]) * len(pos["B"])

    
    sk = sorted(["R", "G", "B"], key=lambda k: len(pos[k]))
    for p1 in pos[sk[0]]:
        for p2 in pos[sk[1]]:
            p_min, p_max = (p1, p2) if p1 < p2 else (p2, p1)
            if p_min - (p_max - p_min) in pos[sk[2]]:
                base -= 1
            if p_max + (p_max - p_min) in pos[sk[2]]:
                base -= 1
            if (p_max + p_min) % 2 == 0 and (p_max + p_min) // 2 in pos[sk[2]]:
                base -= 1

    print(base)


if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

c = collections.Counter(s)

ans_count = c['R'] * c['G'] * c['B']

for i in range(1, (n+1)//2):
    for j in range(n):
        if j+(2*i) >= n:
            break

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


print(ans_count)

Solution (Python):
import math

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

minuscount=0
for i in range(n-2):
    a=i+1
    for j in range(i+1,math.floor(n/2 + a/2)):
        b=j+1
        a1=s[i]
        a2=s[j]
        a3=s[2*b-a-1]
        if a1!=a2 and a2!=a3 and a1!=a3:
            minuscount+=1

rcount=s.count("R")
gcount=s.count("G")
bcount=s.count("B")

print(rcount*gcount*bcount-minuscount)

Solution (Python):
N = int(input())
S = input()
r = 0
g = 0
b = 0

for c in S:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1

ans = r * g * b

cnt = 0
for d in range(1, (N - 3) // 2 + 1 + 1):
    i = 0
    while i + 2 * d < N:
        if not (S[i] == S[i + d]) and  not (S[i + d] == S[i + 2*d]  ) and not (S[i + 2 * d] == S[i]):
            cnt += 1
        i += 1

ans -= cnt

print(ans)

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

nr = S.count("R")
ng = S.count("G")
nb = S.count("B")

exp = 0
for i in range(N-2):
    for d in range(1,(N-1-i)//2+1):
        if S[i] !=S[i+d] and S[i+d] != S[i+2*d] and S[i] != S[i+2*d]:
            exp = exp +1

print(nr*ng*nb-exp)

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

nowR = s.count("R")
nowG = s.count("G")
nowB = s.count("B")

ans = 0
for i in range(n-1):
    R = nowR
    G = nowG
    B = nowB
    now = s[i]
    if now == "R":
        R -= 1
    elif now == "G":
        G -= 1
    else:
        B -= 1
    nowR = R
    nowG = G
    nowB = B
    for j in range(i+1,n):
        now2 = s[j]
        if now == "R":
            if now2 == "G":
                G -= 1
                ans += B
                if 2*j-i < n and s[2*j-i] == "B":
                    ans -= 1
            elif now2 == "B":
                B -= 1
                ans += G
                if 2*j-i < n and s[2*j-i] == "G":
                    ans -= 1
        elif now == "G":
            if now2 == "R":
                R -= 1
                ans += B
                if 2*j-i < n and s[2*j-i] == "B":
                    ans -= 1
            elif now2 == "B":
                B -= 1
                ans += R
                if 2*j-i < n and s[2*j-i] == "R":
                    ans -= 1
        elif now == "B":
            if now2 == "G":
                G -= 1
                ans += R
                if 2*j-i < n and s[2*j-i] == "R":
                    ans -= 1
            elif now2 == "R":
                R -= 1
                ans += G
                if 2*j-i < n and s[2*j-i] == "G":
                    ans -= 1
print(ans)

Solution (Python):
import itertools
from sys import exit

N = int(input())
S = input()
if N < 3:
    print(0)
    exit()

d = {'R': 0, 'G': 1, 'B': 2}
rgb = {'R', 'G', 'B'}

table = [[0] * N for _ in range(3)]
for i, s in enumerate(S):
    table[d[s]][i] = 1

for x in range(3):
    table[x] = tuple(itertools.accumulate(table[x]))

ans = 0
for k in range(2, N):
    for j in range(1, k):
        if S[j] != S[k] :
            x = (rgb - {S[j], S[k]}).pop()
            ans += table[d[x]][j - 1]
            if 2 * j - k >= 0 and S[2 * j - k] == x:
                ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
RGB = ['R', 'G', 'B']
import collections
if len(set(list(S))) <= 2:
    print(0)
else:
    dct = dict(collections.Counter(S))

    cnt = 0
    for i in range(N-2):
        for k in range((N - 1 - i) // 2 + 1):
            temp = {S[i], S[i + k], S[i + k * 2]}
            if len(temp) == 3:
                cnt += 1
            
    print(dct['R'] * dct['G'] * dct['B'] - cnt)

Solution (Python):


from collections import Counter
def MI():return map(int,input().split())
def LI():return list(MI())

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


for j in range(n):
    jj=s[j]
    for i in range(j):
        k=2*j-i
        if not j<=k<=n-1:
            continue
        ii=s[i]
        kk=s[k]
        if jj!=ii and jj!=kk and ii!=kk:
            ans-=1

print(ans)

Solution (Python):
N=int(input())
S=input()
R=[0]*N
G=[0]*N
B=[0]*N

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

ans=0
for j in range(1,N-1):
    char=S[j]
    if char=="G":
        ans += R_S[j-1]*(B_S[N-1]-B_S[j])
        ans += B_S[j-1]*(R_S[N-1]-R_S[j])
        min_len=min(j,N-1-j)
        for i in range(1,min_len+1):
            if (S[j-i]=="R" and S[j+i]=="B") or (S[j-i]=="B" and S[j+i]=="R"):
                ans -= 1
    if char=="R":
        ans += G_S[j-1]*(B_S[N-1]-B_S[j])
        ans += B_S[j-1]*(G_S[N-1]-G_S[j])
        min_len=min(j,N-1-j)
        for i in range(1,min_len+1):
            if (S[j-i]=="G" and S[j+i]=="B") or (S[j-i]=="B" and S[j+i]=="G"):
                ans -= 1
    if char=="B":
        ans += G_S[j-1]*(R_S[N-1]-R_S[j])
        ans += R_S[j-1]*(G_S[N-1]-G_S[j])
        min_len=min(j,N-1-j)
        for i in range(1,min_len+1):
            if (S[j-i]=="G" and S[j+i]=="R") or (S[j-i]=="R" and S[j+i]=="G"):
                ans -= 1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())


def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))


def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
S = input()

if N < 3:
    print(0)
    exit()
rs = []
gs = []
bs = []
for i in range(len(S)):
    if S[i] == 'R':
        rs.append(i)
    elif S[i] == 'G':
        gs.append(i)
    else:
        bs.append(i)
count = len(rs) * len(gs) * len(bs)








for skip in range(1, N // 2 + 1):
    for i in range(N - 2):
        if i + 2 * skip > N - 1:
            break
        elif S[i] == S[i + skip] or S[i + skip] == S[i + 2 * skip] or S[i] == S[i + 2 * skip]:
            continue
        else:
            count -= 1

print(count)

Solution (Python):
from collections import defaultdict
n = int(input())
S = input()
patterns = ["RGB","RBG","GBR","GRB","BRG","BGR"]
acc  = [ [0] * (n + 1) for _ in range(n+1)]

ans = 0
for pattern in patterns:
    c1 = pattern[0]
    c2 = pattern[1]
    c3 = pattern[2]
    cands = []
    
    for i1 in range(n):
        for i2 in range(i1 + 1,n):
            if S[i1] == c2 and S[i2] == c3:
                cands.append((i1,i2-i1))
    
    cands.reverse()
    di = defaultdict(lambda :set())
    for cand in cands:
        left,dist = cand
        di[left].add(dist)
    acc = [0]
    for i in range(1,n):
        acc.append(acc[i-1] + len(di[i]))

    for  i in range(0,n):
        if S[i] == c1:
            ans += acc[n - 1] -acc[i]
            for i2 in range(i + 1,n):
                if i2 - i in di[i2]:
                    ans -=1
print(ans)

Solution (Python):
import copy

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

RL = [0]*(N+1)
GL = copy.deepcopy(RL)
BL = copy.deepcopy(RL)
R = []
G = []
B = []
for i in range(N):
    if S[i] == 'R':
        R.append(i+1)
        RL[i+1] = 1
    elif S[i] == 'G':
        G.append(i+1)
        GL[i+1] = 1
    elif S[i] == 'B':
        B.append(i+1)
        BL[i+1] = 1

len_r = len(R)
len_g = len(G)
len_b = len(B)
ans = 0
if max(len_r, len_g, len_b) == len_b:
    for i in range(len_r):
        for j in range(len_g):
            ans += len_b
            tmp_abs = abs(R[i]-G[j])

            index = (min(R[i], G[j])-tmp_abs)
            if index > 0 and BL[index] == 1:
                ans -= 1

            index = (max(R[i], G[j])+tmp_abs)
            if index <= N and BL[index] == 1:
                ans -= 1

            if tmp_abs % 2 == 0 and BL[max(R[i], G[j])-(tmp_abs//2)] == 1:
                ans -= 1

elif max(len_r, len_g, len_b) == len_g:
    for i in range(len_r):
        for j in range(len_b):
            ans += len_g
            tmp_abs = abs(R[i]-B[j])

            index = (min(R[i], B[j])-tmp_abs)
            if index > 0 and GL[index] == 1:
                ans -= 1

            index = (max(R[i], B[j])+tmp_abs)
            if index <= N and GL[index] == 1:
                ans -= 1

            if tmp_abs % 2 == 0 and GL[max(R[i], B[j])-(tmp_abs//2)] == 1:
                ans -= 1

else:
    for i in range(len_g):
        for j in range(len_b):
            ans += len_r
            tmp_abs = abs(G[i]-B[j])

            index = (min(G[i], B[j])-tmp_abs)
            if index > 0 and RL[index] == 1:
                ans -= 1

            index = (max(G[i], B[j])+tmp_abs)
            if index <= N and RL[index] == 1:
                ans -= 1

            if tmp_abs % 2 == 0 and RL[max(G[i], B[j])-(tmp_abs//2)] == 1:
                ans -= 1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	n = II()
	s = IS()
	r,g,b=[],[],[]

	def binary_search(lst, K):
		l = 0; r = len(lst);
		while(l+1<r):
			mid = (l+r)//2
	    
			if(lst[mid]>K): r=mid
			else: l=mid
		return l

	for i,ss in enumerate(s):
		if ss=='R':
			r.append(i)
		elif ss=='G':
			g.append(i)
		else:
			b.append(i)
	ans=len(r)*len(g)*len(b)
	for rr in r:
		for gg in g:
			a=max(rr,gg)+abs(rr-gg)
			if(a<n):
				if(s[a]=="B"):
					ans-=1
			a=min(rr,gg)-abs(rr-gg)
			if(a<n and a>=0):
				if(s[a]=="B"):
					ans-=1
			if((rr+gg)%2==0):
				a=(rr+gg)//2
				if(a<n):
					if(s[a]=="B"):
						ans-=1
	print(ans)







if __name__ == '__main__':
	main()

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

rgb = [[S.count('R'), S.count('G'), S.count('B')]]

if S[0] == 'R':
    rgb[0][0] -= 1
elif S[0] == 'G':
    rgb[0][1] -= 1
else:
    rgb[0][2] -= 1

for i in range(1, n):
    r, g, b = rgb[i-1][0], rgb[i-1][1], rgb[i-1][2]
    if S[i] == 'R':
        r -= 1
    elif S[i] == 'G':
        g -= 1
    else:
        b -= 1
    rgb.append([r, g, b])

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
            if 2*j-i < n and S[2*j-i] == 'B':
                ans += rgb[j][2] - 1
            else:
                ans += rgb[j][2]
        elif (S[i] == 'R' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'R'):
            if 2*j-i < n and S[2*j-i] == 'G':
                ans += rgb[j][1] - 1
            else:
                ans += rgb[j][1]
        elif (S[i] == 'G' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'G'):
            if 2*j-i < n and S[2*j-i] == 'R':
                ans += rgb[j][0] - 1
            else:
                ans += rgb[j][0]

print(ans)

Solution (Python):
import sys,math,copy,queue,itertools,bisect
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

N = NI()
S = input()
R = []
G = []
B = []
cnt = 0
for b in range(N-1,-1,-1):
    if S[b] == 'R':
        cnt += 1
    R.append(cnt)
R.reverse()
cnt = 0
for b in range(N-1,-1,-1):
    if S[b] == 'G':
        cnt += 1
    G.append(cnt)
G.reverse()
cnt = 0
for b in range(N-1,-1,-1):
    if S[b] == 'B':
        cnt += 1
    B.append(cnt)
B.reverse()

ans = 0
for r in range(N-2):
    if S[r] == 'R':
        for g in range(r+1,N-1):
            if S[g] == 'G' and B[g] > 0:
                ans += B[g]
                if g + (g-r) < N:
                    ans -= S[g+(g-r)] == 'B'
        for b in range(r+1,N-1):
            if S[b] == 'B' and G[b] > 0:
                ans += G[b]
                if b + (b-r) < N:
                    ans -= S[b+(b-r)] == 'G'

for r in range(N-2):
    if S[r] == 'G':
        for g in range(r+1,N-1):
            if S[g] == 'B' and R[g] > 0:
                ans += R[g]
                if g + (g-r) < N:
                    ans -= S[g+(g-r)] == 'R'
        for b in range(r+1,N-1):
            if S[b] == 'R' and B[b] > 0:
                ans += B[b]
                if b + (b-r) < N:
                    ans -= S[b+(b-r)] == 'B'

for r in range(N-2):
    if S[r] == 'B':
        for g in range(r+1,N-1):
            if S[g] == 'R' and G[g] > 0:
                ans += G[g]
                if g + (g-r) < N:
                    ans -= S[g+(g-r)] == 'G'
        for b in range(r+1,N-1):
            if S[b] == 'G' and R[b] > 0:
                ans += R[b]
                if b + (b-r) < N:
                    ans -= S[b+(b-r)] == 'R'

print (ans)

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
rg = 0
gb = 0
br = 0
rgb = 0
for i in range(n):
    if s[i] == 'R':
        r+=1
        rg+=g
        br+=b
        rgb+=gb
    elif s[i] == 'G':
        g+=1
        rg+=r
        gb+=b
        rgb+=br
    else:
        b+=1
        br+=r
        gb+=g
        rgb+=rg

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

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):
    mid = s[i]
    front = s[i-1::-1]
    back = s[i+1:]
    for j in range(min(len(front),len(back))):
        if mid != front[j] and mid != back[j] and front[j] != back[j]:
            ans -= 1
    
print(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(1,int((n-i-1)/2)+1):
        if len(set([s[i],s[i+j],s[i+j+j]])) == 3:
            c += 1
print((r*g*b) - c)

Solution (Python):



def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))

n = int(raw_input())
s = raw_input()
con = {'R':0,'B':1,'G':2}
r_dict = [[0, 0, 0] for i in range(n)]
r_dict[n - 1][con[s[n - 1]]] = 1
for i in range(n - 2,-1,-1):
    for j in range(3):
        r_dict[i][j] = r_dict[i + 1][j]
    r_dict[i][con[s[i]]] += 1
ans = 0
for i in range(n):
    f = s[i]
    for j in range(i + 1,n):
        if s[j] != s[i]:
            se = s[j]
            gap = j - i
            if 'R' != f and 'R' !=se:
                th= 'R'
            elif 'B' != f and 'B' != se:
                th = 'B'
            else:
                th = 'G'
            
            ans += r_dict[j][con[th]]
            if j + gap < n and s[j + gap] == th:
                ans -= 1
        
print ans

Solution (Python):
from bisect import bisect_left
N = int(input())
S = input()
X = {"R": [], "G": [], "B": []}
Xset = {"R": set(), "G": set(), "B": set()}
for i, c in enumerate(S):
    X[c].append(i)
    Xset[c].add(i)

ans = 0
for i in range(N - 2):
    si = S[i]
    for j in range(i + 1, N - 1):
        sj = S[j]
        if si == sj:
            continue
        x = ["R", "G", "B"]
        x.remove(si)
        x.remove(sj)
        sk = x[0]
        ks = X[sk]
        count = len(ks) - bisect_left(ks, j)
        if 2 * j - i in Xset[sk]:
            count -= 1
        ans += count
print(ans)

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


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

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

ans=0
q=len(g)
if q==0:
    print(0)
    exit()

for i in range(len(r)):
    for j in range(len(b)):
        if r[i]<b[j]:
            x=b[j]+b[j]-r[i]
            y=r[i]-(b[j]-r[i])
        else:
            x=r[i]+r[i]-b[j]
            y=b[j]-(r[i]-b[j])

        if 0<=x and x<=n-1:
            z=bisect.bisect_right(g,x)-1
            if g[z]==x:
                ans-=1
        if 0<=y and y<=n-1:
            z=bisect.bisect_right(g,y)-1
            if g[z]==y:
                ans-=1
        if (r[i]+b[j])%2==0 and 0<=(r[i]+b[j])//2 and (r[i]+b[j])//2<=n-1:
            w=(r[i]+b[j])//2
            z=bisect.bisect_right(g,w)-1
            if g[z]==w:
                ans-=1
        ans+=q
print(ans)

Solution (Python):
n=int(input())
s=list(input())
r=0
g=0
b=0
for c in s:
    if c=='R':
        r+=1
    elif c=='G':
        g+=1
    else:
        b+=1
u=0
m=(n-1)//2
for c in range(1,m+1):
    for d in range(n-2*c):
        w={s[d],s[d+c],s[d+2*c]}
        if len(w)==3:
            u-=1
u+=r*g*b
print(u)

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
RGB = set(['R', 'G', 'B'])

exclude = 0
for d in range(1, N // 2 + 1):
    for i in range(N - 2 * d):
        if set([S[i], S[i + d], S[i + 2 * d]]) == RGB:
            exclude += 1
ans = r * g * b
print(ans - exclude)

Solution (Python):
N = int(input())
S = input()
r = []
g = []
b = []
n_r = n_g = n_b = 0

for i in range(N):
    if S[i] == "R":
        r.append(i)
        n_r += 1
    elif S[i] == "G":
        g.append(i)
        n_g += 1
    else:
        b.append(i)
        n_b += 1

all = n_r*n_b*n_g

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

        j += 1
print(all-tmp)

Solution (Python):
import bisect
n=int(input())
s=input()
r=[]
g=[]
b=[]
for i in range(n):
  if s[i]=='R':
    r.append(i)
  elif s[i]=='G':
    g.append(i)
  else:
    b.append(i)
ans=len(r)*len(g)*len(b)
r.append(n*4)
g.append(n*4)
b.append(n*4)
for i in g:
  for j in b:
    a=i+j
    if a%2==0 and r[bisect.bisect_left(r, a//2)]==a//2:
      ans-=1
for i in r:
  for j in b:
    a=i+j
    if a%2==0 and g[bisect.bisect_left(g, a//2)]==a//2:
      ans-=1
for i in g:
  for j in r:
    a=i+j
    if a%2==0 and b[bisect.bisect_left(b, a//2)]==a//2:
      ans-=1
print(ans+3)

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

Solution (Python):
def main():
    N = int(input())
    S = input()
    r = [0]
    g = [0]
    b = [0]
    for i in S:
        if i == 'R':
            r.append(r[-1]+1)
            g.append(g[-1])
            b.append(b[-1])
        if i == 'G':
            r.append(r[-1])
            g.append(g[-1]+1)
            b.append(b[-1])
        if i == 'B':
            r.append(r[-1])
            g.append(g[-1])
            b.append(b[-1]+1)
    rr = r[-1]
    gg = g[-1]
    bb = b[-1]
    ans = 0
    for i, v in enumerate(S[:-2], start=1):
        if v == 'R':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'G':
                    ans += bb - b[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'B':
                        ans -= 1
                if w == 'B':
                    ans += gg - g[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'G':
                        ans -= 1
        if v == 'G':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'R':
                    ans += bb - b[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'B':
                        ans -= 1
                if w == 'B':
                    ans += rr - r[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'R':
                        ans -= 1
        if v == 'B':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'R':
                    ans += gg - g[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'G':
                        ans -= 1
                if w == 'G':
                    ans += rr - r[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'R':
                        ans -= 1
    return ans
print(main())

Solution (Python):
n = int(input())
s = input()
c = [[0,0,0] for _ in range(n+1)]
if n<3:
    result = 0
else:
    for i in range(1, n+1):
        if s[-i]=="R":
            c[i][0] += 1
        elif s[-i]=="G":
            c[i][1] += 1
        else:
            c[i][2] += 1
        for j in range(3):
            c[i][j] += c[i-1][j]
    c = c[::-1]
    s = s+"0"*(n+1)
    result = 0
    for i in range(n-1):
        if s[i]=="R":
            for j in range(i+1, n):
                if s[j]=="G":
                    result += c[j][2] - ("B"==s[2*j-i])
                elif s[j]=="B":
                    result += c[j][1] - ("G"==s[2*j-i])
        if s[i]=="G":
            for j in range(i+1, n):
                if s[j]=="R":
                    result += c[j][2] - ("B"==s[2*j-i])
                elif s[j]=="B":
                    result += c[j][0] - ("R"==s[2*j-i])
        if s[i]=="B":
            for j in range(i+1, n):
                if s[j]=="G":
                    result += c[j][0] - ("R"==s[2*j-i])
                elif s[j]=="R":
                    result += c[j][1] - ("G"==s[2*j-i])
print(result)

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


i,j = 0,1
while 2*j < N:
    while i+2*j < N:
        
        if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i+2*j] != S[i]:
            c -= 1
        i += 1
    i = 0
    j += 1
print(c)

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    S = input()
    C = Counter([s for s in S]).items()
    ans = 1
    if len (C) < 3:
        ans = 0
    for c in C:
        ans *= c[1]
    for i in range(n):
        mid = i + 1
        last = i + 2
        while last < n:
            if S[i] != S[mid] and S[mid] != S[last] and S[last] != S[i]:
                ans -= 1
            mid += 1
            last += 2
    print(ans)
main()

Solution (Python):
N = int(input())
S = input()
r_cnt = S.count('R')
g_cnt = S.count('G')
b_cnt = N - (r_cnt + g_cnt)

ans = r_cnt * g_cnt * b_cnt

for i in range(1,(N//2)+1):
	for j in range(N):
		if j + 2*i >= N:
			break
		tmp = [S[j],S[j+i],S[j+2*i]]
		tmp = list(set(tmp))
		if len(tmp) == 3:
			ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = list(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)

r_kazu = len(r)
g_kazu = len(g)
b_kazu = len(b)

A = r_kazu * g_kazu * b_kazu



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

                    A -= 1


print(A)

Solution (Python):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

Rs = []
Gs = []
Bs = []
for index, s in enu(S):
    if s == 'R':
        Rs.append(index+1)
    elif s == 'G':
        Gs.append(index+1)
    elif s == 'B':
        Bs.append(index+1)


cnt = len(Rs) * len(Gs) * len(Bs)
sBs = set(Bs)

for rr in Rs:
    for gg in Gs:
        r, g = sorted([rr, gg])

        pt1 = g + (g-r)
        pt2 = -1
        if (g-r)%2==0:
            pt2 = r + (g-r)//2
        pt3 = r - (g-r)

        chks = [val for val in [pt1, pt2, pt3] if val > 0]
        for chk in chks:
            if chk in sBs:
                cnt -= 1

print(cnt)

Solution (Python):
import bisect
import sys
input=sys.stdin.readline
def main():
    N=int(input())
    S=input().rstrip()
    R,G,B=[],[],[]
    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)
    ans=0
    for i in range(N):
        for j in range(i):
            if(S[i]==S[j]):
                continue
            s=set([S[i],S[j]])
            if('B' not in s):
                lon=len(B)-bisect.bisect_left(B,i)
                mi=bisect.bisect_left(B,i+i-j)
                if(mi<len(B) and B[mi]==i*2-j):
                    lon-=1
            elif('R' not in s):
                lon=len(R)-bisect.bisect_left(R,i)
                mi=bisect.bisect_left(R,i+i-j)
                if(mi<len(R) and R[mi]==i*2-j):
                    lon-=1
            elif('G' not in s):
                lon=len(G)-bisect.bisect_left(G,i)
                mi=bisect.bisect_left(G,i+i-j)
                if(mi<len(G) and G[mi]==i*2-j):
                    lon-=1
            ans+=lon
        
    print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):
import collections

n=int(input())
s=input()
m=(n-1)//2
x=0

l=collections.Counter(s)
ans=l['R']*l['G']*l['B']

for i in range(1, m+1):
    for j in range(n-i*2):
        a=j
        b=a+i
        c=b+i
        if s[a] != s[b] != s[c] != s[a]:
            x += 1

print(ans-x)

Solution (Python):
n = int(input())
s = list(input())
r = 0
g = 0
b = 0
if (n==3):
    print(0)
    exit()


for i in range(n):
    if (s[i]=="R"):
        r += 1
    elif (s[i]=="G"):
        g += 1
    else:
        b += 1
da = r*g*b
count = 0
if (n%2==1):
    for i in range (0,n//2):
        for j in range(0,n-2*(i+1)):
            if (s[j]!=s[j+i+1] and s[j+i+1]!=s[j+2*(i+1)] and s[j+2*(i+1)]!=s[j]):
                count += 1
else:
    for i in range (0,n//2-1):
        for j in range(0,n-2*(i+1)):
            if (s[j]!=s[j+i+1] and s[j+i+1]!=s[j+2*(i+1)] and s[j+2*(i+1)]!=s[j]):
                count += 1
                      
print(da -count)

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)
    elif s[i] == "B":
        b.append(i)

ans = len(r) * len(g) * len(b)

j = 1
while j * 2 <= n:
    for k in range(n - 2 * j):
        if set([s[k], s[k + j] , s[k + 2 * j]]) == set(["R", "G", "B"]):
            ans -= 1
    j += 1

print(ans)

Solution (Python):
x = int(input())
s = list(input())
di = {'R':0, 'G':0, 'B':0}
for i in s:
    di[i] += 1
c = 0 
p = (len(s) - 3) / 2
for i in range(len(s)-2):
    j = 1
    while(j <= p + 1and i+(2*j)<=len(s)-1):
        ss = s[i] + s[i+j] + s[i+(2*j)]
        j += 1
        if ss == 'GBR'or ss ==  'GRB' or ss ==  'RBG' or ss == 'RGB' or  ss == 'BGR' or ss == 'BRG':
                c += 1
print(di['R'] * di['G'] * di['B'] - c)

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

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

print(count)

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

R = S.count("R")
G = S.count("G")
B = S.count("B")
ignore = 0
for i in range(N):
    a = S[i]
    for j in range(1,N):
        if i + j + j >= N:
            break
        b = S[i+j]
        c = S[i+j+j]
        if a != b and b != c and c != a:
            ignore += 1
print(R * G * B - ignore)