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

rs = {}
gs = {}
bs = {}

for i, e in enumerate(s):
  if e == 'R':
    rs[i] = ''
  if e == 'G':
    gs[i] = ''
  if e == 'B':
    bs[i] = ''
    
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  a = max(bs.keys())
  b = min(bs.keys())
  for i in rs.keys():
    for j in gs.keys():
      mi = i if i < j else j
      ma = j if i < j else i
      p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
      for e in p:
        if e%1 != 0 or e < b or e > a:
          continue
        if e in bs:
          saidai -= 1
  print(int(saidai))

Solution (Python):
N = int(input())
S = input()
ans = 0
T = {'R', 'G', 'B'}
sum_R, sum_G, sum_B = [0], [0], [0]
 
for i in S:
    sum_R.append(i=='R')
    sum_G.append(i=='G')
    sum_B.append(i=='B')
    
for i in range(1, N+1):
    sum_R[i] = sum_R[i-1] + sum_R[i]
    sum_G[i] = sum_G[i-1] + sum_G[i]
    sum_B[i] = sum_B[i-1] + sum_B[i]
    
dict_RGB = {'R':sum_R, 'G':sum_G, 'B':sum_B}
 
for i in range(N-2):
    s_i = S[i]
    for j in range(i+1, N-1):
        s_j = S[j]
        if s_i == s_j:
            continue
        
        s_k = list(T - set((s_i, s_j)))[0]
        
        sum_s_k = dict_RGB[s_k]
        ans = ans + sum_s_k[-1] - sum_s_k[j]
        if (2 * j - i < N) and (S[2*j - i]==s_k):
            ans -= 1
        
    
print(ans)

Solution (Python):
import collections

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

CNT_S = collections.Counter(S)

cnt_r = CNT_S['R']
cnt_b = CNT_S['B']
cnt_g = CNT_S['G']

total = cnt_r * cnt_g * cnt_b
subdu = 0


for pos in range(N):
    for i in range(1, N):
    
        index_r = pos
        index_m = pos + i
        index_l = pos + 2 * i
        
        if (index_l < N and index_m < N and index_r < N):
            if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                subdu += 1


ans = total - subdu

print(ans)

Solution (Python):
n = int(input())
strings = input()
r_array, g_array, b_array = list(), list(), list()

sums = [0, 0, 0]


def counter(s):
    if s == "R":
        sums[0] += 1
    elif s == "G":
        sums[1] += 1
    else:
        sums[2] += 1
    r_array.append(sums[0])
    g_array.append(sums[1])
    b_array.append(sums[2])


for s in strings:
    counter(s)

ret = 0
for i in range(n):
    s = strings[i]
    _r, _g, _b = r_array[i], g_array[i], b_array[i]
    r_, g_, b_ = sums[0] - _r, sums[1] - _g, sums[2] - _b
    if s == "R":
        ret += _g * b_ + _b * g_
    elif s == "G":
        ret += _r * b_ + _b * r_
    else:
        ret += _r * g_ + _g * r_

    for j in range(min(i + 1, n - i)):
        if (strings[i - j] != s) and (strings[i + j] != s) and (strings[i - j] != strings[i + j]):
            ret -= 1
print(ret)

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

r = tmp["R"]
g = tmp["G"]
b = tmp["B"]
ans = 0








