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 = list(input())

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

def get_t(a, b):
    a, b = smap[a], smap[b]
    if a + b == 1:
        return 2
    elif a + b == 3:
        return 0
    else:
        return 1

d = [[0 for _ in range(3)] for _ in range(N)]

for i in range(N):
    for j in range(N):
        if i < j:
            s = S[j]
            d[i][smap[s]] += 1

ans = 0
for j in range(1, N-1):
    c = S[j]
    for i in range(0, j):        
        l = S[i]
        if c == l:
            continue
        t = get_t(c, l)
            
        if j + j - i < N and smap[S[j + (j - i)]] == t:
            ans -= 1
        ans += d[j][t]

print(ans)

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

ans=0
a=0
b=0
c=0
for i in range(n):
    if s[i]=="R":
        a+=1
    if s[i]=="G":
        b+=1
    if s[i]=="B":
        c+=1
ans=a*b*c

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

print(ans)

Solution (Python):
import math
n = int(input())
s = input()
r_cnt = 0
g_cnt = 0
b_cnt = 0
for i in s:
    if i == 'R':
        r_cnt += 1
    elif i == 'G':
        g_cnt += 1
    elif i == 'B':
        b_cnt += 1
        
total = r_cnt*g_cnt*b_cnt

if n < 4:
    print(0)

else:
    for i in range(1,math.floor((len(s)-2)/2)+10):
        for j in range(n-2*i):
            tmp = s[j]+s[j+i]+s[j+2*i]
            if (tmp == 'RGB') or (tmp == 'RBG') or (tmp == 'GBR') or (tmp == 'GRB') or (tmp == 'BRG') or (tmp == 'BGR'):
                total -= 1

    print(total)

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

rc = s.count('R')
gc = s.count('G')
bc = s.count('B')

ans = rc*gc*bc

for i in range(n):
    if s[i] == 'R':
        for j in range(1,min(n-i-1, i)+1):
            if s[i-j] != s[i+j] and s[i-j] != 'R' and s[i+j] != 'R':
                ans -= 1
        for j in range(1,int((n-i-1)/2)+1):
            if s[i+2*j] != s[i+j] and s[i+2*j] != 'R' and s[i+j] != 'R':
                ans -= 1
        for j in range(1,int(i/2)+1):
            if s[i-j] != s[i-2*j] and s[i-j] != 'R' and s[i-2*j] != 'R':
                ans -= 1

print(ans)

Solution (Python):
import collections
import math

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

if 'R' in S and 'G'in S and 'B' in S:
    count = collections.Counter(S)
    c_value = list(count.values())
    con = c_value[0]*c_value[1]*c_value[2]
else:
    con = 0

for i in range(N):
    for j in range(i+1, int(math.ceil((N+i)/2))):
        if S[i] != S[j]:
            if S[i] != S[j + (j - i)] and S[j] != S[j + (j - i)]:
                con -= 1

print(con)

Solution (Python):
n=int(input())
s=input()
r=0
g=0
b=0
gh=0
for i in range(n):
    if s[i]=='R':
        r=r+1
    elif s[i]=='G':
        g=g+1
    else:
        b=b+1
