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):
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys
import bisect


sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N = ii()
S = si()

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 dist in range(N):
        j = i + dist
        k = j + dist
        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):
import sys
import collections

N = int(input())
S = [ch for ch in input()]
counter = collections.Counter(S)
if len(counter.keys()) != 3:
    print(0)
    sys.exit()
total = 1
for k, v in counter.items():
    total *= v

for i in range(len(S)):
    l, r = i - 1, i + 1
    while 0 <= l and r < len(S):
        if len(set([S[i], S[l], S[r]])) == 3:
            total -= 1
        l -= 1
        r += 1
print(total)

Solution (Python):
s=int(input())
t=input()
a=0
b=0
c=0
for i in range(s):
  if t[i]=='R':
    a=a+1
  elif t[i]=='G':
    b=b+1
  else:
    c=c+1
g=a*b*c
for j in range(s):
  for k in range(j+1,s):
    p=2*k-j
    if p>s-1:
      break
    if t[j]!=t[k] and t[p]!=t[k] and t[p]!=t[j]:
      g=g-1
print(g)

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()



def primeFactor(N):
    i,n=2,N
    ret={}
    d,sq=2,99
    while i<=sq:
        k=0
        while n%i==0:
            n,k,ret[i]=n//i,k+1,k+1
        if k>0 or i==97:
            sq=int(n**(1/2)+0.5)
        if i<4:
            i=i*2-1
        else:
            i,d=i+d,d^6
    if n>1:
        ret[n]=1
    return ret




def PrimeNumSet(N):
    Max = int(N**0.5)
    seachList = [i for i in range(2,N+1)]
    primeNum = []
    while seachList:
        if seachList[0] <= Max:
            break
        primeNum.append(seachList[0])
        tmp = seachList[0]
        seachList = [i for i in seachList if i % tmp != 0]
    primeNum.extend(seachList)
    return primeNum



def LCM(b,mo=10**9+7):
    prs=PrimeNumSet(max(b))
    M=dict(zip(prs,[0]*len(prs)))
    for i in b:
        dc=primeFactor(i)
        for j,k in dc.items():
            M[j]=max(M[j],k)
    
    r=1
    for j,k in M.items():
        if k!=0:
            r*=pow(j,k,mo)
            r%=mo
    return r

show_flg=False
show_flg=True

'''

g=[[1]*201 for i in range(201)]
for a in range(1,201):
    for b in range(1,201):
        g[a][b]=math.gcd(a,b)

k=I()
ans=0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans+=g[a][g[b][c]]
         
print(ans)
'''



n=I()
s=input()
A=[[0],[0],[0]]
for i in range(n):
    w=s[i]
    A[0].append(A[0][-1]+(w=='R'))
    A[1].append(A[1][-1]+(w=='G'))
    A[2].append(A[2][-1]+(w=='B'))


t='RGB'

a=0
for i in range(1,n-1):
    w=s[i]
    if w=='R':
        j,k=1,2
    elif w=='G':
        j,k=0,2
    elif w=='B':
        j,k=0,1

    a+=A[j][i]*(A[k][n]-A[k][i])
    a+=A[k][i]*(A[j][n]-A[j][i])
    for l in range(i):
        if i+i-l>=n:
            continue
        if set([s[l],s[i+i-l]])==set([t[j],t[k]]):
            a-=1

print(a)

Solution (Python):
n = int(input())
s = list(input())
ans = 0
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)
d = 0
for i in range(n-2):
    for j in range(i,n-1):
        k = j+j-i
        if k < n and s[i] != s[j] and s[k] != s[i] and s[k]!=s[j]:
            d+=1
print(ans-d)

Solution (Python):
import collections

def other(a, b):
    if(a!="R" and b!="R"):
        return "R"
    if(a!="G" and b!="G"):
        return "G"
    if(a!="B" and b!="B"):
        return "B"


N = int(input())
S = str(input())
counterList = []

for i in range(N):
    counterList.append(collections.Counter(S[i:]))

