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()
bad = [0] * n
pair = 0
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair += 1
        if 2 * i - j >= 0:
            bad[2 * i - j] += 1
        if (j - i) % 2 == 0:
            bad[(i + j) // 2] += 1
        if 2 * j - i < n:
            bad[2 * j - i] += 1
if s[-1] == "B":
    b_num.append(n - 1)

for k in b_num:
    ans += pair - bad[k]
print(ans)

Solution (Python):
n = int(input())
r = []
g = []
b = []
s = input()
blen = 0
glen = 0
rlen = 0
for i in range(n):
    if(s[i]=='R'):
        r.append(i)
        rlen += 1
    elif(s[i]=='G'):
        g.append(i)
        glen += 1
    else:
        b.append(i)
        blen += 1

ans = 0
minus = 0
t = 0
for i in r:
    for j in g:
        mn = min(i,j)
        mx = max(i,j)

        mid2 = mx + (mx-mn)
        mid3 = mn - (mx-mn)

        if((mx-mn)%2==0):
            mid1 = mn + (mx-mn)//2
            if(mid1 >= 0 and mid1 < n):
                if(s[mid1]=='B'):
                    minus += 1
        if(mid2 >= 0 and mid2 < n):
            if(s[mid2] == 'B'):
                minus += 1
                

                
        if(mid3 >= 0 and mid3 < n):
            if(s[mid3]=='B'):
                minus += 1
                

        t += blen
                

ans = rlen * glen * blen
ans -= minus
print(ans)

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

r,g,b,rg,gb,br,rgb = 0,0,0,0,0,0,0,

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


cnt = 0
for i in range(N):
    s1 = S[i]
    for j in range(1, (N-i-1)//2+1):
        s2, s3 = S[i+j], S[i+j*2]
        if s1 != s2 and s2 != s3 and s1 != s3:
            cnt += 1

print(rgb-cnt)

Solution (Python):
import itertools
import bisect
n = int(input())
s = input()
listR = []
listG = []
listB = []
counter = 0
for i in range(len(s)):
    if s[i] == "R":
        listR.append(i)
    elif s[i] == "G":
        listG.append(i)
    elif s[i] == "B":
        listB.append(i)

listB = sorted(listB)
def binary_search_loop(data, target):
    imin = 0
    imax = len(data)-1
    while imin <= imax:
        imid = imin + (imax - imin) // 2
        if target == data[imid]:
            return True
        elif target < data[imid]:
            imax = imid -1
        else:
            imin = imid + 1
    return False
for bits in itertools.product(listR, listG):
    diff = max(bits) - min(bits)
    remover = 0
    if sum(bits) % 2 == 0:
        mid = int(sum(bits) / 2)
    else:
        mid = 1000000
    if binary_search_loop(listB, max(bits) + diff):
        remover += 1
    if binary_search_loop(listB, min(bits) - diff):
        remover += 1
    if binary_search_loop(listB, mid):
        remover += 1
    counter += (len(listB) - remover)
print(counter)

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

x = S.count("R")
y = S.count("G")

total = x*y*(N-x-y)

count = 0
for i in range(N):
    texta = S[i]
    for j in range(i,int((N-1+i)/2)+1):
        if S[j] != texta:
            textb = S[j]
            if S[2*j-i] != texta and S[2*j-i] !=textb:
                count += 1

print(total - count)

Solution (Python):



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

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

    temp = r * g * b
    minus = 0
    for i in range(1, n):
        for j in range(1, min(i + 1, abs(n - i))):
            if len(set([s[i], s[i - j], s[i + j]])) == 3:
                minus += 1

    return temp - minus


if __name__ == '__main__':
    print(main())

Solution (Python):
import math

def solve():
    N = int(input())
    S = input()
    R = []
    G = []
    B = []

    
    for i in range(len(S)):
        if S[i] == 'R':
            R.append(i)
        elif S[i] == 'G':
            G.append(i)
        elif S[i] == 'B':
            B.append(i)

    
    lR = len(R)
    lG = len(G)
    lB = len(B)
    RGBmap = []
    for i in range(len(S)):
        if S[i] == 'R':
            lR -= 1
        elif S[i] == 'G':
            lG -= 1
        elif S[i] == 'B':
            lB -= 1
        RGBmap.append([lR, lG, lB])

    
    sum = 0
    for i in range(len(S)):
        if S[i] == 'R':
            a1 = G
            a2 = B
            b1 = 2
            b2 = 1
            c1 = 'B'
            c2 = 'G'
        elif S[i] == 'G':
            a1 = R
            a2 = B
            b1 = 2
            b2 = 0
            c1 = 'B'
            c2 = 'R'
        else:
            a1 = G
            a2 = R
            b1 = 0
            b2 = 1
            c1 = 'R'
            c2 = 'G'
        for a in a1:
            if i < a:
                sumb = RGBmap[a][b1]
                if sumb == 0:
                    continue
                if a + (a - i) < len(S) and S[a + (a - i)] == c1:
                    sumb -= 1
                sum += sumb
        for a in a2:
            if i < a:
                sumb = RGBmap[a][b2]
                if sumb == 0:
                    continue
                if a + (a - i) < len(S) and S[a + (a - i)] == c2:
                    sumb -= 1
                sum += sumb
    print(sum)
 

if __name__ == '__main__':
    solve()

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

ans = 0

for i in range(1,N-1):
    if S[i] == 'R':
        leftG = S[:i].count('G')
        rightG = S[i+1:].count('G')
        leftB = S[:i].count('B')
        rightB = S[i+1:].count('B')

        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'G' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'G'):
                ans -= 1

for i in range(1,N-1):
    if S[i] == 'G':
        leftG = S[:i].count('R')
        rightG = S[i+1:].count('R')
        leftB = S[:i].count('B')
        rightB = S[i+1:].count('B')
            
        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
                ans -= 1

                
for i in range(1,N-1):
    if S[i] == 'B':
        leftG = S[:i].count('R')
        rightG = S[i+1:].count('R')
        leftB = S[:i].count('G')
        rightB = S[i+1:].count('G')
            
        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'R' and S[i+j] == 'G') or (S[i-j] == 'G' and S[i+j] == 'R'):
                ans -= 1
            
