Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
n = int(input())
s = input()
bad_num = [0] * n
pair = 0
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair += 1
        if 2 * i - j >= 0:
            bad_num[2 * i - j] += 1
        if (j - i) % 2 == 0:
            bad_num[(i + j) // 2] += 1
        if 2 * j - i < n:
            bad_num[2 * j - i] += 1
if s[-1] == "B":
    b_num.append(n - 1)

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

Solution (Python):
import math

n = int(input())
s = input()
count = 0

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

for dis in range(1, math.ceil(len(s) / 2)):
    for i in range(dis, len(s) - dis):
        if s[i-dis] != s[i] and s[i-dis] != s[i+dis] and s[i] != s[i+dis]:
            ans -= 1
print(ans)

Solution (Python):
e=enumerate
_,s=open(0)
r,g,b=map(s.count,'RGB')
print(r*g*b-sum(c!=d!=s[i+j]!=c for i,c in e(s)for j,d in e(s[i:-1: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[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math
import sys
import collections

def main():
    N=int(input())
    Tr=list(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
           continue
       if Tr[i]=="G":
           G.append(i)
           continue
       if Tr[i]=="B":
           B.append(i)
           continue
    cnt=len(R)*len(G)*len(B)
    for j in range(N):
        for i in range(j):
            if 2*j-i<=N-1:
                if Tr[2*j-i]!=Tr[i] and Tr[2*j-i]!=Tr[j] and Tr[j]!=Tr[i]:
                    cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
nR = 0
nG = 0
nB = 0
cnt = 0
for i in range(N):
  if S[i] == 'R':
    nR += 1
  elif S[i] == 'G':
    nG += 1
  else:
    nB += 1
  for j in range(i+1, N):
    k = j - i + j
    if k < N:
      L = [S[i], S[j], S[k]]
      if 'R' in L and 'G' in L and 'B' in L:
        cnt += 1
    else:
      break
print(nR*nG*nB-cnt)

Solution (Python):
n = int(input())
S = list(input())
R, G, B = set(), set(), set()
for i in range(n):
    if S[i] == 'R':
        R.add(i)
    if S[i] == 'G':
        G.add(i)
    if S[i] == 'B':
        B.add(i)
cnt = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        if 2*g-r in B:
            cnt -= 1
        if 2*r-g in B:
            cnt -= 1
        if (r+g) % 2 == 0 and (r+g)//2 in B:
            cnt -= 1
print(cnt)

Solution (Python):
N=int(input())
 
S=list(input())
m=0
 
a=S.count("R")
b=S.count("G")
c=S.count("B")
 
num=a*b*c
 
for i in range(N-2):
  for j in range(i+1,N-1):
    k=2*j-i 
    if k>N-1:
      break
    elif S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      m+=1
      
print(num-m)

Solution (Python):
from collections import Counter
N=int(input())
S=[0]*5000
S=list(input())

S_count=Counter(S)
r=S_count["R"]
g=S_count["G"]
b=S_count["B"]

ans=0

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

print(r*g*b-ans)

Solution (Python):
import bisect
N = int(input())
S = list(input())
R = []
G = []
B = []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)

r = len(R)
g = len(G)
b = len(B)
ans = r*g*b

for i in R:
    for j in G:
        M = max(i,j)
        m = min(i,j)
        d = M - m
        x = (M+m)/2
        y = M + d
        z = m - d
        if bisect.bisect_left(B,x) != bisect.bisect_right(B,x):
            ans -= 1
        if bisect.bisect_left(B,y) != bisect.bisect_right(B,y):
            ans -= 1
        if bisect.bisect_left(B,z) != bisect.bisect_right(B,z):
            ans -= 1
print(ans)

Solution (Python):

import numpy as np
import itertools
import time
N = int(input())
S = (input())

r_index_lis, g_index_lis, b_index_lis = [], [], []
for i, s in enumerate(S):
    if s == 'R':
        r_index_lis.append(i)
    elif s == 'G':
        g_index_lis.append(i)
    else:
        b_index_lis.append(i)
count = len(r_index_lis)*len(g_index_lis)*len(b_index_lis)

















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


print(count)

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

S=list(input())
m=0

a=S.count("R")
b=S.count("G")
c=S.count("B")

num=a*b*c

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

Solution (Python):
import sys



import collections




sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read
count = collections.Counter

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

    values = list(count(S).values())
    if len(values) < 3:
        print(0)
        return
    
    ans = values[0] * values[1] * values[2]

    for i in range(N-2):
        for j in range(i+1, N-1):
            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)
    


if __name__ == "__main__":
    main()

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 = 2*j-i
        if k<n:
            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 = list(input())
r,g,b = 0,0,0
k = 0
buf = 0

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

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

print(r*g*b - buf)

Solution (Python):
e=enumerate
_,s=open(0)
r,g,b=map(s.count,'RGB')
print(r*g*b-sum(c!=d!=s[i+j]!=c for i,c in e(s)for j,d in e(s[i:-1:2])))

Solution (Python):
N = int(input())
K = list(str(input()))
R = 0
G = 0
B = 0
for i in range(N):
    if K[i] == 'R':
        R += 1
    if K[i] == 'G':
        G += 1
    if K[i] == 'B':
        B += 1

count = 0
for i in range(N-2):
    for j in range((N-3-i)//2 + 1):
        if K[i] != K[i+j+1]:
            if K[i+j+1] != K[i+j+j+2]:
                if K[i+j+j+2] != K[i]:
                    count += 1

print(R*G*B - count)

Solution (Python):
e=enumerate
_,s=open(0)
r,g,b=map(s.count,'RGB')
print(r*g*b-sum(c!=d!=s[i-~j]!=c for i,c in e(s)for j,d in e(s[i+2:-1:2])))

Solution (Python):
import itertools
import bisect
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

STR = [[] for _ in range(3)]
RGB = {'R': 0, 'G': 1, 'B': 2}
for i in range(N):
    s = S[i]
    STR[RGB[s]].append(i)

if not STR[0] or not STR[1] or not STR[2]:
    print(0)
    exit()

cnt = 0
for x, y in itertools.combinations(range(3), 2):
    for a, b in itertools.product(STR[x], STR[y]):
        if b < a:
            a, b = b, a

        c = b + (b - a)

        if N - 1 < c:
            continue

        t = RGB[S[c]]

        if t != x and t != y:
            cnt += 1

ans = len(STR[0]) * len(STR[1]) * len(STR[2]) - cnt
print(ans)

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

r = 0
g = 0
b = 0
for s in S:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

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

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
cnt = 0

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]:
            cnt += 1

ans = ans - cnt
print(ans)

Solution (Python):
import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
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()))

N = INT()
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
res = r*g*b
for i in range(len(S)-2):
    for j in range(i+1, len(S)-1):
        if 2*j-i < len(S):
            if S[2*j-i] != S[j] and S[2*j-i] != S[i] and S[i] != S[j]:
                res -= 1

print(res)

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

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


p1 = r * g * b


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


print(p1 - p1_not_p2)

Solution (Python):




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

    R = 0
    G = 0
    B = 0

    for s in S:
        if s == 'R':
            R += 1
            continue
        elif s == 'G':
            G += 1
            continue
        elif s == 'B':
            B += 1
    

    total = 0

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

    print(R*G*B - total)


if __name__ == '__main__':
    main()

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

ans = len(r)*len(g)*len(b)

for A, B, C in permutations([r,g,b]):
    for i in A:
        for j in B:
            k = 2 * j - i
            if k < N and i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]: 
                ans -= 1
print(ans)

Solution (Python):
from collections import Counter

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

for i in range(n):
    l = 1
    si = s[i]
    while i + l * 2 < n:
        if si != s[i + l] and si != s[i + l * 2] and s[i + l] != s[i + l * 2]:
            ans -= 1
        l += 1
print(ans)

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

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


p1 = r * g * b


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


print(p1 - p1_not_p2)

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


r = s.count('R')
g = s.count('G')
b = len(s) - r - g
total = r*g*b


for p in range(1, (n-1)//2 + 1):
    for i in range(0, n-(2*p)):
        si, sj, sk = s[i], s[i+p], s[i+2*p]
        if si != sj and sj != sk and sk != si:
            total -=  1
print(total)

Solution (Python):
N = int(input())
S = str(input())
R,G,B = 0,0,0
for i in range(N):
    if S[i] == 'R':
        R += 1
    elif S[i] == 'B':
        B += 1
    else:
        G += 1

exc = 0
for j in range(1,N-1):
    num = 1
    while 0 <= j-num and j+num < N:
        if S[j-num] != S[j+num] and  S[j-num] != S[j] and  S[j] != S[j+num]:
            exc += 1
        num += 1

print(int(R*B*G - exc))

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

    ans = s.count("R") * s.count("B") * s.count("G")
    for i in range(n):
        for j in range(i, 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)

if __name__ == '__main__':
    main3()

Solution (Python):
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range((n-3)//2+1):
    for j in range(n-3-i*2+1):
        j1 = j+i+1
        j2 = j+i*2+2
        if s[j] != s[j1] and s[j1] != s[j2] and s[j2] != s[j]:
            ans -= 1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n=ii()
s=list(ss())
rarr = []
garr=[]
barr=[]

for i in range(n):
    if s[i]=="R":
        rarr.append(i)
    elif s[i]=="G":
        garr.append(i)
    else:
        barr.append(i)


ans=len(rarr)*len(barr)*len(garr)




if len(rarr)>len(garr):
    rarr,garr=garr,rarr
if len(barr)>len(garr):
    barr,garr=garr,barr

garr=Counter(garr)

for r in rarr:
    for b in barr:
        
        if r-b >0 and garr[r + r-b]:
            ans-=1
        
        elif b-r >0 and garr[b + b-r]:
            ans-=1
        
        if r-b >0 and (r-b)%2==0 and garr[b+((r-b)//2)]:
            ans-=1

        elif b-r >0 and (b-r)%2==0 and garr[r+((b-r)//2)]:
            ans-=1
        
        if r-b>0 and garr[b-(r-b)]:
            ans-=1
        elif b-r>0 and garr[r-(b-r)]:
            ans-=1

print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n=ii()
s=list(ss())
rarr = []
garr=[]
barr=[]

for i in range(n):
    if s[i]=="R":
        rarr.append(i)
    elif s[i]=="G":
        garr.append(i)
    else:
        barr.append(i)


ans=len(rarr)*len(barr)*len(garr)




if len(rarr)>len(garr):
    rarr,garr=garr,rarr
if len(barr)>len(garr):
    barr,garr=garr,barr

garr=Counter(garr)

for r in rarr:
    for b in barr:
        
        if r-b >0 and garr[r + r-b]:
            ans-=1
        
        elif b-r >0 and garr[b + b-r]:
            ans-=1
        
        if r-b >0 and (r-b)%2==0 and garr[b+((r-b)//2)]:
            ans-=1

        elif b-r >0 and (b-r)%2==0 and garr[r+((b-r)//2)]:
            ans-=1
        
        if r-b>0 and garr[b-(r-b)]:
            ans-=1
        elif b-r>0 and garr[r-(b-r)]:
            ans-=1

print(ans)

Solution (Python):
n = int(input())
s = input()
r = len([i for i in range(n) if s[i] == 'R'])
g = len([i for i in range(n) if s[i] == 'G'])
b = len([i for i in range(n) if s[i] == 'B'])
ans = r * g * b
sub = 0
for j in range(n):
    for i in range(j):
        if s[i] == s[j]:
            continue
        k = 2 * j - i
        if k >= n or ((s[i] == s[k]) or (s[j] == s[k])):
            continue
        sub += 1
print(ans - sub)

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

cnt_r = s.count('R')
cnt_g = s.count('G')
cnt_b = s.count('B')

ans = cnt_r * cnt_g * cnt_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[i] != s[k]:
            ans -= 1

print(ans)

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

num_r = S.count('R')
num_g = S.count('G')
num_b = S.count('B')

ans = num_r * num_g * num_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[j]!=S[k] and S[k]!=S[i]:
      ans -= 1

print(ans)

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

nR = 0
nG = 0
nB = 0

for s in S:
    if s == "R":
        nR += 1
    elif s == "G":
        nG += 1
    elif s == "B":
        nB += 1
        
c1pat = nR*nG*nB

mcount = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = j + (j-i)
        if k < N:

            if set([S[i],S[j],S[k]]) == set(["R","G","B"]):
                mcount += 1
                
                
print(c1pat - mcount)

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

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

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 - 1):
            j = i + d
            k = j + d
            if k < N and 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=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b
for i in range(n):
    for j in range(n):
        if i+j+j>=n:
            break
        s1=s[i]
        s2=s[i+j]
        s3=s[i+j*2]
        if s1!=s2 and s2!=s3 and s1!=s3:
            ans-=1
print(ans)

Solution (Python):



from math import gcd
from functools import reduce
from itertools import product 

from collections import defaultdict 
n=int(input())
s=input()
 
ret=0
 
for i,x in enumerate(s):
	for j,y in enumerate(s[i+1:],start=i+1):
		if x==y:
			continue
		if i+(j-i)*2 < n :
			z=s[i+(j-i)*2]
			if y!=z!=x:
				ret-=1
 
dic=defaultdict(lambda:0)
for x in s:
	if not x in dic: 
		dic[x]=0
	dic[x]+=1

f=dic.items()
for (i,x),(j,y),(k,z) in product(f,f,f):
	if i<j<k:
		ret+=x*y*z
print(ret)

Solution (Python):
import itertools
import bisect

N = int(input())
S = input()
R_set = set()
G_set = set()
B_set = set()
R = []
G = []
B = []
R_ids = [0] * N
G_ids = [0] * N
B_ids = [0] * N

for i, s in enumerate(S):
    if s == 'R':
        R_set.add(i)
        R.append(i)
        R_ids[i] = 1
    elif s == 'G':
        G_set.add(i)
        G.append(i)
        G_ids[i] = 1
    else:
        B_set.add(i)
        B.append(i)
        B_ids[i] = 1


R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]
G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]
B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]

r = (R_set, R, R_ids)
g = (G_set, G, G_ids)
b = (B_set, B, B_ids)

def count_rgb(r, g, b):
    res = 0
    r_set, _r, r_ids = r
    g_set, _g, g_ids = g
    b_set, _b, b_ids = b
    for r_idx in _r:
        start = bisect.bisect_left(_g, r_idx)
        for g_idx in _g[start:]:
            diff = g_idx - r_idx
            num = b_ids[g_idx]
            if g_idx+diff in b_set:
                num -= 1
            res += num
    return res
            

ans = 0
for rgb in itertools.permutations((r, g, b), 3):
    ans += count_rgb(rgb[0], rgb[1], rgb[2])

print(ans)

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

solution()

Solution (Python):
def solution():
    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+1):
            k = (j - i) + j
            if k < 1 or k >= n:
                continue
            if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:              
                ans -= 1
    print(ans)

solution()

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(n):
        if i+j+j>=n:
            break
        s1=s[i]
        s2=s[i+j]
        s3=s[i+j*2]
        if s1!=s2 and s2!=s3 and s1!=s3:
            ans-=1
print(ans)

Solution (Python):
n = int(input())
S = input()
k = 0
num = 0
r, g, b = 0, 0, 0
for s in S:
    if s == "R": r += 1
    elif s == "G": g += 1
    else: b += 1
whole = r * g * b
for i in range(n-2):
    for j in range(i+1,n-1):
        k = 2 * j - i
        if k >= n:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            num += 1
print(whole - num)

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

print(count)

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

print(count)

Solution (Python):
import sys
from collections import *
import heapq
import math
from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
s=input()
dic=defaultdict(list)
for i in range(n):
    if not s[i] in dic:
        dic[s[i]]=[i]
    else:
        dic[s[i]].append(i)

se=set(dic["B"])

ans=len(dic["R"])*len(dic["G"])*len(dic["B"])
for i in range(len(dic["R"])):
    for j in range(len(dic["G"])):
        l,r=min(dic["R"][i],dic["G"][j]),max(dic["R"][i],dic["G"][j])
        if r+(r-l) in se:
            ans-=1
        if l-(r-l) in se:
            ans-=1
        if (r-l)%2==0 and l+(r-l)//2 in se:
            ans-=1
print(ans)

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

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


ans = r * g * b


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

print(ans - c)

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

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


ans = r * g * b


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

print(ans - c)

Solution (Python):
def SearchCombination_RGB(Str):
    Sum_Comb = 0
    Num_R = 0
    Num_G = 0
    Num_B = 0
    for i in range(len(Str)):
        if(Str[i] =='R'):
            Num_R +=1
        elif(Str[i] == 'G'):
            Num_G += 1
        else:
            Num_B += 1
    Num_Comb = Num_R * Num_G * Num_B
    
    if(Num_Comb ==0):
        return 0
    for Pos1 in range(0, len(Str)-2):
        for Pos2 in range(Pos1+1, len(Str)-1):
            if(Str[Pos1] != Str[Pos2]):
                Pos3 = 2 * Pos2-Pos1
                if(Pos3 < len(Str) and Str[Pos3]!= Str[Pos1] and Str[Pos3] != Str[Pos2]):
                    Sum_Comb +=1

    return Num_Comb - Sum_Comb

Str = [input() for i in range(2)]



print( SearchCombination_RGB(Str[1].strip()) )

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

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

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

print(d_rgb_triplets())

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

if n<4:
    print(0)
else:
    r=0
    g=0
    ans=0
    for i in range(n):
        if s[i]=="R":
            r+=1
        elif s[i]=="G":
            g+=1
        if i>n-3:
            continue
        for k in range(1,(n-i-1)//2+1):
            if s[i]==s[i+k]:
                continue
            else:
                if s[i+k]==s[i+2*k] or s[i]==s[i+2*k]:
                    continue
                else:
                    ans-=1

    ans+=r*g*(n-r-g)
    print(max(0,ans))

Solution (Python):
n = int(input())
s = input()
c = s.count('R')*s.count('G')*s.count('B')

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

print(c)

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()
c = s.count('R')*s.count('G')*s.count('B')

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

print(c)

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

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

R=[]
G=set()
B=set()

from collections import defaultdict
Bd = defaultdict(lambda: -1)


for i in range(N):
	c = S[i]
	if c=='R': R.append(i)
	if c=='G': G.add(i)
	if c=='B': B.add(i)


p(R)
p(G)
p(B)

lenB = len(B)

ans=0
for i in R:
	for j in G:
		a = (i+j)/2
		b = (i*2-j)
		c = (j*2-i)
		
		x = lenB
		if (a in B): x -= 1
		if (b in B): x -= 1
		if (c in B): x -= 1
		
		ans += x

print(ans)

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

d = {"R": 0, "G": 0, "B": 0}
for i in range(N):
    d[S[i]] += 1
    
ans = d["R"] * d["G"] * d["B"]

cnt = 0
for i in range(1, N - 1):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k > N or S[i - 1] == S[j - 1]:
            continue
            
        if S[i - 1] != S[k - 1] and S[j - 1] != S[k - 1]:
            cnt += 1
            
print(ans - cnt)

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

    red, green, blue = 0, 0, 0
    for ch in S:
        if ch == 'R':
            red += 1
        elif ch == 'G':
            green += 1
        elif ch == 'B':
            blue += 1
    total = red * green * blue

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

print(d_rgb_triplets())

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

ans = 0
rgb_idx = {
    'R': [],
    'G': [],
    'B': [],
}
for i in range(n):
    if s[i] == 'R':
        rgb_idx['R'].append(i)
    elif s[i] == 'G':
        rgb_idx['G'].append(i)
    else:
        rgb_idx['B'].append(i)

for i in range(len(rgb_idx['R'])):
    for j in range(len(rgb_idx['G'])):
        ans += len(rgb_idx['B'])
        
        rnum = rgb_idx['R'][i]
        gnum = rgb_idx['G'][j]
        if gnum-rnum>0:
            g = gnum
            l = rnum
        else:
            g = rnum
            l = gnum
        dist = abs(gnum-rnum)
        if n > g+dist and s[g+dist]=='B':
            ans -= 1
        if 0 <= l-dist and s[l-dist]=='B':
            ans -= 1
        if (g+l)%2==0 and s[int((g+l)/2)]=='B':
            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(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=g=b=0
for i in s:
  if i=="R":r+=1
  elif i=="G":g+=1
  else:b+=1
ans=r*g*b
for i in range(1,n//2+2):
  for j in range(n):
    if j+i+i>=n:break
    if not(s[j]==s[j+i] or s[j+i]==s[j+i+i] or s[j]==s[j+i+i]):ans-=1
print(ans)