result = 0
for j in range(1, N-1):
    for i in range(j):
        if(S[i] != S[j]):
            key = other(S[i], S[j])
            result += counterList[j][key]
            if(2*j-i < N):
                if(S[2*j-i] == key):
                    result -= 1
print(result)

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

lr = []
lg = []
lb = []

for i in range(n):
    if ln[i] == "R":
        lr.append(i)
    elif ln[i] == "G":
        lg.append(i)
    else:
        lb.append(i)

ans = 0
ba = len(lb)

for i in lr:
    for j in lg:
        ta = ba
        if (i+j) % 2 == 0 and ln[(i+j) // 2] == "B":
            ta -= 1

        d = abs(j - i)
        df = max(j+d, i+d)
        dn = min(j-d, i-d)
        if df < n and ln[df] == "B":
            ta -= 1
        if dn > 0 and ln[dn] == "B":
            ta -= 1
        ans += ta

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()



 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']


 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)

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

Solution (Python):
N = int(input())
S = input()
X = [0]*3
for s in S:
  if s == "R":
    X[0] += 1
  elif s == "B":
    X[1] += 1
  else:
    X[2] += 1
total = X[0]*X[1]*X[2]
flag = [False] * 3
for i in range(N-2):
  for j in range(i+1, N-1):
    if S[i] == "R":
      flag[0] = True
    elif S[i] == "B":
      flag[1] = True
    else:
      flag[2] = True
    if S[j] == "R":
      flag[0] = True
    elif S[j] == "B":
      flag[1] = True
    else:
      flag[2] = True
    k = j + j - i
    if k <= N-1:
      if S[k] == "R":
        flag[0] = True
      elif S[k] == "B":
        flag[1] = True
      else:
        flag[2] = True
    if all(flag):
      total -= 1
    flag = [False]*3
print(total)

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input())
S = input().strip()

dic = {}
ans = 0

for i in range(N-2):
    dic['R'] = S[i+1:].count('R')
    dic['G'] = S[i+1:].count('G')
    dic['B'] = S[i+1:].count('B')
    for j in range(i+1, N-1):
        dic[S[j]] -= 1
        arr = ['R', 'G', 'B']
        if S[i] != S[j]:
            arr.remove(S[i])
            arr.remove(S[j])
            ans += dic[arr[0]]
            if j+j-i <= N-1 and S[j+j-i] == arr[0]:
                ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = 0
ans_list = []

R_index = []
G_index = []
B_index = []

for i in range(n):
    if s[i] == 'R':
        R_index.append(i)
    elif s[i] == 'G':
        G_index.append(i)
    elif s[i] == 'B':
        B_index.append(i)

ans = len(R_index) * len(G_index) * len(B_index)

for j in range(1, n-1):
    for i in range(min(n-j-1,j)+1):
        
        
        
        
        if s[j-i] != s[j] and s[j] != s[j+i] and s[j-i] != s[j+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):
from collections import Counter

n = int(input())
s = input()
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(n) for j in range(i + 2, n, 2)))
except ValueError:
  print(0)

Solution (Python):
N = int(input())
S = input()
A = [[],[],[]]

for i in range(N):
    if S[i] == 'R':
        A[0].append(i)
    elif S[i] == 'G':
        A[1].append(i)
    else:
        A[2].append(i)
ans = len(A[0])*len(A[1])*len(A[2])
p = 0
for i in range(N):
  for j in range(N):
    if i+j+j <= N-1:
      if S[i] != S[i+j] and S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
        p += 1

print(ans-p)

Solution (Python):

N = int(input())
S = 'D' + input()


rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1


dict={'R':1, 'G':2, 'B':4}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = rgb_count[1][0]*rgb_count[1][1]*rgb_count[1][2]
for i in range(2,N):
    ran = min([i-1, N-i])
    if ran < 1: continue
    for k in range(1, ran+1):
        if dict[S[i-k]]+dict[S[i]]+dict[S[i+k]] == 7:
            ret -= 1

print(ret)