for i in range(len(S) - 2):
    j = 1
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    while not (i + j + j >= len(S)):
        if S[i] == "R":
            if S[i + j] == "R":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "G":
                j += 1
                continue
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "B":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
        j += 1
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
s = input()
c = Counter({'R': 0,'G': 0, 'B': 0})
c.update(s)
x, y, z = c.values()
print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                      for i in range(n) for j in range(i + 2, n, 2)))

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

    rc0 = [0] * N
    gc0 = [0] * N
    bc0 = [0] * N

    rc = [0] * N
    gc = [0] * N
    bc = [0] * N

    rlist = []
    glist = []
    blist = []

    if True:
        i = N - 1
        if S[i] == 'R':
            rc[i] = 1
            rc0[i] = 1
        elif S[i] == 'G':
            gc[i] = 1
            gc0[i] = 1
        elif S[i] == 'B':
            bc[i] = 1
            bc0[i] = 1

    for i in range(N - 2, -1, -1):
        if S[i] == 'R':
            rc[i] += 1
            rc0[i] = 1
            rlist.append(i)
        elif S[i] == 'G':
            gc[i] += 1
            gc0[i] = 1
            glist.append(i)
        else:
            bc[i] += 1
            bc0[i] = 1
            blist.append(i)

    for i in range(N - 2, -1, -1):
        rc[i] += rc[i + 1]
        gc[i] += gc[i + 1]
        bc[i] += bc[i + 1]

    def count(rlist, glist, bc, bc0):
        v = 0

        for i in rlist:
            for j in glist:
                if i >= j:
                    break
                k = 2 * j - i
                if k < N:
                    v += bc[j + 1] - bc0[k]
                else:
                    v += bc[j + 1]
        return v

    rgb = 0
    rgb += count(rlist, glist, bc, bc0)
    rgb += count(rlist, blist, gc, gc0)
    rgb += count(glist, rlist, bc, bc0)
    rgb += count(glist, blist, rc, rc0)
    rgb += count(blist, rlist, gc, gc0)
    rgb += count(blist, glist, rc, rc0)
    print(rgb)


main()

Solution (Python):
N = int(input())
S = list(input())
R =[]
G = []
B = []
lr = 0
lg = 0
lb = 0
ncount = 0
for i in range(N):
    if S[i] == "R":
        R.append(i)
        lr+=1
    elif S[i] == "G":
        G.append(i)
        lg+=1
    else:
        B.append(i)
        lb+=1
    if i < N/2:
        for j in range(i+1):
            if S[i-j]!=S[i]:
                if S[i-j] != S[i+j]:
                    if S[i+j] != S[i]:
                        ncount+=1
    else:
        for j in range(N-i):
                if S[i-j]!=S[i]:
                    if S[i-j] != S[i+j]:
                        if S[i+j] != S[i]:
                            ncount+=1
print(lr*lg*lb - ncount)

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

r = 0
r_l = []
g = 0
g_l = []
b = 0
b_l = []

for i in range(n):
    if s[i] == "R":
        r += 1
        r_l.append(i)
    if s[i] == "G":
        g += 1
        g_l.append(i)
    if s[i] == "B":
        b += 1
        b_l.append(i)
        
ans = r*g*b

