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 math
N = int(input())
S = input()




def calculate(n, s):
    arr = list(s)

    rNum = arr.count("R")
    gNum = arr.count("G")
    bNum = arr.count("B")

    sum = 0

    for step in range(1,math.ceil(n / 2) + 1):
        for i in range(n - 2*step):
            s = "".join([arr[i],arr[i+step],arr[i+step*2]])
            if s == "RGB" or s == "RBG" or s == "BGR" or s == "BRG" or s == "GBR" or s == "GRB":
                sum = sum + 1


    print(rNum*gNum*bNum - sum)



calculate(N, S)

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

r_num, g_num, b_num = 0, 0, 0
for i in s :
    if i == "R":
        r_num+=1
    elif i== "G":
        g_num +=1
    else:
        b_num+=1

ans = r_num * g_num *b_num

for i in range(len(s)):
    for j in range(i,len(s)):
        k = 2*j -i 
        if k <= j :
            continue
        else:
            if len(s) > k and k > 0  and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                ans  -=1

print(ans)

Solution (Python):
from collections import Counter

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

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

sum = r*g*b

for i in range(N):
    for j in range(i+1, N):
        k = 2 * j - i
        if k > N - 1:
            break
        if not S[i] == S[j] and not S[j] == S[k] and not S[k] == S[i]:
            sum -= 1
print(sum)

Solution (Python):
n = int(input())
s = input()
cnt = 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]:
            continue
        k = j - i + j
        if k > n-1:
            break
        if s[i] == s[k] or s[j] == s[k]:
                continue
        cnt -= 1
print(cnt)

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

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

ans = R*G*B

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

Solution (Python):
n = int(input())
s = input()
cnt = 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]:
            continue
        k = j - i + j
        if k > n-1:
            continue
        if s[i] == s[k] or s[j] == s[k]:
                continue
        cnt -= 1
print(cnt)

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

r_num, g_num, b_num = 0, 0, 0
for i in s :
    if i == "R":
        r_num+=1
    elif i== "G":
        g_num +=1
    else:
        b_num+=1

ans = r_num * g_num *b_num

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

ans = 0