Solution (Python):
N = int(input())
S = input()
ans = 0
r = g = b = rg = rb = gb = 0

for c in S:
    if c=="B":
        ans += rg
        rb += r
        gb += g
        b += 1
    elif c=="G":
        ans += rb
        rg += r
        gb += b
        g += 1
    else:
        ans += gb
        rg += g
        rb += b
        r += 1
for j in range(1, N):
    Sj = S[j]
    for Si, Sk in zip(S[j-1::-1], S[j+1:]):
        if len({Si, Sj, Sk}) == 3:
            ans -= 1
print(ans)

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

r = [0] * (n)
g = [0] * (n)
b = [0] * (n)
if s[0] == 'R':
  r[0] = 1
elif s[0] == 'G':
  g[0] = 1
else:
  b[0] = 1

for i in range(n-1):
  r[i+1] = r[i]
  g[i+1] = g[i]
  b[i+1] = b[i]
  
  if s[i+1] == 'R':
    r[i+1] += 1
  elif s[i+1] == 'G':
    g[i+1] += 1
  else:
    b[i+1] += 1
    
ans = 0
for i in range(n):
  if s[i] == 'R':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'G':
        ans += b[-1] - b[j]
        if j+j-i < n and s[j+j-i] == 'B':
          ans -= 1
          
      if s[j] == 'B':
        ans += g[-1] - g[j]
        if j+j-i < n and s[j+j-i] == 'G':
          ans -= 1        
        
  if s[i] == 'G':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'R':
        ans += b[-1] - b[j]
        if j+j-i < n and s[j+j-i] == 'B':
          ans -= 1        
        
      if s[j] == 'B':
        ans += r[-1] - r[j]
        if j+j-i < n and s[j+j-i] == 'R':
          ans -= 1        
        
  if s[i] == 'B':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'G':
        ans += r[-1] - r[j]
        if j+j-i < n and s[j+j-i] == 'R':
          ans -= 1        
        
      if s[j] == 'R':
        ans += g[-1] - g[j]
        if j+j-i < n and s[j+j-i] == 'G':
          ans -= 1        

        
print(ans)

Solution (Python):
n = int(input())
S = input()
r = []
g = []
b = []
for i, s in enumerate(S):
    if s == 'R':
        r.append(i+1)
    elif s == 'G':
        g.append(i+1)
    else:
        b.append(i+1)

lenr = len(r)
leng = len(g)
lenb = len(b)
total = lenr*leng*lenb
for i in range(0, n-2):
    for j in range(n-(i+1)*2):
        j1 = j+i+1
        j2 = j+i*2+2
        if (S[j] != S[j1]) and (S[j1] != S[j2]) and (S[j2] != S[j]):
            total -= 1
print(total)

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

count = 0
r = []
g = []
b = []

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

count += len(r)*len(g)*len(b)

for i in range(n-2):
    for j in range(i+1,n):
        if j+(j-i) >= n:
            continue
        k = j + (j-i)
        if (s[i] != s[j]) and (s[j] != s[k]) and (s[k] != s[i]):
            count -= 1

print(count)

Solution (Python):
N = int(input())
S = 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):
        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):
from collections import Counter

n = int(input())
s = input()
c = Counter(s)
cnt = c['R'] * c['G'] * c['B']

for i in range(n-2):
    for j in range(i+1, n-1):
        w = j - i
        if j+w >= n:
            continue
        if s[i] != s[j] and s[j+w] != s[j] and s[j+w] != s[i]:
            cnt -= 1

print(cnt)

Solution (Python):


import queue

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

     r_list = []
     g_list = []
     b_list = []

     ans = 0

     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)

     for i in range(n):
          if s[i] == 'R':
               ans += check(i, g_list, b_list)

          if s[i] == 'G':
               ans += check(i, r_list, b_list)

          if s[i] == 'B':
               ans += check(i, r_list, g_list)

     print(ans)