for j in range(n-2):
    
    for h in range((n-j-1)//2):
        
        if s[j]!=s[j+h+1] and s[j+h+1]!=s[j+2*(h+1)] and s[j]!=s[j+2*(h+1)]:
            gh=gh+1
        else:
            continue
print(r*g*b-gh)

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')
 
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
 
N = iin()
S = input()

R = []
G = []
B = []
R_count = [0 for _ in range(N)]
G_count = [0 for _ in range(N)]
B_count = [0 for _ in range(N)]
for si in range(len(S)):
    if si > 0:
        R_count[si] = R_count[si-1]
        G_count[si] = G_count[si-1]
        B_count[si] = B_count[si-1]
    if S[si] == 'R': 
        R.append(si)
        R_count[si] += 1
    elif S[si] == 'G': 
        G.append(si)
        G_count[si] += 1
    else: 
        B.append(si)
        B_count[si] += 1


def isOK(index, key):
    
    if index > key: return True
    else: return False


def binary_search(key):
    ng = -1
    ok = N

    while abs(ok - ng) > 1:
        mid = (ok+ng) // 2
        if isOK(mid, key): ok = mid
        else: ng = mid
    return right

first = ''
second = ''
first_index = -1
second_index = -1
ans = 0
for i in range(len(S)):
    first = S[i]
    for j in range(i+1, len(S)):
        if first != S[j]:
            second = S[j]
            diff = j - i
            if not(first == 'R' or second == 'R'):
                ans += R_count[N-1] - R_count[j]
                if j+diff < len(S) and S[j+diff] == 'R':
                    ans -= 1
            elif not(first == 'G' or second == 'G'):
                ans += G_count[N-1] - G_count[j]
                if j+diff < len(S) and S[j+diff] == 'G':
                    ans -= 1
            else:
                ans += B_count[N-1] - B_count[j]
                if j+diff < len(S) and S[j+diff] == 'B':
                    ans -= 1
print(ans)

Solution (Python):
n=int(input())
s=list(input())
r=s.count("R")
b=s.count("B")
g=s.count("G")
ans=0
herasu=0
for i in range(n):
  if s[i]=="R":
    ab=s[:i].count("B")
    ag=s[:i].count("G")
    bb=b-ab
    bg=g-ag
    l=ab*bg+ag*bb
    ans+=l
  elif s[i]=="B":
    ar=s[:i].count("R")
    ag=s[:i].count("G")
    br=r-ar
    bg=g-ag
    l=ar*bg+ag*br
    ans+=l
  else:
    ab=s[:i].count("B")
    ar=s[:i].count("R")
    bb=b-ab
    br=r-ar
    l=ab*br+ar*bb
    ans+=l
for i in range(1,n):
  for e in range(i):
    if s[i]!=s[e]:
      if i*2-e<=n-1:
        if s[e]!=s[i*2-e] and s[i]!=s[i*2-e]:
          herasu+=1
print(ans-herasu)

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




n_R = S.count('R')
n_G = S.count('G')
n_B = S.count('B')
ans = n_R * n_G * n_B

for b in range(1, N // 2 + 1):
    
    for a in range(N - 2 * b):
        
        i = a
        j = a + b
        k = a + 2 * b

        if k > N - 1:
            continue

        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
S = input()

ans = 0

count_S = {
    "R": [0] * N,
    "G": [0] * N,
    "B": [0] * N,
}

count_S[S[-1]][-1] = 1
for i in reversed(range(N-1)):
    count_S["R"][i] = count_S["R"][i+1]
    count_S["B"][i] = count_S["B"][i+1]
    count_S["G"][i] = count_S["G"][i+1]
    count_S[S[i]][i] += 1












for i in range(0, N-3):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        for c in "RGB":
            if S[i] != c and S[j] != c:
                last_color = c
        ans += count_S[last_color][j+1]
        if j+j-i <= N-1 and S[j+j-i] == last_color:
            ans -= 1

print(ans)

Solution (Python):

import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    S = np.array([0 if c == 'R' else (1 if c == 'G' else 2) for c in args[1]])

    RGB = [np.where(S == 0)[0],
           np.where(S == 1)[0],
           np.where(S == 2)[0]]
    X = [0]*3

    ret = 0
    X[S[0]] += 1
    for j in range(1, n-1):
        X[S[j]] += 1
        a, b = (S[j]+1) % 3, (S[j]-1) % 3
        ret += X[a]*(len(RGB[b])-X[b])
        ret += X[b]*(len(RGB[a])-X[a])
        ret -= len(RGB[a])+len(RGB[b])-len(
            np.unique(np.concatenate([RGB[a]-j, j-RGB[b]]))
        )

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
from math import gcd
from functools import reduce
 
n=int(input())
s=input()
 
ret=0
 
for i,x in enumerate(s):
	for j,y in enumerate(s[i+1:],start=i+1):
		if x==y:
			continue
		if i+(j-i)*2 < n :
			z=s[i+(j-i)*2]
			if y!=z!=x:
				ret-=1
 
dic={}
for x in s:
	if not x in dic: 
		dic[x]=0
	dic[x]+=1
 
for i,x in dic.items():
	for j,y in dic.items():
		for k,z in dic.items():
			if i<j<k:
				ret+=x*y*z
print(ret)

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

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

for i in range(n -2):
    num = 1
    while i + (num)* 2 <= n -1:
        if s[i] != s[i + num] and s[i] != s[i + 2 * num] and s[i + num] != s[i + 2 * num]:
            cnt += 1
        num += 1
print(s.count("R") * s.count("G") * s.count("B") - cnt)

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 = j
        min2 = 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):
n = int(input())
s = str(input())
rr,gg = [],[]
for i in range(0,len(s)):
    if s[i] == "R":
        rr.append(i)
    elif s[i] == "G":
        gg.append(i)

bb = len(s) -len(rr) -len(gg)
ans = len(rr) * len(gg) * bb
for i in range(0,len(rr)):
    for j in range(0,len(gg)):
        che = 0
        a = rr[i] + gg[j]
        if a % 2 == 0:
            if s[a//2] == "B":
                che += -1
        b = min(rr[i],gg[j]) - abs(rr[i]-gg[j])
        if b >= 0:
            if s[b] == "B":
                che += -1
        c = a - b
        if c < n:
            if s[c] == "B":
                che += -1
        ans += che

print(ans)

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

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

ans = r * g * b

for i in range(1, N-1):
    now = S[i]
    for j in range(1, min(i, N-1-i)+1):
        if len(set([S[i-j], now, S[i+j]])) == 3:
            ans -= 1


print(ans)

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

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

d = defaultdict(int)
for i in range(N):
    if S[i] == 'R':
        R.append(i+1)
    elif S[i] == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
        d[i+1] = 1

res = 0
bl = len(B)
sb = set(B)
for r in R:
    for g in G:
        for b in [2*r-g, 2*g-r, g+(r-g)/2]:
            res -= d[b]
        res += bl





print(res)

Solution (Python):

from itertools import permutations as permus

from collections import deque,Counter
import re









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

Sset = Counter(S)
iter = permus(list("RGB")) 

total = Sset["R"]*Sset["G"]*Sset["B"]

Slen = len(S)
hikukazu = 0
for i in range(Slen):
    for num in range(1,Slen//2+1):
        j = i+num
        k = i+num*2
        if k>Slen-1:
            break
        if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
            hikukazu += 1

ans = total - hikukazu
print(ans)

Solution (Python):
from collections import deque, Counter
from itertools import chain, combinations
import json

import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)


M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    lgs = len(gs)
    lbs = len(bs)
    for i in rs:
        j = bisect.bisect_left(gs, i)
        k = bisect.bisect_left(bs, i)
        res += (lgs - j) * (lbs - k)

        for jj in range(j, lgs):
            d = (gs[jj] - i)
            index = bisect.bisect_left(bs, i + 2 * d)
            if index != lbs:
                if bs[index] == i + 2 * d:
                    res -= 1

        for kk in range(k, lbs):
            d = (bs[kk] - i)
            index = bisect.bisect_left(gs, i + 2 * d)
            if index != lgs:
                if gs[index] == i + 2 * d:
                    res -= 1


        
        
        
        
        
        
        
        
        
        
        
        

    return res


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

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
count_r = s.count("R")
count_g = s.count("G")
count_b = s.count("B")
sum = count_r * count_g * count_b
minus = 0

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

print(sum - minus)

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

if len(S) <= 3:
    print(0)
    exit()

sorted_S = sorted(S)
c = [0, 0, 0]
j = 0
for i in range(len(sorted_S) - 1):
    if sorted_S[i] != sorted_S[i + 1]:
        if j == 0:
            c[j] = i + 1
            j += 1
        else:
            c[j] = (i + 1) - c[j - 1]
            j += 1


if j != 2:
    print(0)
    exit()
c[2] = N - c[0] - c[1]

condition1 = c[0] * c[1] * c[2]


condition2 = 0
for i in range(N):
    for d in range(N // 2 + 2):
        j = i + d
        k = j + d
        if j >= N or k >= N:
            continue
        
        if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]):
            condition2 += 1
ans = condition1 - condition2
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)


