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




R  =[]
G  =[]
B  =[]
for i, s in enumerate(S):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)
answer = len(R)*len(G)*len(B)
hiku = 0

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

Solution (Python):
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
z = a*b*c
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)
x = 0
for i in R:
  for j in G:
    if 0 <= 2*j-i <= n-1 and s[2*j-i]=="B":
      x+=1
    if 0 <= 2*i-j <= n-1 and s[2*i-j]=="B":
      x+=1
    if (i+j)%2==0 and s[(i+j)//2]=="B":
      x+=1
print(z-x)

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

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Solution (Python):
N=int(input())
S=input()
count=0
R=0
G=0
B=0
for i in range(N):
  if S[i]=="R":
    R+=1
  if S[i]=="G":
    G+=1
  if S[i]=="B":
    B+=1
count=0
for i in range(N-2):
  for j in range(i,N-1):
    k=j+j-i
    try:
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
        count+=1
    except:
      pass
    
print(R*G*B-count)

Solution (Python):
import bisect
n = int(input())
s = input()
rl = []
gl = []
bl = []
for i in range(n):
    if s[i] == "R": rl.append(i+1)
    elif s[i] == "G": gl.append(i+1)
    else: bl.append(i+1)

ans = len(rl)*len(gl)*len(bl)

for i in rl:
    for j in gl:
        lb = len(bl)
        if (i+j)%2==0:
            index = bisect.bisect_left(bl, (i+j)//2)
            if lb>index and bl[index] == ((i+j)//2):
                ans -= 1
        if 2*i-j>0:
            index = bisect.bisect_left(bl, 2*i-j)
            if lb>index and bl[index] == (2*i-j):
                ans -= 1
        if 2*j-i>0:
            index = bisect.bisect_left(bl, 2*j-i)
            if lb>index and bl[index] == (2*j-i):
                ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
noCount = {'R': 0, 'G': 0, 'B': 0}
minusCount = 0
for i in S:
    if i == 'R':
        noCount['R'] += 1
    elif i == 'G':
        noCount['G'] += 1
    elif i == 'B':
        noCount['B'] += 1
RGB = noCount['R']*noCount['G']*noCount['B']

for step in range(1, (N+1)//2):
    for cenPos in range(step, N-step):
        if S[cenPos - step] != S[cenPos] and S[cenPos] != S[cenPos + step] and S[cenPos - step] != S[cenPos + step]:
            minusCount += 1
print(RGB - minusCount)

Solution (Python):
n = int(input())
s = str(input())
count = 0
sum_RGB = s.count("R") * s.count("G") * s.count("B")
for i in range(n):
    for j in range(i, n):  
        
        k = 2*j - i
        if k < n:
            a = (s[i], s[j], s[k])
            
            a_set = set(a)
            
            if len(a_set) == 3:
                count += 1

print(sum_RGB - count)

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-1):
  for j in range(i+1,n):
    k = j*2 - i
    if k >= n:
      continue
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      ans -= 1
      
print(ans)

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import Counter
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def main():
    N = si()
    S = ss()
    rr = 0
    gg = 0
    bb = 0
    ans = 0
    for i in range(N):
        if S[i] == 'R':
            rr += 1
        elif S[i] == 'B':
            bb += 1
        elif S[i] == 'G':
            gg += 1
    ans = rr*gg*bb
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] != S[j] and 2*j-i < N and S[i] != S[2*j-i] and S[j] != S[2*j-i]:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
r_cnt = s.count("R")
g_cnt = s.count("G")
b_cnt = s.count("B")

sub = r_cnt * g_cnt * b_cnt

for i in range(n):
    for d in range(n):
        j = i+d
        k = j+d
        if k >= n:
            break

        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            sub-=1
print(sub)

Solution (Python):
def resolve():
    N = int(input())
    S = input()
    rc = S.count('R')
    bc = S.count('B')
    gc = S.count('G')

    cnt = rc * bc * gc

    for i in range(0, N):
        for d in range(0, N):
            j = i + d
            k = j + d
            if N < k+1:
                break
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                cnt -= 1
    print(cnt)

if __name__ == '__main__':
    resolve()

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

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

for k in range(n):
    if s[k] =="R":
        r.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"G", "B"}:
                sums -=1
    elif s[k] =="G":
        g.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"R", "B"}:
                sums -=1
    else:
        b.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"R", "G"}:
                sums -=1