def check(spot, first, second):
     ret = 0
     small_first = []
     big_first = []
     small_second = []
     big_second = []

     for f in first:
          if f < spot: small_first.append(spot - f)
          else: big_first.append(f - spot)

     for s in second:
          if s < spot: small_second.append(spot - s)
          else: big_second.append(s - spot)

     return len(small_first) * len(big_second) + len(small_second) * len(big_first) - len(set(small_first) & set(big_second)) - len(set(small_second) & set(big_first))
     
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s =     input()
if n < 3:
  print(0)
  exit

result = 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[j] != s[i]:
      result -= 1

print(result)

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(n):
        if i+2*j < n:
            if (s[i],s[i+j],s[i+2*j]) == ("R","G","B") or (s[i],s[i+j],s[i+2*j]) == ("R","B","G")or (s[i],s[i+j],s[i+2*j]) == ("G","B","R") or (s[i],s[i+j],s[i+2*j]) == ("G","R","B") or (s[i],s[i+j],s[i+2*j]) == ("B","G","R") or (s[i],s[i+j],s[i+2*j]) == ("B","R","G"):
                ans -= 1
print(ans)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    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)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            
            
            
            
            
            
                
                
                
                
                
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    print(ans)
resolve()

Solution (Python):
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
S = input()

d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1

print(plus - minus)

Solution (Python):


import bisect

N=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)
    else:
        B.append(i)

ans=0

def calc(lis0,lis1,lis2):
    temp=0
    for l0 in lis0:
        for l1 in lis1:
            if l1<l0:
                continue
            start=bisect.bisect_right(lis2,l1)
            temp+=len(lis2)-start
            index=bisect.bisect_left(lis2,2*l1-l0)
            
            
            if index!=len(lis2):
                if lis2[index]==2*l1-l0:
                    temp-=1
    return temp

ans=calc(R,G,B)+calc(R,B,G)+calc(G,R,B)+calc(G,B,R)+calc(B,R,G)+calc(B,G,R)
            
print(ans)

Solution (Python):

from collections import Counter, deque
n = int(input())
s = input()







c = Counter(s)
c = list(c.values())
if len(c) == 3:
    ans = c[0] * c[1] * c[2]
else:
    ans = 0

if ans > 0:
    for k in range(n):
        for a in range(1, (n - k)//2 + 1):
            if k + 2 * a >= n:
                continue
            x, y,  = s[k], s[k + a]
            
            if x != y:
                z = s[k + 2 * a]
                if y != z:
                    if z != x:
                        ans -= 1

print(ans)

Solution (Python):
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
Rs = set(R)
Bs = set(B)
Gs = set(G)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Bs)
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Gs)
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & Bs)
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & Gs)
print(res)

Solution (Python):
n = int(input())
s =     input()
if n < 3:
  print(0)
  exit

result = 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[j] != s[i]:
      result -= 1

print(result)

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    for k in range(j+1, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1)] = total
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        if a+str(j) in check:
            total += check[a+str(j)]
            break
        if a != s[j]:
            total += get_pattern2(i, j)
    check[a+str(i+1)] = total

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

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

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

r, g, b = 0, 0, 0
for si in s:
    if si == "R":
        r += 1
    elif si == "G":
        g += 1
    else:
        b += 1
        