count = len(R)*len(G)*len(B)
d={}
for i in R:
    for j in G:
        big = max(i,j)
        small = min(i,j)
        
        x = 2*big-small
        y = 2*small-big
        z = (big+small)/2
        
        if x in d:
            d[x] += 1
        else:
            d[x] = 1
        if y in d:
            d[y] += 1
        else:
            d[y] = 1
        if z%1==0:
            if z in d:
                d[z] += 1
            else:
                d[z] = 1
        
        
            
            
            
        
            
            
        
            
            
            
    
for i in B:
    if i in d:
        count -= d[i]
    
print(count)

Solution (Python):
import copy
n = int(input())
s = input()
ans = 0
for i in range(n-2):
    RGB = ['R', 'G', 'B']
    RGB.remove(s[i])
    Rc = s[i+1:].count('R')
    Gc = s[i+1:].count('G')
    Bc = s[i+1:].count('B')
    
    for j in range(i+1, n-1):
        RGBcopy = copy.copy(RGB)
        if not s[j] in RGB:
            if s[j] == 'R':
                Rc = max(0, Rc-1)
            elif s[j] == 'G':
                Gc = max(0, Gc-1)
            else:
                Bc = max(0, Bc-1)
            continue
        
        if s[j] == 'R':
            Rc = max(0, Rc-1)
            RGBcopy.remove('R')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        elif s[j] == 'G':
            Gc = max(0, Gc-1)
            RGBcopy.remove('G')
            if RGBcopy[0] == 'R':
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        else:
            Bc = max(0, Bc-1)
            RGBcopy.remove('B')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
        
        ans += cnt
        