sums +=len(r)*len(g)*len(b)
print(sums)

Solution (Python):
N = int(input())
S = input()
r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b

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

print(ans)

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

bad_list = ['RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR']
res = S.count("R") * S.count("G") * S.count("B")
for (i, s) in enumerate(S):
  
  for j in range(((i+1)//2)+1)[1:]:
    if S[i::-j][:3] in bad_list:
      res -= 1


print(res)

Solution (Python):
n = int(input())
s = input()
r = []
g = []
b = []

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

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

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
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
ans = rgb[0] * rgb[1] * rgb[2]
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
            ans -= 1
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
s = STR()
t = Counter(s)
ans = t['R'] * t['G'] * t['B']
for i in range(n):
    for j in range(i + 1, n):
        k = j + (j - i)
        if k < n and set([s[i], s[j], s[k]]) == set(['R', 'G', 'B']):
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
R = s.count("R")
G = s.count("G")
B = s.count("B")
point = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        k = j + j-i
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            point += 1
print(R*G*B-point)

Solution (Python):
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:j-i>>1]))>2for i in range(n-2)for j in range(i+2,n,2)))

Solution (Python):
from collections import Counter

N = int(input())
s = input()
cnt = Counter(s)
R = cnt.get('R', 0)
G = cnt.get('G', 0)
B = cnt.get('B', 0)
ans = R * G * B
for i in range(N-1):
  for j in range(i+1, N):
    if s[i] != s[j] and 2*j-i < N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
      ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
cum = [[0]*3 for _ in range(N+1)]
for i,s in enumerate(S,1):
    cum[i]=cum[i-1].copy()
    if s=='R':
        cum[i][0]+=1
    elif s=='G':
        cum[i][1]+=1
    elif s=='B':
        cum[i][2]+=1


for i in range(len(S)):
  for j in range(i+1,len(S)):
    if S[i]==S[j]:
      continue
    else:
      x = 'RGB'.replace(S[i],'').replace(S[j],'')
      if x=='R':
        ans+=cum[-1][0]-cum[j][0]
        y = 2*j-i
        if y<S_l:
          if S[y]=='R':
            ans-=1
      elif x=='G':
        ans+=cum[-1][1]-cum[j][1]
        y = 2*j-i
        if y<S_l:
          if S[y]=='G':
            ans-=1
      else:
        ans+=cum[-1][2]-cum[j][2]
        y = 2*j-i
        if y<S_l:
          if S[y]=='B':
            ans-=1
 
print(ans)

Solution (Python):
from collections import Counter

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

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

if ans == 0:
    print(0)
    exit()

for n_skip in range(1, N):
    for i in range(N):
        try:
            if set(["R", "G", "B"]) == set([S[i], S[i+n_skip], S[i + 2*n_skip]]):
                ans -= 1
        except IndexError:
            break

print(ans)

Solution (Python):
def main():
    N=int(input())
    S=input()
    ans=S.count('R')*S.count('G')*S.count('B')
    for i in range(N):
        for d in range(N):
            j = i+d
            k = j+d
            if k >= N:
                break
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1
    print(ans)
main()

Solution (Python):
N=int(input())
S=input()
R,G,B=0,0,0
for i in range(N):
    if(S[i]=='R'):
        R+=1
    elif(S[i]=='G'):
        G+=1
    else:
        B+=1
res=R*G*B
cnt=0
for i in range(N):
    lr=0
    while(i-lr>=0 and i+lr<N):
        s=set([S[i],S[i-lr],S[i+lr]])
        if(len(s)==3):
            res-=1
        lr+=1
print(res)

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