for j in range(1,n-1):
    if s[j]=='R':
        ans += G[j]*(B[n]-B[j+1])
        ans += B[j]*(G[n]-G[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='B' and s[j+i+1]=='G':
                    ans-=1
                elif s[j-i-1]=='G' and s[j+i+1]=='B':
                    ans-=1
            else:
                break
    elif s[j]=='G':
        ans += R[j]*(B[n]-B[j+1])
        ans += B[j]*(R[n]-R[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='R' and s[j+i+1]=='B':
                    ans-=1
                elif s[j-i-1]=='B' and s[j+i+1]=='R':
                    ans-=1
            else:
                break
    elif s[j]=='B':
        ans += G[j]*(R[n]-R[j+1])
        ans += R[j]*(G[n]-G[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='R' and s[j+i+1]=='G':
                    ans-=1
                elif s[j-i-1]=='G' and s[j+i+1]=='R':
                    ans-=1
            else:
                break
print(ans)

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

ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
  a = S[i]
  for j in range(i+2,N,2):
    b = S[j]
    k = (i+j)//2
    if sorted([a,S[k],b]) == ['B','G','R']:
      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 (i+j)%2 :
      continue
    k = (i+j)//2
    if len(set([S[i],S[k],S[j]])) == 3:
      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-2):
    for j in range(i+1,n-1):
        k = 2*j-i
        if k < n:
            if s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
                    ans -= 1
                    
print(ans)

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

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

for i in range(n):
    for j in range(1,n//2+1):
        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]:
            ans -= 1
print(ans)

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

r = 0
g = 0
b = 0


for ss in s:
    if ss == "R":
        r += 1
    elif ss == "G":
        g += 1
    elif ss == "B":
        b += 1
ng = 0
i = 0
while i < 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]:
            ng += 1
        j += 1
    i += 1
print(r*g*b-ng)

Solution (Python):

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

r = 0
g = 0
b = 0
for c in S:
    if c =="R":
        r +=1    
    if c =="G":
        g +=1
    if c =="B":
        b +=1
_avoid = 0
for i in range(N):
    for j in range(i,N):
        k = 2*j -i
        if k >= N:
            continue
        if k > j and not S[i] == S[j] and not S[i] == S[k] and not S[j] == S[k]:
            _avoid +=1
print(r*g*b-_avoid)

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

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

Ans = L[0]*L[1]*L[2]

for i in range(N):
  s = S[i]
  n = min(N, (N+i)//2 + 1)
  for j in range(i+1,n):
    t = S[j]
    if s != t:
      k = 2*j-i
      if k <= N-1:
        u = S[k]
        if t != u and u != s:
          Ans -=1
print(Ans)

Solution (Python):



def remain(a, b):
    if a == 'R':
        if b == 'G':
            return 'B'
        else:
            return 'G'
    elif a == 'G':
        if b == 'R':
            return 'B'
        else:
            return 'R'
    else:
        
        if b == 'R':
            return 'G'
        else:
            return 'R'
    

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

cs = {}

cs['R'] = [ 0 for i in range(n) ]
cs['G'] = [ 0 for i in range(n) ]
cs['B'] = [ 0 for i in range(n) ]

for i in range(-1, - n - 1, -1):
    if s[i] == 'R':
        cs['R'][i] = cs['R'][i + 1] + 1
        cs['G'][i] = cs['G'][i + 1]            
        cs['B'][i] = cs['B'][i + 1]
    elif s[i] == 'G':
        cs['R'][i] = cs['R'][i + 1]
        cs['G'][i] = cs['G'][i + 1] + 1
        cs['B'][i] = cs['B'][i + 1]            
    elif s[i] == 'B':
        cs['R'][i] = cs['R'][i + 1]
        cs['G'][i] = cs['G'][i + 1]            
        cs['B'][i] = cs['B'][i + 1] + 1

            
c = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] != s[j]:
            r = remain(s[i], s[j])
            c += cs[r][j]
            if j + j - i < n and s[j + j - i] == r:
                c -= 1
            
print(c)

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

r = 0
g = 0
b = 0

for i in S:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  elif i == 'B':
    b += 1

res = r * g * b

for i in range(N - 1):
  for j in range(i, N - 1):
    k = j + j - i
    if k >= N:
      break
    if S[i] != S[j] and S[i] != S[k] and S[k] != S[j]:
      res -= 1
print(res)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
ans = 0

for j in range(1, N-1):
    cl, cr = Counter(S[:j]), Counter(S[j+1:])
    
    for i in set(cl.keys())-set(S[j]):
        ans += cl[i]*(N-1-j-cr[S[j]]-cr[i])
            
    for i in range(1, min(j, N-1-j)+1):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i]!=S[j+i]:
            ans -= 1
    
print(ans)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
ans = 0

for j in range(1, N-1):
    cl, cr = Counter(S[:j]), Counter(S[j+1:])
    
    for i in set(cl.keys())-set(S[j]):
        ans += cl[i]*(N-1-j-cr[S[j]]-cr[i])
            
    for i in range(1, min(j, N-1-j)+1):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i]!=S[j+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):
        k = j + j - i
        if k < N and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            res -= 1

print(res)

Solution (Python):
N = int(input())
S = input()
num_r = 0
num_g = 0
num_b = 0

for i in range(N):
  if S[i] == "R":
    num_r += 1
  if S[i] == "G":
    num_g += 1
  if S[i] == "B":
    num_b += 1

rgb = num_r*num_g*num_b
x = 0

for i in range(N):
  for m in range(N):
    j = i + m
    k = j + m
    if k > N - 1:
      break
    elif S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      x += 1
rgb -= x
print(rgb)

Solution (Python):
import collections

n = int(input())
S = input()



C = collections.Counter(S)
r = C['R']
g = C['G']
b = C['B']

ans = r * g * b


for j in range(1, n-1):
    sj = S[j] 
    if j > n-1-j:
        a = n-1-j
    else:
        a = j
    for d in range(1, a+1):
        si = S[j-d]
        sk = S[j+d]
        if si != sj and si != sk and sj != sk:
            
            ans -= 1
        else:
            pass
print(ans)

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

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

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

print(ans)

Solution (Python):

def main():
    n = int(input())
    s = input()
    r = s.count('R')
    g = s.count('G')
    b = n - r - g
    ans = r * g * 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)    

if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

count = collections.Counter(S)

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

for distance in range(1, (N - 1) // 2 + 1):
    for i in range(N):
        if i + 2 * distance >= N:
            break
        
        if len({S[i], S[i + distance], S[i + 2 * distance]}) == 3:
             ans -= 1
    

print(ans)

Solution (Python):
N=int(input())
S=list(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
    
n=r*g*b

for i in range(N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue
    k = 2*j-i
    if k >= N:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)

Solution (Python):
n = int(input())
s = input()
r_list = []
g_list = []
b_set = set()
for i in range(n):
    if s[i] == "R":
        r_list.append(i)
    if s[i] == "G":
        g_list.append(i)
    if s[i] == "B":
        b_set.add(i)
def tousable(p, q):
    res = []
    if p % 2 == q % 2:
        res.append((p+q)//2)
    res.append(2*p-q)
    res.append(2*q-p)
    return res
b_length = len(b_set)
ans = 0
for i in r_list:
    for j in g_list:
        minus = 0
        for k in tousable(i, j):
            if k in b_set:
                minus += 1
        ans += b_length - minus
print(ans)

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

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

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

Solution (Python):
from itertools import product  


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

R_i = [i for i, s in enumerate(S) if s == "R"]  
G_i = [i for i, s in enumerate(S) if s == "G"]  
B_i = {i for i, s in enumerate(S) if s == "B"}  

R = len(R_i)  
G = len(G_i)  
B = len(B_i)  

xs = map(sorted, product(R_i, G_i))  

s = 0  
for x in xs:  
    k = 2 * x[1] - x[0]  
    if k in B_i:  
        s += 1  
    i = 2 * x[0] - x[1]  
    if i in B_i:  
        s += 1  
    j = (x[0] + x[1]) / 2  
    if j in B_i:  
        s += 1  

print(R * G * B - s)

Solution (Python):

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


from collections import Counter
answer = 0
if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = Counter(S[:i])["G"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["G"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["G"]
            c = Counter(S[:i])["G"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1

print(answer)

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

Rs = 0
Gs = 0
Bs = 0
for il in S:
    if il == 'R':
        Rs += 1
    if il == 'G':
        Gs += 1
    if il == 'B':
        Bs += 1
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(Rs*Gs*Bs - num)

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

Rs = 0
Gs = 0
Bs = 0
for il in S:
    if il == 'R':
        Rs += 1
    if il == 'G':
        Gs += 1
    if il == 'B':
        Bs += 1
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(Rs*Gs*Bs - num)

Solution (Python):
n=int(input())
s=list(input())
ans=s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    if s[i]=='R':
        s[i]=1
    elif s[i]=='G':
        s[i]=2
    elif s[i]=='B':
        s[i]=4
for i in range(n):
    for j in range(i+1,n):
      k=2*j-i
      if k>=n:
        continue
      if s[i]+s[j]+s[k]==7:
          ans-=1
print(ans)

Solution (Python):
def main():
    N=int(input())
    S=input()
    k1=S.count("R")
    k2=S.count("B")
    ans=k1*k2*(len(S)-k1-k2)
    for i in range(N-2):
        for j in range(i+1,N-1):
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)
main()

Solution (Python):
from sys import stdin
N = list(map(int, (stdin.readline().strip().split())))[0]
S= list(stdin.readline().strip())
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:break
        if S[i]!=S[j] and S[j]!=S[k] and S[i] !=S[k]:
                ans-=1
print(ans)

Solution (Python):
from sys import stdin

n=int(input())
s=input()
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
ans=r*g*b
for i in range(n):
    for j in range(n):
        now1=i+j
        now2=i+j+j
        if now2>=n:
            continue
        if(s[i]!=s[now1] and s[now1]!=s[now2] and s[now2]!=s[i]):
            ans-=1
print(str(ans)+"\n")

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):
        k = j-i+j
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1

print(ans)

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time

from fractions import gcd

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


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


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


def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()
S = input()

"""
    Conditions:
        1. S[i] \ne S[j] and S[j] \ne S[k] and S[k] \ne S[i]
        2. j - i \ne k - j
"""

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



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

print(r * g * b - cnt)

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

print(ans)

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

for i in range(1,(n-1)//2+1):
    for k in range(n-2*i):
        if s[k]!=s[k+i] and s[k+i]!=s[k+2*i] and s[k+2*i]!=s[k]:
            ans-=1
print(r*g*b+ans)

Solution (Python):
str_len = int(input().rstrip())
str = input().rstrip()

rgb_nums_dict = {'R':str.count('R'), 'G':str.count('G'), 'B':str.count('B')}
total = 0
for i in range(str_len-2):
    for j in range(i+1,str_len-1):
        first_str = str[i]
        second_str = str[j]
        if first_str==second_str:
            continue
        else:
            if 2*j-i<str_len and second_str!=str[2*j-i]!=first_str:
                total-=1
                


for i in range(str_len):
    first_str = str[i]
    if first_str=='R':
        total += rgb_nums_dict['G']*rgb_nums_dict['B']
        
    elif first_str=='G':
        total += rgb_nums_dict['R']*rgb_nums_dict['B']
        
    else:
        total += rgb_nums_dict['R']*rgb_nums_dict['G']
        
    rgb_nums_dict[first_str]-=1
    if rgb_nums_dict[first_str]==0:
        break
    
print(total)

Solution (Python):
from itertools import product
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)

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

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

Solution (Python):
from itertools import product
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)

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

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

Solution (Python):
from collections import Counter

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

RGB = Counter(S)

su = RGB['R']*RGB['G']*RGB['B']

for i in range(N-2):
    for k in range(i+2,N,2):
        j = (i + k)//2
        a = [S[i],S[j],S[k]]
        if 'R' not in a:
            continue
        if 'G' not in a:
            continue
        if 'B' not in a:
            continue
        su -= 1

print(su)

Solution (Python):
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

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

    def count_(s1, s2):
        ret = 0
        x = bisect(rgb[s1], i)
        for j in rgb[s1][x:]:
            y = bisect(rgb[s2], j)
            d = j - i
            if j + d < N and S[j+d] == s2:
                ret += len_[s2] - y - 1
            else:
                ret += len_[s2] - y
        return ret

    ans = 0
    len_ = {'R': len(rgb['R']), 'G': len(rgb['G']), 'B': len(rgb['B'])}
    for i, s in enumerate(S):
        if s == 'R':
            for s1, s2 in (('G', 'B'), ('B', 'G')):
                ans += count_(s1, s2)
        elif s == 'G':
            for s1, s2 in (('R', 'B'), ('B', 'R')):
                ans += count_(s1, s2)
        else:
            for s1, s2 in (('R', 'G'), ('G', 'R')):
                ans += count_(s1, s2)
    print(ans)


main()

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

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

ans = R * G * B

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

Solution (Python):
n = int(input())
s = str(input())
r, g, b = map(s.count, "RGB")
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k<n and len(set([s[i], s[j], s[k]])) == 3:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
X = input()
ans = 0
r=0
g=0
b=0
G = []
R = []
B = []
for i in range(0,N):
    if(X[i] == 'G'):
        G.append(i)
        g+=1
for k in range(0,N):
    if(X[k] == 'R'):
        R.append(k)
        r+=1
for j in range(0,N):
    if(X[j] == 'B'):
        B.append(j)
        b+=1
ans = r*b*g
for i in range(0,N):
    for j in range(i+1,N):
        if(2*j-i < N) and X[i] != X[j]:
            if(X[2*j-i] != X[i]) and (X[2*j-i] != X[j]):
                ans -= 1
print(ans)

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
all = r * g * b

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

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

r_num = s.count("R")
g_num = s.count("G")
b_num = s.count("B")

ans = r_num * b_num * g_num

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):
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[i] != s[k] and s[j]!=s[k]:
            ans -= 1
print(ans)

Solution (Python):
import numpy as np
from itertools import combinations_with_replacement

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

rgb = 'RGB'

nr = S.count(rgb[0])
ng = S.count(rgb[1])
nb = N - nr - ng
nlst = np.array([nr, ng, nb])

nmax = np.argmax(nlst)
nmin = np.argmin(nlst)
if nmax == nmin:nmin = 2

minlst, midlst = [], []
maxlst = [0]*N

for i, s in enumerate(S):
    if s==rgb[nmax]:maxlst[i] = 1
    elif s==rgb[nmin]:minlst.append(i)
    else: midlst.append(i)
minlst.sort()
midlst.sort()
      
ans = 0

for midl in midlst:
    for minl in minlst:
        mm = abs(midl - minl)
        mmin = min(minl, midl) - mm
        mmax = max(minl, midl) + mm
        if mmin >= 0:
            if maxlst[mmin]: ans += 1
        if mmax <= N-1:
            if maxlst[mmax]: ans += 1
        if mm % 2 == 0 and maxlst[(minl + midl) // 2]: ans += 1
print(nr * ng * nb - ans)

Solution (Python):
n=int(input())
s=input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans=r*g*b
for i in range(1,(n+1)//2):
  for j in range(n-2*i):
    if len(set([s[j],s[j+i],s[j+2*i]]))==3:
      ans-=1
print(ans)

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

Solution (Python):
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = c["R"]*c["G"]*c["B"]
    less = 0
    for d in range(1, N+1):
        for i in range(N):
            
            if i+d < N and i+2*d < N and \
                    S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                less += 1
    print(ans - less)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = 1 if len(c.keys()) > 2 else 0
    for v in c.values():
        ans *= v
    less = 0
    for d in range(1, N+1):
        for i in range(N):
            
            if i+d < N and i+2*d < N and \
                    S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                less += 1
    print(ans - less)


if __name__ == '__main__':
    main()

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

r, g, b = 0, 0, 0
for c in S:
  if c == "R": r += 1
  if c == "G": g += 1
  if c == "B": b += 1

total = r*g*b

for i in range(N):
  for j in range(i+1, N):
    if S[i] == S[j]: continue

    k = 2*j - i
    if k >= N: continue
    if S[i] == S[k] or S[j] == S[k]: continue
    total -= 1

print(total)

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

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


count = 0

for i in range(n):
	for j in range(i+1,n):
		k = j*2-i
		if 0 <= k < n:
			if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
				count += 1
print(r*g*b - count)

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

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

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

print(r*g*b - cnt)

Solution (Python):
import numpy as np
def Main(N, S):
    num = [0]*3
    for c in S:
        if c == 'R':
            num[0] += 1
        elif c == 'G':
            num[1] += 1
        else:
            num[2] += 1
    ans = np.prod(num)
    sub = 0
    i = 0
    j = 1
    while(i-2 < N):
        while(i+2*j < N):
            a = i
            b = i+j
            c = i+2*j
            if S[a] != S[b] and S[a] != S[c] and S[b] != S[c]:
                sub += 1
            j += 1
        i += 1
        j = 1
    return ans - sub
        
    

def main():
    N = int(input())
    S = str(input())
    print(Main(N,S))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

counter = Counter(S)

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

print(counter["R"]*counter["G"]*counter["B"] - cnt)

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

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

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

print(r*g*b - cnt)

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

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

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

print(r*g*b - cnt)

Solution (Python):

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

Solution (Python):

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

Solution (Python):
from sys import stdin, stdout
from collections import defaultdict
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()

def brute(S):
	ans = 0
	for i in range(len(S)):
		for j in range(i+1, len(S)):
			for k in range(j+1, len(S)):
				if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
					if k - j != j - i:
						ans += 1
	return ans

def main():
	n = int(rl())
	s = rll()[0]
	cnt = [[0, 0, 0] for _ in range(len(s))]
	d = {"R": 0, "G": 1, "B": 2}
	for i in range(len(s)):
		if i == 0:
			for c in d.keys():
				if s[i] == c:
					cnt[0][d[c]] += 1
					break
		else:
			for j in range(3):
				cnt[i][j] = cnt[i-1][j] 
			for c in d.keys():
				if s[i] == c:
					cnt[i][d[c]] += 1
					break
	ans = 0
	for k in range(3, len(s)):
		color = s[k]
		for j in range(1, k):
			second_color = s[j]
			if second_color == color: continue
			diff = k - j
			third_color = [x for x in ["R", "G", "B"] if x != color and x != second_color][0]
			choices = cnt[j-1][d[third_color]]
			crit_i = (2*j) - k
			if crit_i >= 0 and crit_i < j:
				choices -= 1 if s[crit_i] == third_color else 0
			ans += choices
	stdout.write(str(ans));stdout.write("\n")
	stdout.close()

if __name__ == "__main__":
	main()