print(ans)

Solution (Python):


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


def colors(target, a, b):
    for i in range(N):
        for j in range(i + 1, N):
            if S[i] == a and S[j] == b:
                target[i] += 1


rg = [0 for _ in range(N)]
colors(rg, 'R', 'G')
gr = [0 for _ in range(N)]
colors(gr, 'G', 'R')

gb = [0 for _ in range(N)]
colors(gb, 'G', 'B')
bg = [0 for _ in range(N)]
colors(bg, 'B', 'G')

rb = [0 for _ in range(N)]
colors(rb, 'R', 'B')
br = [0 for _ in range(N)]
colors(br, 'B', 'R')

ans = 0
for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            ans += rg[j]
            ans += gr[j]

            k = j + j - i
            if k >= N:
                continue
            if S[i] == 'B' and S[j] == 'R' and S[k] == 'G':
                ans -= 1
            if S[i] == 'B' and S[j] == 'G' and S[k] == 'R':
                ans -= 1

    if S[i] == 'R':
        for j in range(i + 1, N):
            ans += gb[j]
            ans += bg[j]

            k = j + j - i
            if k >= N:
                continue
            if S[i] == 'R' and S[j] == 'G' and S[k] == 'B':
                ans -= 1
            if S[i] == 'R' and S[j] == 'B' and S[k] == 'G':
                ans -= 1

    if S[i] == 'G':
        for j in range(i + 1, N):
            ans += rb[j]
            ans += br[j]

            k = j + j - i
            if k >= N:
                continue
            if S[i] == 'G' and S[j] == 'R' and S[k] == 'B':
                ans -= 1
            if S[i] == 'G' and S[j] == 'B' and S[k] == 'R':
                ans -= 1

print(ans)

Solution (Python):
import math
import copy
import sys
import fractions


import decimal
import heapq
import collections
import itertools
import bisect



sys.setrecursionlimit(100001)







def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList




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

ruisekiwa = []
tmp = [0 for _ in range(3)]

for si in s:
    if si == "R":
        tmp[0] += 1
    elif si == "G":
        tmp[1] += 1
    else:
        tmp[2] += 1
    tmpra = copy.deepcopy(tmp)
    ruisekiwa.append(tmpra)

ans = 0
for j in range(1, n - 1):
    sj = 0
    if s[j] == "R":
        sj = 0
    elif s[j] == "G":
        sj = 1
    else:
        sj = 2

    tmpa = ruisekiwa[j - 1]
    tmpb = [0 for _ in range(3)]
    for tmp in range(3):
        tmpb[tmp]=ruisekiwa[n - 1][tmp] - ruisekiwa[j][tmp]

    for a in range(3):
        for b in range(3):
            if a == b:
                continue
            if a == sj or b == sj:
                continue
            ans += tmpa[a] * tmpb[b]

    hikukazu = 0
    for e in range(1, 4000):
        if not(-1< j-e < j+e < n):
            break
        if s[j-e] != s[j] and s[j] != s[j+e] and s[j-e] != s[j+e]:
            hikukazu += 1

    ans -= hikukazu

print(ans)

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

R_count = S.count("R")
B_count = S.count("B")
G_count = S.count("G")

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

print((R_count*B_count*G_count) - t)

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
    else:
        B[i] = 1

sumR = sum(R)
sumG = sum(G)
sumB = sum(B)

cnt = 0
for i in range(N):
    sumR -= R[i]
    sumG -= G[i]
    sumB -= B[i]
    sum_r = sumR
    sum_g = sumG
    sum_b = sumB
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        check_index = j + (j - i)
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if check_index < N:
                    cnt -= B[check_index]
            if S[i] == 'B':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if check_index < N:
                    cnt -= G[check_index]
            if S[i] == 'G':
                cnt += sum_r
                if check_index < N:
                    cnt -= R[check_index]