ans = S.count("R")*S.count("B")*S.count("G")
for i in range(N):
    for d in range(N):
        j = i+d
        k = j+d
        if k < N:
            if S[i] != S[j] and S[k] != S[j] and S[k] != S[i]:
                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()
count=0
A=[[0,0,0]for i in range(n)]
for i in range(0,n):
    if s[n-i-1]=="R":
        A[n-i-2][0]=A[n-i-1][0]+1
        A[n-i-2][1]=A[n-i-1][1]
        A[n-i-2][2]=A[n-i-1][2]
    if s[n-i-1]=="G":
        A[n-i-2][0]=A[n-i-1][0]
        A[n-i-2][1]=A[n-i-1][1]+1
        A[n-i-2][2]=A[n-i-1][2]
    if s[n-i-1]=="B":
        A[n-i-2][0]=A[n-i-1][0]
        A[n-i-2][1]=A[n-i-1][1]
        A[n-i-2][2]=A[n-i-1][2]+1
for i in range(0,n-3):
    s1=s[i]
    for j in range(i+1,n-1):
        s2=s[j]
        if s1!=s2:
            if s1=="R" and s2=="G":
                s3="B"
            elif s1=="G" and s2=="R":
                s3="B"
            elif s1=="R" and s2=="B":
                s3="G"
            elif s1=="B" and s2=="R":
                s3="G"
            elif s1=="B" and s2=="G":
                s3="R"
            elif s1=="G" and s2=="B":
                s3="R"
            if s3=="R":
                s4=0
            if s3=="G":
                s4=1
            if s3=="B":
                s4=2
            count=count+A[j][s4]
            if j+j-i+1<=n: 
                if s[j+j-i]==s3:
                    count=count-1
print(count)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
counter = Counter(S)
ans = counter['R'] * counter['G'] * counter['B']
for l in range(1, N):
    for i in range(N):
        if i + 2*l >= N: break
        a, b, c = S[i], S[i + l], S[i + 2*l]
        if a != b and a != c and b != c: ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = g*r*b

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

print(ans)

Solution (Python):
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:(j-i)//2]))>2for i in range(n-2)for j in range(i+2,n,2)))

Solution (Python):
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)
R,G,B = len(rs),len(gs),len(bs)
rt = set(rs)
gt = set(gs)
bt = set(bs)

ans = 0
ri = gi = bi = 0
for i in range(N):
    if ri < R and i==rs[ri]:
        ans += gi*(B-bi) + bi*(G-gi)
        for g in gs:
            if i+(i-g) in bt:
                ans -= 1
        ri += 1
    elif gi < G and i==gs[gi]:
        ans += ri*(B-bi) + bi*(R-ri)
        for r in rs:
            if i+(i-r) in bt:
                ans -= 1
        gi += 1
    else:
        ans += ri*(G-gi) + gi*(R-ri)
        for r in rs:
            if i+(i-r) in gt:
                ans -= 1
        bi += 1
print(ans)

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

diff_dic = {}
diff_dic[('R', 'G')] = []
diff_dic[('R', 'B')] = []
diff_dic[('G', 'R')] = []
diff_dic[('G', 'B')] = []
diff_dic[('B', 'G')] = []
diff_dic[('B', 'R')] = []

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0

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

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i):
        d = i - j
        if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
            if j - 1 >=0 and B_lst[j - 1] > 0:
                cnt += B_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'B':
                    cnt -= 1

        if (S[i] == 'G' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'G'):
            if j - 1 >=0 and R_lst[j - 1] > 0:
                cnt += R_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'R':
                    cnt -= 1

        if (S[i] == 'B' and S[j] == 'R') or (S[i] == 'R' and S[j] == 'B'):
            if j - 1 >= 0 and G_lst[j - 1] > 0:
                cnt += G_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'G':
                    cnt -= 1

print(cnt)