for i in r_l:
    for j in g_l:
        tmp = abs(i-j)
        if 0 <= i-tmp and s[i-tmp] == "B":
            ans -= 1
        if 0 <= j-tmp and s[j-tmp] == "B":
            ans -= 1
        if i+tmp < n and s[i+tmp] == "B":
            ans -= 1
        if j+tmp < n and s[j+tmp] == "B":
            ans -= 1

        if (tmp / 2).is_integer():
            if s[max(i,j)-(tmp//2)] == "B":
                ans -= 1
            if max(i,j)-(tmp//2) != min(i,j)+(tmp//2) and s[min(i,j)+(tmp//2)] == "B":
                ans -= 1

print(ans)

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

def im(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
R = im(s, "R")
G = im(s, "G")
B = im(s, "B")

ext = 0
if n >= 3:
    for i in range((n-3)//2+1):
        for j in range(n-(3+2*i)+1):
            if s[j] != s[j+i+1] and s[j] != s[j+2*(i+1)] and s[j+i+1] != s[j+2*(i+1)]:
                ext += 1

ans = len(R)*len(G)*len(B) - ext
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
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 = 0

def bs(nums, target):
    l = 0
    r = len(nums) - 1
    while l <= r:
        mid = (l + r) // 2
        if nums[mid] == target:
            return True
        elif nums[mid] < target:
            l = mid + 1
        else:
            r = mid - 1
    return False

def solution(t1, t2, t3):
    ans = 0
    if len(t1) < 1 or len(t2) < 1 or len(t3) < 1:
        return ans
    for i in range(len(t2)):
        num = t2[i]
        left = bisect_left(t1, num)
        right = bisect_right(t3, num)
        ans += left * (len(t3) - right)

    for i in t1:
        for j in t3:
            if i < j:
                if (i + j) % 2 == 0 and bs(t2, (i + j) // 2):
                    ans -= 1
    return ans

ans = 0
ans += solution(r, g, b)
ans += solution(r, b, g)
ans += solution(g, r, b)
ans += solution(g, b, r)
ans += solution(b, r, g)
ans += solution(b, g, r)
print(ans)

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





s = input()
count = 0

"""
for i in range(n):
    for j in range(i+1, n):
        if s[i]!=s[j]:    
            for k in range(j+1, n):
                if j-i!=k-j:
                    if s[i]!=s[k] and s[j]!=s[k]:
                        count += 1
"""
for i in range(n):
    a = s[i:]
    if s[i]=="R":
        point = a.count("G")*a.count("B")
    elif s[i]=="G":
        point = a.count("R")*a.count("B")
    else:
        point = a.count("R")*a.count("G")
    count += point




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


print(count-kom)

Solution (Python):
def solve(n, s):
    rlist, glist, blist = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
    rcount, gcount, bcount = 0, 0, 0
    for i in range(n-1):
        if s[n-1-i] == 'R':
            rcount += 1
        if s[n-1-i] == 'G':
            gcount += 1
        if s[n-1-i] == 'B':
            bcount += 1
        rlist[n-2-i], glist[n-2-i], blist[n-2-i] = rcount, gcount, bcount
        
    ans = 0

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                if s[i] == 'R':
                    if s[j] == 'G':
                        ans += blist[j] - ( 1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    else:
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
                elif s[i] == 'G':
                    if s[j] == 'R':
                        ans += blist[j] - (1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    else:
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                else:
                    if s[j] == 'G':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                    else:
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
    
    return ans

def main():
    n = int(input())
    s = input()
    print(solve(n, s))

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=input()
c=0
B1B=0
B1R=0
B1G=0
if A[0]=="B":
 B1B+=1
elif A[0]=="R":
 B1R+=1
else:
 B1G+=1
for i in range(N-2):
 B2=A[i+1]
 for k in range(N-i-2):
  B3=A[i+2+k]
  if (B2=="B" and B3=="R")or (B2=="R" and B3=="B"):
   c+=B1G
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="G":
     c-=1
  if (B2=="G" and B3=="R")or (B2=="R" and B3=="G"):
   c+=B1B
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="B":
     c-=1
  if (B2=="B" and B3=="G")or (B2=="G" and B3=="B"):
   c+=B1R
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="R":
     c-=1
 if B2=="B":
  B1B+=1
 elif B2=="R":
  B1R+=1
 else:
  B1G+=1
print(c)

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)
temp2 = []
for i in r:
	for j in g:
		temp = []
		if (i+j)%2 == 0:
			temp.append((i+j)//2)
		temp.append(min(i,j)-abs(i-j))
		temp.append(max(i,j)+abs(i-j))
		temp2.append(temp)
l = len(b)
d = {}
for i in b:
	d[i] = 1
res = 0
for i in temp2:
	x = l
	for j in i:
		if j in d:
			x -= 1
	res += x
print(res)

Solution (Python):
N = int(input())
S = list(input())
r = []
g = []
b = []
s = len(S)
for i in range(s):
    if S[i] == 'R':
        r.append(i)
    elif S[i] == 'G':
        g.append(i)
    else:
        b.append(i)
lr = len(r)
lg = len(g)
lb = len(b)
cnt = lr * lg * lb
for i in range(lr):
    for j in range(lg):
        r0 = r[i]
        g0 = g[j]
        up = max(r0,g0)+abs(r0-g0)
        dw = min(r0,g0)-abs(r0-g0)
        if up < s and S[up] == 'B':
            cnt -= 1
        if dw >= 0 and S[dw] == 'B':
            cnt -= 1
        if abs(r0-g0)%2 == 0:
            half = min(r0,g0) + abs(r0-g0)//2
            if 0 <= half < s and S[half] == 'B':
                cnt -= 1
print(cnt)

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

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

ans = 0
for r in R:
  for g in G:
    if r < g: x, y = r, g
    else: x, y = g, r

    ans += len(B)
    if x*2 - y >= 0:
      if s[x*2 - y] == 'B': ans -= 1
    if (x + y) % 2 == 0:
      if s[(x+y)//2] == 'B': ans -= 1
    if y*2 - x < len(s):
      if s[y*2 - x] == 'B': ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = list(input())
r_list = []
g_list = []
b_list = []

for i in range(n):
  if s[i] == "R":
    r_list.append(i)
  elif s[i] == "G":
    g_list.append(i)
  elif s[i] == "B":
    b_list.append(i)
    
judge = n%2
if judge == 1:
  lim = (n-3)//2 + 1
elif judge == 0:
  lim = (n-4)//2 + 1

pena = 0

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

print(len(r_list)*len(g_list)*len(b_list) - pena)

Solution (Python):

"""
K = int(input())

def gcd(a, b): 
    a = abs(a)
    b = abs(b)
    while b:
        a, b = b, a % b
    return a 
ans = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            d = gcd(gcd(i,j),k)
            if i==j==k:
                ans += d
            elif i==j or j== k or k==i:
                ans += 3*d
            else:
                ans += 6*d
print(ans)
"""
N = int(input())
S = input()
R = []
G = []
B = []
import bisect
for i in range(N):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    else:
        B.append(i)
ans = 0
for rr in R:
    for gg in G:
        
        r = min(rr,gg)
        g = max(rr,gg)
        if 2*r-g>=0:
            id = bisect.bisect_left(B,2*r-g)
            if id < len(B) and 2*r-g == B[id]:
                ans -=1
        
        if 2*g-r < N:
            id = bisect.bisect_left(B,2*g-r)
            if id < len(B) and 2*g-r == B[id]:
                ans -=1
        
        if (g+r)%2==0:
            id = bisect.bisect_left(B,(g+r)//2)
            if id < len(B) and (g+r)//2 == B[id]:
                ans -= 1
        ans += len(B)
        
print(ans)

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

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

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

print(max_cnt-tmp)

Solution (Python):
from itertools import accumulate
n = int(input())
s = input()

r_acc = [0] + list(accumulate([int(s[i] == 'R') for i in range(n)]))
g_acc = [0] + list(accumulate([int(s[i] == 'G') for i in range(n)]))
b_acc = [0] + list(accumulate([int(s[i] == 'B') for i in range(n)]))

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        si = s[i]
        sj = s[j]
        if si == sj:
            continue
        if (si == 'R' and sj == 'G') or (si == 'G' and sj == 'R'):
            ans += b_acc[n] - b_acc[j]
            if n >= j + j - i + 1:
                ans -= b_acc[j + j - i + 1] - b_acc[j + j - i]
        elif (si == 'R' and sj == 'B') or (si == 'B' and sj == 'R'):
            ans += g_acc[n] - g_acc[j]
            if n >= j + j - i + 1:
                ans -= g_acc[j + j - i + 1] - g_acc[j + j - i]
        else:
            ans += r_acc[n] - r_acc[j]
            if n >= j + j - i + 1:
                ans -= r_acc[j + j - i + 1] - r_acc[j + j - i]
print(ans)

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

R_num=S.count('R')
G_num=S.count('G')
B_num=S.count('B')

ans=0
for k in range(N//2-2+1 if N&1==0 else N//2-1+1):
    flag=0
    for i in range(N-k-2):

        if i+2*(k+1)<=N-1:
            ls=[S[i],S[i+k+1],S[i+2*(k+1)]]
    
            if "R" in ls and "G" in ls and "B" in ls:
                ans+=1


print(R_num*G_num*B_num-ans)

Solution (Python):
n = int(input())
list_S = list(input())
list_R = []
list_B = []
list_G = []
ans = 0
for i, c in enumerate(list_S):
    if c == "R":
        list_R.append(i+1)
    if c == "B":
        list_B.append(i+1)
    if c == "G":
        list_G.append(i+1)
set_R = set(list_R)
set_G = set(list_G)
set_B = set(list_B)
cnt = 0
for i in list_R:
    for j in list_G:
        max_value = max(i, j)
        min_value = min(i, j)
        d = (max_value - min_value)
        if d % 2 == 0:
            if max_value-d//2 in set_B:
                cnt += 1

        if max_value+d in set_B:
            cnt += 1
        if min_value-d in set_B:
            cnt += 1
        ans += len(set_B)

print(ans-cnt)

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

colors = ['R', 'G', 'B']
thirds = {'RG': 'B', 'GR': 'B', 'BR': 'G', 'RB': 'G', 'BG': 'R', 'GB': 'R'}
accs = {color: [] for color in colors}
nums = {color: 0 for color in colors}
sums = {color: len([s for s in S if s == color]) for color in colors}
for i in range(N):
    nums[S[i]] += 1
    for color in colors:
        accs[color].append(sums[color] - nums[color])
        
res = 0
for i in range(N-2):
    first = S[i]
    for j in range(i+1, N-1):
        second = S[j]
        if first == second:
            continue
        third = thirds[first+second]
        res += accs[third][j]
        if 2 *j - i < N and S[2*j - i] == third:
            res -= 1
                
print(res)

Solution (Python):
n = int(input())
s = list(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
    elif s[i] == 'B':
        b[i] = 1

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        
        if r[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
        
        if r[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                
            elif j+1<n:
                s = s + g_cum[j+1]
                
        
        if g[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                
            elif j+1<n:
                s = s + r_cum[j+1]
                
        
        if g[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                
            elif j+1<n:
                s = s + b_cum[j+1]
                
        if b[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                
            elif j+1<n:
                s = s + g_cum[j+1]
                
        if b[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                
            elif j+1<n:
                s = s + r_cum[j+1]
                
print(s)

Solution (Python):
n=int(input())
a=list(input())
r=[0]*n
g=[0]*n
b=[0]*n
for i in range(n):
  if i==0:
    if a[n-1-i]=='R':
      r[n-1-i]=1
    elif a[n-1-i]=='G':
      g[n-1-i]=1
    else:
      b[n-1-i]=1
  else:
    if a[n-1-i]=='R':
      r[n-1-i]=r[n-i]+1
      g[n-1-i]=g[n-i]
      b[n-1-i]=b[n-i]
    elif a[n-1-i]=='G':
      r[n-1-i]=r[n-i]
      g[n-1-i]=g[n-i]+1
      b[n-1-i]=b[n-i]
    else:
      r[n-1-i]=r[n-i]
      g[n-1-i]=g[n-i]
      b[n-1-i]=b[n-i]+1

count=0
if n==1:
  print(0)
elif n==2:
  print(0)
else:
  for i in range(n-2):
    for j in range(i+1,n-1):
      if a[i]!=a[j] and 2*j-i<n and a[i]!=a[2*j-i] and a[j]!=a[2*j-i]:
        if a[i]!='R' and a[j]!='R':
          count+=r[j+1]-1
        elif a[i]!='G' and a[j]!='G':
          count+=g[j+1]-1
        else:
          count+=b[j+1]-1
      elif a[i]!=a[j]:
        if a[i]!='R' and a[j]!='R':
          count+=r[j+1]
        elif a[i]!='G' and a[j]!='G':
          count+=g[j+1]
        else:
          count+=b[j+1]
  print(count)

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

Solution (Python):
from itertools import product as pro

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

r = [i for i in range(n) if s[i] == 'R']
g = [i for i in range(n) if s[i] == 'G']
b = [i for i in range(n) if s[i] == 'B']
cnt = len(r) * len(g) * len(b)

length = max(len(r), len(g), len(b))
for i, j, k in [(r, g, b), (g, b, r), (b, r, g)]:
    if length == len(i):
        max1 = set(i)
        min1, min2 = j, k

for i, j in pro(min1, min2):
    if 2*j-i in max1:
        cnt -= 1
    if 2*i-j in max1:
        cnt -= 1
    if (i+j)/2 in max1:
        cnt -= 1

print(cnt)

Solution (Python):


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

    Rnum = [0 for _ in range(N)]  
    Gnum = [0 for _ in range(N)]
    Bnum = [0 for _ in range(N)]

    if S[N-1] == "R":
        Rnum[N-1] = 1
    elif S[N-1] == "G":
        Gnum[N-1] = 1
    else:
        Bnum[N-1] = 1
    for i in range(N-2, -1, -1):
        if S[i] == "R":
            Rnum[i] = Rnum[i+1] + 1
            Gnum[i] = Gnum[i+1]
            Bnum[i] = Bnum[i+1]
        elif S[i] == "G":
            Rnum[i] = Rnum[i+1]
            Gnum[i] = Gnum[i+1] + 1
            Bnum[i] = Bnum[i+1]
        else:
            Rnum[i] = Rnum[i+1]
            Gnum[i] = Gnum[i+1]
            Bnum[i] = Bnum[i+1] + 1

    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i]=="R" and S[j]=="G":
                ans += Bnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "B":
                        ans -= 1
            elif S[i]=="R" and S[j]=="B":
                ans += Gnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "G":
                        ans -= 1
            elif S[i]=="G" and S[j]=="R":
                ans += Bnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "B":
                        ans -= 1
            elif S[i]=="G" and S[j]=="B":
                ans += Rnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "R":
                        ans -= 1
            elif S[i]=="B" and S[j]=="R":
                ans += Gnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "G":
                        ans -= 1
            elif S[i]=="B" and S[j]=="G":
                ans += Rnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "R":
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
n = int(input().strip())
s = '_' + input().strip()
s += ''.join(['_'] * 10 ** 5)
r = []
g = []
b = []
for i in range(1, n + 1):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

le = len(b)
ans = len(r) * len(g) * le
for x in r:
    for y in g:
        if (x + y) % 2 == 0 and s[(x + y) // 2] == 'B':
            ans -= 1
        if (1 <= (2 * x - y) and s[2 * x - y] == 'B'):
            ans -= 1
        if (1 <= (2 * y - x) and s[2 * y - x] == 'B'):
            ans -= 1
print(ans)

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

for x in range(N):
    if S[x] =="R":
        R +=1
        S2.append(["R",R])
    elif S[x] =="G":
        G +=1
        S2.append(["G",G])
    else:
        B +=1
        S2.append(["B",B])
ans =0
a = 0
b =0
for x in range(N):
    i = S2[x][0]
    for y in range(x,N):
        j = S2[y][0]
        if j != i:
            if j =="R":
                a =R-S2[y][1]+1
            elif j =="G":
                a =G-S2[y][1]+1
            else:
                a =B-S2[y][1]+1
            for z in range(x,N):
                k = S2[z][0]
                if k != i and k != j:
                    if k =="R":
                        b =R-S2[z][1]+1
                    elif k =="G":
                        b =G-S2[z][1]+1
                    else:
                        b =B-S2[z][1]+1
                    break
            else:
                b = 0
            break
    else:
        a = 0
    ans +=a*b
subn =0
for x in range(N):
    i = S2[x][0]
    for y in range(x,N):
        j = S2[y][0]   
        if j != i:
            d = y-x
            if y+d<N:
                if i!= S2[y+d][0] and j!=S2[y+d][0]:
                    subn +=1
            else:
                break
print(ans-subn)

Solution (Python):

def main():
    n = int(input())
    s = input()
    dic = {'R':[],'G':[],'B':[]}
    for i in range(n):
        if s[i]== 'R':
            dic['R'].append(i)
        elif s[i]== 'G':
            dic['G'].append(i)
        else:
            dic['B'].append(i)
    cnt = len(dic['R'])*len(dic['G'])*len(dic['B'])
    for i in range(1,n//2+1):
        for j in range(n-2*i):
            if s[j]!=s[j+i] and s[j+i] != s[j+i+i] and s[j]!=s[j+i+i]:
                cnt -=1
    print(cnt)
     
            
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
s = input().strip()

if ('R' not in s) or ('G' not in s) or ('B' not in s):
    print(0)
    sys.exit(0)

from collections import deque

index_cache = {'R': deque(), 'G': deque(), 'B': deque()}
for i, v in enumerate(s):
    index_cache[v].append(i)

ans = 0
for i, v in enumerate(s):
    index_cache[v].popleft()

    v2, v3 = set('RGB') - set(v)
    if len(index_cache[v2]) == 0 or len(index_cache[v3]) == 0:
        break

    i2, i3 = index_cache[v2][0], index_cache[v3][0]
    ans += len(index_cache[v2]) * len(index_cache[v3])

    if i2 < i3:
        for i4 in index_cache[v2]:
            if i4 + (i4 - i) < n and s[i4 + (i4 - i)] == v3:
                ans -= 1
            if (i + i4) % 2 == 0 and s[(i + i4) // 2] == v3:
                ans -= 1
    else:
        for i5 in index_cache[v3]:
            if i5 + (i5 - i) < n and s[i5 + (i5 - i)] == v2:
                ans -= 1
            if (i + i5) % 2 == 0 and s[(i + i5) // 2] == v2:
                ans -= 1

print(ans)

Solution (Python):
import math

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

def Word(i, a, b):
    if S[i] != S[i + a] and S[i] != S[i + a + b] and S[i + a] != S[i + a + b]:
        return True
    else:
        return False

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

ans = R * G * B
for i in range(0, N - 1):
    for a in range(1, math.ceil(N / 2)):
        if i + a * 2 >= N:
            break
        if S[i] != S[i + a] and S[i] != S[i + a * 2] and S[i + a] != S[i + a * 2]:
            ans -= 1

print(ans)

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

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

for i in range (0, N-2):
  colour1 = S[i]
  for j in range (i+1, N-1):
    colour2 = S[j]
    if colour1 == colour2:
      continue
    else:
      if 2*j-i > N-1:
        continue
      elif colour1 != S[2*j-i] and colour2 != S[2*j-i]:
          count-=1
      else:
          continue

print(count)

Solution (Python):
N = int(input())
S = list(input())
rli = []
gli = []
bli = []
for i in range(N):
    if S[i] == "R":
        rli.append(i+1)
    if S[i] == "G":
        gli.append(i+1)
    if S[i] == "B":
        bli.append(i+1)
ans = len(rli)*len(gli)*len(bli)
for i in range(N):
    for j in range(i+1, ((N-i)//2+(N-i)%2)+i):
        if S[i] != S[j] and S[j] != S[2*j-i] and S[2*j-i] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
import itertools

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

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

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))
for i, s in enumerate(S):
  if s == 'R': R.append(i)
  if s == 'G': G.append(i)
  if s == 'B': B.append(i)
out = len(R) * len(G) * len(B)
for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)