print(cnt)

Solution (Python):
n=int(input())
s=input()
r=[0]*n
b=[0]*n
g=[0]*n
cb=0
cg=0
cr=0
for i in range(n-1,-1,-1):
  if s[i]=='R':
    cr+=1
  elif s[i]=='G':
    cg+=1
  else:
    cb+=1
  r[i]=cr
  g[i]=cg
  b[i]=cb
  
fr=[0]*n
fb=[0]*n
fg=[0]*n
cb=0
cg=0
cr=0
for i in range(n):
  if s[i]=='R':
    cr+=1
  elif s[i]=='G':
    cg+=1
  else:
    cb+=1
  fr[i]=cr
  fg[i]=cg
  fb[i]=cb
  
p=0
for i in range(1,n-1):
  if s[i]=='B':
    p+=fg[i-1]*r[i+1]+fr[i-1]*g[i+1]
    j=i-1
    k=i+1
    while (j>-1 and k<n):
      if (s[j]=='G' and s[k]=='R' or s[j]=='R' and s[k]=='G'):
        p-=1
      j-=1
      k+=1
      
  if s[i]=='G':
    p+=fb[i-1]*r[i+1]+fr[i-1]*b[i+1]
    j=i-1
    k=i+1
    while (j>-1 and k<n):
      if (s[j]=='B' and s[k]=='R' or s[j]=='R' and s[k]=='B'):
        p-=1
      j-=1
      k+=1
      
  if s[i]=='R':
    p+=fb[i-1]*g[i+1]+fg[i-1]*b[i+1]
    j=i-1
    k=i+1
    while (j>-1 and k<n):
      if (s[j]=='B' and s[k]=='G' or s[j]=='G' and s[k]=='B'):
        p-=1
      j-=1
      k+=1
  
print(p)

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)
    else:
        pass

count = 0
for i in range(n):
    x = s[i]
    for sa in range(n):
        j = i + sa
        k = j + sa
        if j<n:
            y = s[j]
        else:
            break
        if k<n:
            z = s[k]
        else:
            break
        if x!=y and y!=z and z!=x:
            count += 1

ans = len(r) * len(g) * len(b) - count
print(ans)