Solution (Python):
n = int(input())
S = input().strip()
r, g, b = map(S.count, 'RGB')
t = 0
for i in range(n-2):
    for w in range(1, (n-i+1) // 2):
        t += len({S[i], S[i+w], S[i+2*w]}) == 3
print(r*g*b - t)

Solution (Python):
N=int(input())
S=input()
r = S.count("R")
g = S.count("G")
b = S.count("B")
out =r*g*b

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

Solution (Python):
import numpy as np
n = int(input())
s = list(input())

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


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

print(ans)

Solution (Python):
N = int(input())
S = input()
r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b
 
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        x = j + j - i
        if S[i] != S[j] and x <= N - 1:
            if S[i] != S[x] and S[x] != S[j]:
                ans -= 1
 
print(ans)

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

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Solution (Python):
N = int(input())
S = input()
result = 0
for i in range(1,N-1):
    a, b = ("G","B") if S[i] == "R" else ("R","B") if S[i] == "G" else ("R","G")
    mae = S[:i]
    ushiro = S[i+1:]
    result += mae.count(a) * ushiro.count(b)
    result += mae.count(b) * ushiro.count(a)
    result -= sum([(a == mae[-1-j] and b == ushiro[j]) or (b == mae[-1-j] and a == ushiro[j]) for j in range(min(len(mae),len(ushiro)))])
print(result)

Solution (Python):
N = int(input())
S = input()
r = g =  b = 0
for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1

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

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-1):
  for j in range(i+1,n):
    k = j*2 - i
    if k >= n:
      continue
    if len(set([s[i],s[j],s[k]])) == 3:
      ans -= 1
      
print(ans)

Solution (Python):
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:(j-i)//2]))>2for i in range(n-2)for j in range(i+2,n,2)))

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

R_cnt = s.count("R")
G_cnt = s.count("G")
B_cnt = s.count("B")

ans = R_cnt * G_cnt * B_cnt

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

print(ans)

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

R,G,B = 0,0,0

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

count = R * G * B

N2 = int(N/2 + 0.5)
for i in range(0,N):
    stri = S[i]
    for j in range(0, N2):
        j2 = 2 * j
        if i+j2 >= N:
            break
        strj = S[i+j]
        strk = S[i+j2]
        if stri != strj and strj != strk and stri != strk:
            count -= 1
print(count)

Solution (Python):

input_d41 = int(input())
input_d42 = input()

N = int(input_d41)
S = input_d42

R = S.count('R')
G = S.count('G')
B = N - (R + G)

ans = R*G*B
answ = 0

for i in range(0,N):
    for j in range(i+1,(N+i)//2 +1):
        if S[i] != S[j]:
            if 2*j-i < N and S[j] != S[2*j-i] and S[2*j-i] != S[i]:
                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)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R)*len(G)*len(B)
for i in range(N):
    for j in range(i+1,(N+i+1)//2):
        if S[i] != S[j] and S[j] != S[2*j-i] and S[i] != S[2*j-i]:
            s -= 1
print(s)

Solution (Python):
N = int(input())
S = input()
ans = S.count('R')*S.count('G')*S.count('B')
for i in range(N):
    for d in range(N):
        j = i+d
        k = j+d
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

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

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

print(ans)

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

rsum = 0
gsum = 0
bsum = 0

for s in S_list:
	if s == "R":
		rsum += 1
	elif s == "G":
		gsum += 1
	elif s == "B":
		bsum += 1

sum = rsum * gsum * bsum

for i in range(n-2):
	p = (n-i-1) // 2
	for j in range(1,p+1):
		if S_list[i] != S_list[i+j] and S_list[i+2*j]  != S_list[i+j] and S_list[i] != S_list[i+2*j]:
			sum -= 1

print(sum)

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


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


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = list(input())

    t = Counter(s)

    ans = t['R']*t['G']*t['B']

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

    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)