ans = r * g * b

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

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((N-1)//2):
    for j in range(N-i-i-2):
        if S[j] != S[j+i+1] and S[j+i+1] != S[j+i+i+2] and S[j] != S[j+i+i+2]:
            hiku += 1


answer -= hiku
print(answer)

Solution (Python):
def solve(n,s,l1,l2,l3):
  ans = len(l1)*len(l2)*len(l3)

  for a in l1:
    for b in l2:
      if a < b:
        if 2*b-a in l3:
          ans -= 1
        if 2*a-b in l3:
          ans -= 1
        if (b-a-1)%2==0:
          continue
        else:
          if (b-a)%2 == 0 and (b-a)/2 + a in l3:
            ans -= 1
      else:
        if 2*a-b in l3:
          ans -= 1
        if 2*b-a in l3:
          ans -= 1
        if (a-b-1)%2==0:
          continue
        else:
          if (a-b)%2 == 0 and (a-b)/2 + b in l3:
            ans -= 1
  return ans



if __name__ == "__main__":
  n= int(input())
  s = input()
  r_list = set()
  g_list = set()
  b_list = set()
  r_count = 0
  g_count = 0
  b_count = 0
  for i, str in enumerate(s):
    if str == "R":
      r_list.add(i+1)
      r_count += 1
    elif str == "G":
      g_list.add(i+1)
      g_count += 1
    else:
      b_list.add(i+1)
      b_count += 1

  if r_count <= g_count and r_count <= b_count:
    l1 = r_list
    if g_count <= b_count:
      l2 = g_list
      l3 = b_list
    else:
      l2 = b_list
      l3 = g_list
  elif g_count <= r_count and g_count <= b_count:
    l1 = g_list
    if r_count <= b_count:
      l2 = r_list
      l3 = b_list
    else:
      l2 = b_list
      l3 = r_list
  elif b_count <= g_count and b_count <= r_count:
    l1 = b_list
    if g_count <= r_count:
      l2 = g_list
      l3 = r_list
    else:
      l2 = r_list
      l3 = g_list

  print(solve(n, s, l1,l2,l3))

Solution (Python):
N = int(input())
S = "0"+input()
lr = [0]
lg = [0]
lb = [0]
for j in range(1,N+1):
  if S[j] == "R":
    i = j-1
    lr.append(lr[i]+1)
    lg.append(lg[i])
    lb.append(lb[i])
  if S[j] == "G":
    i = j-1
    lr.append(lr[i])
    lg.append(lg[i]+1)
    lb.append(lb[i])
  if S[j] == "B":
    i = j-1
    lr.append(lr[i])
    lg.append(lg[i])
    lb.append(lb[i]+1)

cnt = 0
mcnt = 0
for i in range(1,N+1):
  s = S[i]
  for j in range(1,N-i+1): 
    
    if s == "R":      
      if S[i+j] == "R":
        continue
      else:
        if i+j <= N and i-j >=0:
          if (S[i+j] == "B" and S[i-j] == "G") or (S[i+j] == "G" and S[i-j] == "B"):
            mcnt += -1 
        if S[i+j] == "G":
          cnt += lb[i-1]
        if S[i+j] == "B":
          cnt += lg[i-1]
    
    if s == "G":      
      if S[i+j] == "G":
        continue
      else:
        if i+j <= N and i-j >=0: 
          if (S[i+j] == "B" and S[i-j] == "R") or (S[i+j] == "R" and S[i-j] == "B"):
            mcnt += -1 
        if S[i+j] == "R":
          cnt += lb[i-1]
        if S[i+j] == "B": 
          cnt += lr[i-1]   
    
    if s == "B":      
      if S[i+j] == "B":
        continue
      else:
        if i+j <= N and i-j >=0: 
          if (S[i+j] == "G" and S[i-j] == "R") or (S[i+j] == "R" and S[i-j] == "G"):
            mcnt += -1 
        if S[i+j] == "R":
          cnt += lg[i-1]
        if S[i+j] == "G": 
          cnt += lr[i-1]  
print(cnt+mcnt)

Solution (Python):
from sys import stdin, stdout
def main():
    N=int(stdin.readline())
    S=stdin.readline().rstrip()
    R, G, B = set(), set(), set()
    for i, c in enumerate(S):
        if c == 'R':
            R.add(i)
        elif c == 'G':
            G.add(i)
        else:
            B.add(i)
    cnt, total = 0, len(R) * len(G) * len(B)
    for ri in R:
        for gi in G:
            if abs(ri-gi)+gi in B:
                cnt += 1
            if abs(ri-gi)+ri in B:
                cnt += 1
    for ri in G:
        for gi in B:
            if abs(ri-gi)+gi in R:
                cnt += 1
            if abs(ri-gi)+ri in R:
                cnt += 1
    for ri in R:
        for gi in B:
            if abs(ri-gi)+gi in G:
                cnt += 1
            if abs(ri-gi)+ri in G:
                cnt += 1

    stdout.write(str(total-cnt)+"\n")

main()

Solution (Python):
S = input()
S = list(input())
S_len = len(S)
ans = 0
i = 0
for _ in range(S_len):
  if S[0] == 'R':
    ans += S[1:].count('B') * S[1:].count('G')
    for j in range(int(len(S)/2+1)):
      try:
        if S[j+1] != S[j*2+2] and S[j*2+2] !='R' and S[j+1] !='R':
          ans-=1
      except:
        pass
  if S[0] == 'B':
    ans += S[1:].count('R') * S[1:].count('G')
    for j in range(int(len(S)/2+1)):
      try:
        if S[j+1] != S[j*2+2] and S[j*2+2] !='B' and S[j+1] !='B':
          ans-=1
      except:
        pass
  if S[0] == 'G':
    ans += S[1:].count('B') * S[1:].count('R')
    for j in range(int(len(S)/2+1)):
      try:
        if S[j+1] != S[j*2+2] and S[j*2+2] !='G' and S[j+1] !='G':
          ans-=1
      except:
        pass
  S  = S[1:]
print(ans)

Solution (Python):
import sys

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

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

for i in range(n):
  if s[i] == "R":
    rs.append(i)
  elif s[i] == "G":
    gs.append(i)
  elif s[i] == "B":
    bs.append(i)

def binary(lst, d):
  low = 0
  high = len(lst) - 1
  while low <= high:
    mid = (low + high) // 2
    if lst[mid] == d:
      return True
    elif lst[mid] < d:
      low = mid + 1
    else:
      high = mid - 1
  return False

ans = len(rs)*len(gs)*len(bs)
if len(bs) == 0:
  print(0)
  sys.exit()
for i in range(len(rs)):
  for j in range(len(gs)):
    dis = abs(rs[i] - gs[j])
    if (rs[i] + gs[j])%2 == 0:
      can1 = abs(rs[i] + gs[j])//2
      if binary(bs, can1):
      	ans -= 1
    if max(rs[i], gs[j]) + dis <= bs[-1]:
      can2 = max(rs[i], gs[j]) + dis
      if binary(bs, can2):
      	ans -= 1
    if min(rs[i], gs[j]) - dis > 0:
      can3 = min(rs[i], gs[j]) - dis
      if binary(bs, can3):
      	ans -= 1
        
print(ans)

Solution (Python):
def remaining(s1, s2):
    if not("R" in [s1, s2]):
        return "R"
    elif not("B" in [s1, s2]): 
        return "B"
    else:
        return "G"

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

num_R = [0 for _ in range(N)]
num_G = [0 for _ in range(N)]
num_B = [0 for _ in range(N)]
if S[-1] == "R":
    num_R[-1] = 1
elif S[-1] == "B":
    num_B[-1] = 1
else:
    num_G[-1] = 1

for i in range(N-2, -1, -1): 
    if S[i]=="R":
        num_R[i] = num_R[i+1]+1
        num_G[i] = num_G[i+1]
        num_B[i] = num_B[i+1]
    elif S[i]=="G":
        num_R[i] = num_R[i+1]
        num_G[i] = num_G[i+1]+1
        num_B[i] = num_B[i+1]
    else:
        num_R[i] = num_R[i+1]
        num_G[i] = num_G[i+1]
        num_B[i] = num_B[i+1]+1

if __name__ == "__main__":
    ans = 0
    for i in range(0, N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            target = remaining(S[i], S[j])
            if target == 'R':
                num = num_R
            elif target == 'G':
                num = num_G
            else:
                num = num_B

            ans += num[j+1]
            if j+(j-i) < N:
                if S[j+(j-i)]==target:
                    ans -= 1
    print(ans)

Solution (Python):
import bisect
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)
	else:
		b.append(i+1)
ans = 0
if len(r) == 0 or len(g) == 0 or len(b) == 0:
	print (0)
	exit()
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)