Solution (Python):
def main():
    n=int(input())
    s=input()
    r,g,b=[],[],[]
    if n<=3:
        print(0)
    else:
        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 range(len(r)):
            for j in range(len(g)):
                if (r[i]+g[j])%2==0 and s[(r[i]+g[j])//2]=="B":
                        ans-=1
        for i in range(len(g)):
            for j in range(len(b)):
                if (g[i]+b[j])%2==0 and s[(g[i]+b[j])//2]=="R":
                        ans-=1
        for i in range(len(r)):
            for j in range(len(b)):
                if (r[i]+b[j])%2==0 and s[(r[i]+b[j])//2]=="G":
                        ans-=1
        print(ans)
if __name__=="__main__":
    main()

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 b:
    for j in g:
        a = max(i,j)
        u = min(i,j)
        if a*2-u >= 0 and a*2-u < n :
            if s[a*2-u] == 'R':
                ans -= 1
        if 2*u-a >= 0 and 2*u-a < n:
            if s[2*u-a] == 'R':
                ans -= 1
        if (a-u)%2 == 0 and u+(a-u)/2 >= 0 and u+(a-u)/2 < n:
            if s[u+(a-u)//2] == 'R':
                ans -= 1
print(ans)

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

r = list()
g = list()
b = list()
for idx, si in enumerate(s):
    if si == 'R':
        r.append(idx)
    elif si == 'G':
        g.append(idx)
    else:
        b.append(idx)

ret = 0
bi_cnt = len(b)
for ri in r:
    for gi in g:
        cnt = 0
        try:
            dan1 = 2*ri - gi
            if (dan1 > 0) & (s[dan1] == 'B'):
                cnt += 1
        except:
            pass

        try:
            dan3 = 2*gi - ri
            if (dan3 > 0) & (s[dan3] == 'B'):
                cnt += 1
        except:
            pass

        try:
            dan2, mod = divmod(ri+gi, 2)
            if (mod == 0) & (s[dan2] == 'B'):
                cnt += 1
        except:
            pass

        ret += (bi_cnt - cnt)

print(ret)

Solution (Python):
from collections import Counter


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


count = 0

for start in range(N):
  offset = 1
  while True: 
      middle = start + offset
      end = middle + offset

      if end >= N:
          break

      s = {S[start], S[middle], S[end]}

      if len(s) == 3:
          count += 1

      offset += 1

c = Counter(S)

if len(c) != 3:
  print(0)
else:  
  x, y, z = c.values()
  print(x * y * z - count)

Solution (Python):
n=int(input())
s=input()
a=n*(n-1)*(n-2)//6
r=s.count("R")
g=s.count("G")
b=s.count("B")
ra=r*(r-1)*(r-2)//6+(r*(r-1)//2)*(n-r)
ga=g*(g-1)*(g-2)//6+(g*(g-1)//2)*(n-g)
ba=b*(b-1)*(b-2)//6+(b*(b-1)//2)*(n-b)
sum=0
for i in range((n-3)//2+1):
	
	for j in range(n-(2*i+3)+1):
		
		if not (s[j]==s[j+i+1] or s[j]==s[j+2*(i+1)] or s[j+i+1]==s[j+2*(i+1)] ):
			sum+=1

print(a-(ra+ga+ba)-sum)

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

def rest(a):
    if a == "R":
        return(["G", "B"])
    elif a == "G":
        return(["R", "B"])
    else:
        return (["R", "G"])

for i in range(n - 3):
    rest1 = rest(s[i])[0]
    rest2 = rest(s[i])[1]
    ans = ans + s[i + 1:].count(rest1) * s[i + 1:].count(rest2)

    j = i + 1
    k = 1
    while True:
        if (s[j] == rest1 and s[j + k] == rest2) or (s[j] == rest2 and s[j + k] == rest1):
            ans = ans - 1
        k = k + 1
        j = i + k
        if j + k > n - 1:
            break

print(ans)

Solution (Python):
n = int(input())
s = input()
dp = [[0] * 7 for i in range(4001)]

for i in range(n):
    if s[i] == 'R':
        dp[i + 1][0] = dp[i][0] + 1
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][1] + dp[i][3]
        dp[i + 1][4] = dp[i][4]
        dp[i + 1][5] = dp[i][2] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][4]
    elif s[i] == 'G':
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1] + 1
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][0] + dp[i][3]
        dp[i + 1][4] = dp[i][2] + dp[i][4]
        dp[i + 1][5] = dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][5]
    else:
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2] + 1
        dp[i + 1][3] = dp[i][3]
        dp[i + 1][4] = dp[i][1] + dp[i][4]
        dp[i + 1][5] = dp[i][0] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][3]
    for j in range(1, i // 2 + 1):
        if s[i] != s[i - j] and s[i] != s[i - j * 2] and s[i - j] != s[i - 2 * j]:
            dp[i + 1][6] -= 1
print(dp[n][6])

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

if n < 2:
    print(0)
    exit(0)

cum_r = [0] * (n + 1)
cum_g = [0] * (n + 1)
cum_b = [0] * (n + 1)

for i in range(n):
    cum_r[i + 1] = cum_r[i]
    cum_g[i + 1] = cum_g[i]
    cum_b[i + 1] = cum_b[i]
    if s[i] == "R":
        cum_r[i + 1] += 1
    elif s[i] == "G":
        cum_g[i + 1] += 1
    else:  
        cum_b[i + 1] += 1

sum = 0
for i in range(n - 2):
    for k in range(n - 1, i + 1, -1):
        if s[i] == s[k]:
            continue

        
        if (s[i] == "R" and s[k] == "G") or (s[i] == "G" and s[k] == "R"):
            sum += cum_b[k] - cum_b[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "B":
                sum -= 1
        elif (s[i] == "R" and s[k] == "B") or (s[i] == "B" and s[k] == "R"):
            sum += cum_g[k] - cum_g[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "G":
                sum -= 1
        else:
            sum += cum_r[k] - cum_r[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "R":
                sum -= 1

print(sum)

Solution (Python):

N = int(input())


S = input()

res = 0

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

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

BN = len(B)

for i in R:
    for j in G:
        res += BN
        ii,jj = i,j
        if ii > jj:
            ii,jj = jj,ii
        if ii - (jj-ii) >= 0:
            if S[ii-(jj-ii)] == "B":
                res -= 1
        if (abs(jj-ii)) % 2 == 0:
            if S[(jj+ii) // 2] == "B":
                res -= 1
        if jj + (jj-ii) <= N-1:
            if S[jj+(jj-ii)] == "B":
                res -= 1
print(res)