count = 0
unchi = set(b)
for i in r:
	for j in g:
		u = abs(i-j)
		count += [0,1][(max(i,j)+u) in unchi] + [0,1][(min(i,j)-u) in unchi] + [0,1][u%2 == 0 and (i+j)//2 in unchi]
print(len(r)*len(g)*len(b)-count)

Solution (Python):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    R = C.get("R", 0)
    G = C.get("G", 0)
    B = C.get("B", 0)
    ans = R * G * B
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid + mid - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
 
ans = r_cnt * g_cnt * b_cnt
 
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
 
print(ans)

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

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

for i in range(n):
    for j in range(i+1, n-1):
        k = j + (j - i)
        if k < n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                res -= 1
        else:
            break

print(res)

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

colors = ['R', 'B', 'G']

cnt = 1
for c in colors:
    cnt *= s.count(c)

for i in range(n):
    w = 1
    while True:
        if i + 2*w >= n: break
        if s[i] != s[i+w] and s[i+w] != s[i+2*w] and s[i+2*w] != s[i]:
            cnt -= 1
        w += 1
print(cnt)

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

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


print(len(R)*len(G)*len(B) - rem)

Solution (Python):
n = int(input())
s = input()
total_triplets = sum(int(val=="R") for val in s)*sum(int(val=="G") for val in s)*sum(int(val=="B") for val in s)
num_missing = 0
for centre_point in range(1, n-1):
  for halfwidth in range(1, 1+min(centre_point, n-centre_point-1)):
    if s[centre_point-halfwidth] != s[centre_point]:
      if s[centre_point] != s[centre_point+halfwidth]:
        if s[centre_point-halfwidth] != s[centre_point+halfwidth]: num_missing += 1
print(total_triplets-num_missing)

Solution (Python):
from collections import defaultdict
n = int(input())
s = input()
c_dic = defaultdict(list)
for idx,i in   enumerate(s):
    c_dic[i].append(idx)
ans = len(c_dic['R'])*len(c_dic['G'])*len(c_dic['B'])
for i in range(0,n-1):
    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[k] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
n=int(input())
s=input()
count=0
char="RGB"
A=[[0,0,0] for i in range(n)]
if s[0]=="R":
    A[0][0]+=1
elif s[0]=="G":
    A[0][1]+=1
else:
    A[0][2]+=1

for i in range(1,n):
    x=A[i-1][0]
    y=A[i-1][1]
    z=A[i-1][2]
    if s[i]=="R":
        x+=1
    elif s[i]=="G":
        y+=1
    else:
        z+=1
    A[i][0]=x
    A[i][1]=y
    A[i][2]=z

count=0
for i in range(n):
    for k in range(i+2,n):
        if s[i]!=s[k]:
            c=3-char.index(s[i])-char.index(s[k])
            x=A[k][c]-A[i][c]
            count+=x
            if (i+k)%2==0:
                j=(i+k)//2
                if char.index(s[j])==c:
                    count-=1
print(count)

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

s = input()
cnt = s.count('R') * s.count('G') * s.count('B')

for i in range(n):
    for j in range(n):
        if 0 <= i - j <= i and 0 <= i + j <= n-1:
            x1, x2, x3 = s[i - j], s[i], s[i + j]
            if x1 != x2 and x1 != x3 and x2 != x3:
                cnt -= 1
print(cnt)

Solution (Python):
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(sorted(s[i:j+1:(j-i)//2])==list('BGR')for i in range(n-2)for j in range(i+2,n,2)))

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

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

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

Solution (Python):
import math
N = int(input())
S = input()
outexp = 0

def comb(n, r):
  comb = math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
  return comb

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

if R >= 3:
  Rnum = comb(R, 2) * (N - R) + comb(R, 3)
if R == 2:
  Rnum = N - R
if R <= 1:
  Rnum = 0

if B >= 3:
  Bnum = comb(B, 2) * (N - B) + comb(B, 3)
if B == 2:
  Bnum = N - B
if B <= 1:
  Bnum = 0

if G >= 3:
  Gnum = comb(G, 2) * (N - G) + comb(G, 3)
if G == 2:
  Gnum = N - G
if G <= 1:
  Gnum = 0

for i in range(N - 2):
  m = 1
  while 2 * m + i <= N - 1:
    if not S[i] == S[i + m]:
      if not S[i + m] == S[i + 2 * m]:
        if not S[i] == S[i + 2 * m]:
          outexp += 1
    m += 1

sum = comb(N, 3) - Rnum - Bnum - Gnum - outexp

print(int(sum))