print(ans)

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

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

cnt = {'R': 0, 'G': 0, 'B': 0}
for c in S:
    cnt[c] += 1

print(cnt['R'] * cnt['G'] * cnt['B'] - same)

Solution (Python):
from collections import Counter

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

c = Counter(s)
ans = c["R"]*c["G"]*c["B"]

for i in range(n-1):
    for j in range(i+1, (n-i-1)//2+i+1):
        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 math
N = int(input())
S = str(input())
r=0
g=0
b=0
error=0
n=0

if N%2==0:
    n=(N//2)-1
else:
    n=N//2

for i in range(N):
    if S[i]=='R':
        r+=1
    if S[i]=='G':
        g+=1
    if S[i]=='B':
        b+=1

for p in range(n+1):
    for i in range(N-(p*2)):
        if S[i]==S[i+p] or S[i+p]==S[i+2*p] or S[i]==S[i+2*p]:
            continue
        else:
            error+=1
d=r*g*b

print(d-error)

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

count = 0
r=0
g=0
b=0
for i in range(0,n):
  if s[i] == 'R':
    r = r + 1
  if s[i]=='G':
    g = g+1
  if s[i]=='B':
    b = b+1
    
for i in range(0,n):
  ii = s[i]
  for j in range(i+1, n):
    jj = s[j]
    if ii==jj:
      continue
    if (2*j-i)<=(n-1):
      if ii!=s[2*j-i] and jj!=s[2*j-i]:
        count = count + 1
print(r*g*b-count)

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

N = I()
S = input()

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

r = 0
g = 0
b= 0
for i in range(N):
    if S[N-i-1] == "R":
        r += 1
        R[N-i-1] = r
    else:
        R[N-i-1] = r
    
    if S[N-i-1] == "G":
        g += 1
        G[N-i-1] = g
    else:
        G[N-i-1] = g

    if S[N-i-1] == "B":
        b += 1
        B[N-i-1] = b
    else:
        B[N-i-1] = b

ans = 0
for i, s in enumerate(S):
    if i > N-3:
        break

    if s == "R":
        ans += G[i+1] * B[i+1]
    if s == "G":
        ans += R[i+1] * B[i+1]
    if s == "B":
        ans += R[i+1] * G[i+1]
    
    j = 1
    while i + j*2 < N:
        if s == "R":
            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 == "G":
            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 == "B":
            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 bisect import bisect_right, bisect_left
from itertools import permutations


def main():
    n = int(input())
    s = input()
    ind = [[] for _ in range(3)]
    for i in range(n):
        if s[i] == "R":
            ind[0].append(i)
        elif s[i] == "G":
            ind[1].append(i)
        else:
            ind[2].append(i)
    length = [len(ind[i]) for i in range(3)]
    ind_set = [set(ind[i]) for i in range(3)]
    answer = 0
    for p1, p2, p3 in permutations([0, 1, 2]):
        for j in ind[p2]:
            i = bisect_right(ind[p1], j)
            k = bisect_left(ind[p3], j)
            if length[p1] < i or length[p3] <= k:
                continue
            answer += i * (length[p3] - k)
            for ii in ind[p1]:
                target = 2 * j - ii
                t_ind = bisect_left(ind[p3], target)
                if target in ind_set[p3] and k <= t_ind < length[p3]:
                    answer -= 1
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    rdict={}
    gdict={}
    bdict={}
    ans=0
    for i in range(n):
        if s[i]=='R':
            r.append(i)
            rdict[2*i]=2*i
        if s[i]=='G':
            g.append(i)
            gdict[2*i]=2*i
        if s[i]=='B':
            b.append(i)
            bdict[2*i]=2*i
    ans=len(r)*len(g)*len(b)
    for i in r:
        for j in g:
            if i+j in bdict:
                ans-=1
    for i in g:
        for j in b:
            if i+j in rdict:
                ans-=1
    for i in b:
        for j in r:
            if i+j in gdict:
                ans-=1
    print(ans)
    
if __name__ == '__main__':
    main()

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

s = input()


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

le = []
ri = []
past = [0, 0, 0]
unt = copy.copy(rgb)

for i in range(n):
    p = copy.copy(past)
    u = copy.copy(unt)
    if s[i] == 'R':
        unt[0] -= 1
        u[0] -= 1
        le.append(p)
        ri.append(u)
        past[0] += 1
    elif s[i] == 'G':
        unt[1] -= 1
        u[1] -= 1
        le.append(p)
        ri.append(u)
        past[1] += 1
    else:
        unt[2] -= 1
        u[2] -= 1
        le.append(p)
        ri.append(u)
        past[2] += 1
ans = 0
for i in range(n):
    if s[i] == 'R':
        ans += le[i][1]*ri[i][2] + le[i][2]*ri[i][1]
    elif s[i] == 'G':
        ans += le[i][0]*ri[i][2] + le[i][2]*ri[i][0]
    else:
        ans += le[i][0]*ri[i][1] + le[i][1]*ri[i][0]

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

print(ans)

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

num_r = S.count('R')
num_g = S.count('G')
num_b = N - num_r - num_g
C = [[num_r, num_g, num_b] for _ in range(N)]
r_cnt, g_cnt, b_cnt = 0, 0, 0

for i in range(N):
    if S[i] == 'R':
        r_cnt += 1
    elif S[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    C[i][0] -= r_cnt
    C[i][1] -= g_cnt
    C[i][2] -= b_cnt

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] != S[j]:
            if 'R' not in [S[i], S[j]]:
                ans += C[j][0]
                if j+(j-i) < N and S[j+(j-i)]=='R':
                    ans -= 1
            if 'G' not in [S[i], S[j]]:
                ans += C[j][1]
                if j+(j-i) < N and S[j+(j-i)]=='G':
                    ans -= 1
            if 'B' not in [S[i], S[j]]:
                ans += C[j][2]
                if j+(j-i) < N and S[j+(j-i)]=='B':
                    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)
base = len(R) * len(G) * len(B)
B = set(B)
no_num = 0
for r in R:
    for g in G:
        y = max(r,g)
        x = min(r,g)
        tmp = int((x+y)/2)
        if (x+y)%2==0 and (tmp in B):
            no_num += 1
        tmp = 2*y-x
        if tmp in B:
            no_num += 1
        tmp = 2*x-y
        if tmp in B:
            no_num += 1
print(base-no_num)

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

    c = 0

    r_indices = tuple([i for i in range(N) if S[i] == 'R'])
    g_indices = tuple([i for i in range(N) if S[i] == 'G'])
    b_indices = tuple([i for i in range(N) if S[i] == 'B'])

    
    
    

    avoid = 0

    for center in range(1, N - 1):
        
        
        

        for width in range(1, min(center + 1, N - center)):
            
            little = center - width
            great = center + width
            
            
            

            if S[little] != S[great] and S[center] != S[great] and S[little] != S[center]:
                avoid += 1

    total = len(r_indices) * len(g_indices) * len(b_indices)

    print(total - avoid)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    s=input()
    c_s=Counter(s)
    sum_a=0
    for diff in range(1,(n-1)//2+1):
        for i in range(n-2*diff):
            if s[i]!=s[i+diff] and s[i+diff]!=s[i+2*diff] and s[i]!=s[i+2*diff]:
                sum_a+=1
    print(c_s['R']*c_s['G']*c_s['B']-sum_a)

if __name__=='__main__':
    main()

Solution (Python):

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

remain = [[0,0,0]]
for i in range(n):
    r, g, b = remain[-1]
    if s[-i-1] == "R":
        r = r+1
    elif s[-i-1] == "G":
        g = g + 1
    elif s[-i-1] == "B":
        b = b + 1

    remain.append([r,g,b])

total = 0

rgb="RGB"
for i in range(n-2):
    first = s[i]
    search = []
    for j in range(3):
        if j==rgb.find(first):
            continue
        else:
            search.append(j)
    total = total + (remain[-i-2][search[0]]*remain[-i-2][search[1]])
    try:
        for j in range(1,n):
            if s[i] != s[i+j] and s[i] != s[i+j+j] and s[i+j] != s[i+j+j]:
                total -= 1
    except:
        pass


print(total)

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


dp = {}

ans = 0

for i in range(n - 2):
    c_i = S[i]
    for j in range(i + 1, n - 1):
        if S[j] == c_i:
            continue
        c_j = S[j]
        d_ij = j - i
        if (c_i, j) in dp:
            ans += dp[(c_i, j)]
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    ans -= 1
        else:
            tmp = 0
            for k in range(j + 1, n):
                c_k = S[k]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp += 1
            dp[(c_i, j)] = tmp
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp -= 1
            ans += tmp
print(ans)

Solution (Python):
import sys
def getch(i,j):
  rgb = {'R':0,'G':1,'B':2}
  for k in rgb:
    if i == k or j == k:
      continue
    return k,rgb[k]
N = int(input())
S = list(input())
l = len(S)
rgb_num = [[0,0,0] for i in range(l)]
for i in range(l):
  for j in range(i+1,l):
    if S[j] == 'R':
      rgb_num[i][0] += 1
    elif S[j] == 'G':
      rgb_num[i][1] += 1
    else:
      rgb_num[i][2] += 1

su = 0

for i in range(l):
  si = S[i]
  for j in range(i+1,l):
    sj = S[j]
    if si != sj:
      k = 2*(j+1) - (i+1)-1
      sk,n = getch(si,sj)
      if k < l and S[k] == sk:
        su += rgb_num[j][n]-1
      else:
        su += rgb_num[j][n]
print(su)

Solution (Python):
n=int(input())
s=input()
ans=0
r=[]
g=[]
b=[]
for i in range(n):
  if s[i]=="R":
    r.append(i+1)
  elif s[i]=="G":
    g.append(i+1)
  elif s[i]=="B":
    b.append(i+1)
count=0
j=0
rcount=[]
gcount=[]
sb=set(b)
for i in r:
  for j in g:
    if j>i and j+(j-i) in sb:
      count+=1
    elif i>j and i+(i-j) in sb:
      count+=1
sr=set(r)
for i in g:
  for j in b:
    if j>i and j+(j-i) in sr:
      count+=1
    elif i>j and i+(i-j) in sr:
      count+=1
sg=set(g)
for i in r:
  for j in b:
    if j>i and j+(j-i) in sg:
      count+=1
    elif i>j and i+(i-j) in sg:
      count+=1

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

Solution (Python):

def combi_2(n):
    if n < 2:
        return 0
    return n * (n-1) // (2 * 1)

def combi_3(n):
    if n < 3:
        return 0
    return n * (n-1) * (n-2) // (3 * 2 * 1)


def main():

    n = int(input())
    s = input()
    
    numR = s.count("R")
    numG = s.count("G")
    numB = s.count("B")
    total = combi_3(len(s))

    red_pair = combi_2(numR) * numB + combi_2(numR) * numG
    gre_pair = combi_2(numG) * numR + combi_2(numG) * numB
    blu_pair = combi_2(numB) * numR + combi_2(numB) * numG
    three_red = combi_3(numR)
    three_gre = combi_3(numG)
    three_blu = combi_3(numB)


    first_error = red_pair + gre_pair + blu_pair + three_red + three_gre + three_blu


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

    print(total - first_error - second_error)











if __name__ == '__main__':
    pass
    main()

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

R = [None] * S.count("R")
G = [None] * S.count("G")
B = [None] * S.count("B")

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

c = 0
for r in R:
    for g in G:
        for d in [(r + g) / 2, 2 * r - g, 2 * g - r]:
            if d in B:
                c += 1

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

Solution (Python):
ans=0
n=int(input())
k=str(input())
r=k.count("R")
b=k.count("B")
g=k.count("G")
ans+=r*b*g
    
for i in range(n-2):
  ii=k[i]
  for j in range(i+1,(n-i)//2+1+i):
    jj=k[j]
    l=j+(j-i)
    
    if jj!=ii and l<n:
      
      ll=k[l]
      if ii!=ll and jj!=ll:
        ans-=1
          
print(ans)

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

R_count = [0]*n
G_count = [0]*n
B_count = [0]*n

Rc = 0
Gc = 0
Bc = 0

for i in range(1,n+1):
    if s[-i] == "R":
        Rc += 1
    elif s[-i] == "G":
        Gc += 1
    else:
        Bc += 1
        
    R_count[-i] = Rc
    G_count[-i] = Gc
    B_count[-i] = Bc

ans = 0

for i in range(n):
    if s[i] == "R":
        for j in range(i+1,n-1):
            if  s[j] == "G":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "B":
                    ans -= 1
            if s[j] == "B":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "G":
                    ans -= 1               

    if s[i] == "B":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "G":
                    ans -= 1
            if s[j] == "G":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "R":
                    ans -= 1

    if s[i] == "G":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "B":
                    ans -= 1
            if s[j] == "B":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "R":
                    ans -= 1
    
print(ans)

Solution (Python):
import sys, math
def input(): return sys.stdin.readline().strip()


def func(R, G, B, lR, lG, lB):
    pG = lG - 1
    pB = lB - 1
    sumR = 0
    sumG = 0

    for i in range(lR - 1, -1, -1):
        while pG >= 0 and R[i] < G[pG]:
            while pB >= 0 and G[pG] < B[pB]:
                pB -= 1
            sumG += (lB - pB - 1)
            pG -= 1
        sumR += sumG
    return sumR


def main():
    N = int(input())
    S = input()
    
    R = []
    G = []
    B = []
    lR = 0
    lG = 0
    lB = 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
    
    ans = 0
    ans += func(R, G, B, lR, lG, lB)
    
    ans += func(R, B ,G, lR, lB, lG)
    
    ans += func(G, R, B, lG, lR, lB)
    
    ans += func(G, B, R, lG, lB, lR)
    
    ans += func(B, R, G, lB, lR, lG)
    
    ans += func(B, G, R, lB, lG, lR)
    

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

    print(ans - subtract)
    


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
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 li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]



N = ii()
S = input()
h = [0]*N
h = 'RGB'*1000


'''
cnt = -1
ans = 0
for i in range(N):
    cnt = int(h[i])
    for j in range(i+1, N):
        num = cnt + int(h[j])
        if num == 3 or num == 9 or num==8:
            for k in range(j+1, N):
                if num + int(h[k]) == 10 and k!=2*j-i:
                    ans += 1
'''



c_r = c_g = c_b = 0
h = ''
for i, a in enumerate(S):
    if a == 'R':
        c_r += 1
        h += '1'
    if a == 'G':
        c_g += 1
        h += '2'
    if a == 'B':
        c_b += 1
        h += '7'

ans = c_r * c_g * c_b

for i in range(N):
    for j in range(i+1, N):
        if 2*j-i<N and int(h[i]) + int(h[j]) + int(h[2*j-i]) == 10:
            ans -= 1
print(ans)

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

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

ok = len(R) * len(G) * len(B)
for i in range(1,N):
  for j in range(N//2+1):
    if i - j < 0 or i + j >= N:
      break
    else:
      if S[i-j] != S[i] and S[i] != S[i+j] and S[i-j] != S[i+j]:
        ok -= 1

print(ok)

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

Solution (Python):
from collections import Counter

n = int(input())
s = list(input())
c = Counter(s)
total = c['R'] * c['G'] * c['B']
count = 0
for i in range(n):
	for j in range(i + 1, n):
			k = 2 * j - i
			if k < n and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
				count += 1
print(total - count)

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd

N = int(input())
S = 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)
    else:
        b_list.append(i)



R_count = len(r_list)
G_count = len(g_list)
B_count = len(b_list)


b_num_list = [0 for i in range(N)]
for i in range(B_count):
    b_num_list[b_list[i]] += 1

    

counter = R_count * G_count * B_count
NG_list = []
for i in range(R_count):
    for j in range(G_count):
        r = r_list[i]
        g = g_list[j]
        hit_count = 0
        
        
        if(g+g-r >= 0 and g+g-r <= N-1): 
            
            if(b_num_list[g+g-r] == 1):
                hit_count += 1
        
        if( (g+r)%2 == 0 and b_num_list[int((g+r)/2)] == 1):
            hit_count+=1
        
        if(r+r-g >= 0 and r+r-g <= N-1 ):
            if(b_num_list[r+r-g] == 1):
                hit_count+=1
        counter -= hit_count
print(counter)

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

total = r_cnt*g_cnt*b_cnt

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

print(total)

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

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

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

ans = r * g * b - sub
print(ans)

Solution (Python):
import sys,math
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
S = list(input())

ANS = 0
dp = [None]*N
r,g,b = 0,0,0
for i in range(N):
    elm = S[i]
    if elm=='R':
        r += 1
    elif elm=='G':
        g += 1
    else:
        b += 1
    dp[i] = [r,g,b]

TTL = dp[-1]
for i in range(N):
    f = S[i]
    for j in ['R','G','B']:
        if j==f:
            continue

        for k in range(i+1,N):
            elm = S[k]
            if elm!=j:
                continue
            for l in ['R','G','B']:
                if l==f or l==elm:
                    continue

                if l=='R':
                    d = 0
                elif l=='G':
                    d = 1
                else:
                    d = 2
                ANS += TTL[d] - dp[k][d]

                if k+k-i<N and S[k+k-i]==l:
                    ANS -= 1
print(ANS)

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

from collections import defaultdict 


counter = [defaultdict(int) for _ in range(N)]
for n in range(N):
    for i in range(n, N):
        counter[n][S[i]] += 1


ans = 0
for x in range(N):
    for y in range(x + 1, N):
        if S[x] != S[y]:
            length = y - x
            if "R" not in (S[x], S[y]):
                color = "R"
            elif "G" not in (S[x], S[y]):
                color = "G"
            else:
                color = "B"

            if (y + 1) < N:
                ans += counter[y + 1][color]

            if (y + length) < N and S[y + length] == color:
               ans -= 1

print(ans)

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

import random
from collections import Counter

res = 1
val = Counter(s).values()
if len(val) < 3:
    print(0)
    exit()

for i in val:
    res *= i

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

print(res - count)

Solution (Python):
n = int(input())
s = list(input())
cnt = 0
for i in range(1,n-1):
    if s[i] == 'R':
        cnt += (s[:i].count('G'))*(s[i+1:].count('B')) + (s[:i].count('B'))*(s[i+1:].count('G'))
    if s[i] == 'G':
        cnt += (s[:i].count('R'))*(s[i+1:].count('B')) + (s[:i].count('B'))*(s[i+1:].count('R'))
    if s[i] == 'B':
        cnt += (s[:i].count('G'))*(s[i+1:].count('R')) + (s[:i].count('R'))*(s[i+1:].count('G'))
for i in range(1,n-1):
    if s[i] == 'R':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='G':
                cnt -=1
    if s[i] == 'G':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'R' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='R':
                cnt -=1
    if s[i] == 'B':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='R':
                cnt -=1
            if s[i-j] == 'R' and s[i+j]=='G':
                cnt -=1
print(cnt)

Solution (Python):
n=int(input())
s=input()
l=[[] for _ in range(3)]
for i in range(n):
    if s[i]=='R':
        l[0].append(i)
    elif s[i]=='G':
        l[1].append(i)
    else:
        l[2].append(i)
ans=len(l[0])*len(l[1])*len(l[2])
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n:
            if s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
                ans-=1
print(ans)