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 sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
  return list(map(int,input().split()))
def m(): 
  return map(int,input().split())
def onem(): 
  return int(input())
def s(x): 
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): 
  return " ".join(map(str,x))
def max2(x): 
  return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n = onem()

s = input()[:-1]

R = [0 for _ in range(n)]
B = [0 for _ in range(n)]
G = [0 for _ in range(n)]

co = 0

if s[0] == "R":
  R[0] = 1
elif s[0] == "G":
  G[0] = 1
else:
  B[0] = 1

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

for i in range(1,n-1):
  if s[i] == "G":
    for j in range(i):
      if s[j] == "R":
        un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "B" else 0)
        co += B[-1] - B[i] - un
      elif s[j] == "B":
        un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "R" else 0)
        co += R[-1] - R[i] - un

  elif s[i] == "R":
    for j in range(i):
      if s[j] == "G":
        un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "B" else 0)
        co += B[-1] - B[i] - un
      elif s[j] == "B":
        un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "G" else 0)
        co += G[-1] - G[i] - un

  elif s[i] == "B":
    for j in range(i):
      if s[j] == "G":
          un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "R" else 0)
          co += R[-1] - R[i] - un
      elif s[j] == "R":
          un = (1 if (i + (i-j) < n) and s[i + (i - j)] == "G" else 0)
          co += G[-1] - G[i] - un
    
print(co)

Solution (Python):
n=int(input())
s=input()
r,g,b=[],[],[]
for i in range(n):
    if s[i]=="R":
        r.append(i)
    elif s[i]=="G":
        g.append(i)
    else:
        b.append(i)
b=set(b)
lb=len(b)
ans=0
for i in r:
    for j in g:
        x,y=min(i,j),max(i,j)
        ans_=lb
        if x-(y-x) in b:
            ans_-=1
        if y+(y-x) in b:
            ans_-=1
        if (x%2==y%2) and (y-x)//2+x in b:
            ans_-=1
        ans+=ans_
print(ans)

Solution (Python):

import sys
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(N: int, S: str):
    c = Counter(S)
    count = c["R"] * c["G"]*c["B"]
    for i in range(1, (N-3)//2+2):
        for j in range(N):
            if j+2*i >= N:
                break
            if not (S[j] == S[j+i] or S[j] == S[j+i+i] or S[j+i] == S[j+i+i]):
                count -= 1
    print(count)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)


if __name__ == '__main__':
    main()

Solution (Python):


def resolve():
    N = int(input())
    R=set()
    G=set()
    B=set()
    S = input()
    for index,i in enumerate(S):
        if i == "R":
            R.add(index)
        elif i == "G":
            G.add(index)
        else:
            B.add(index)
    count = len(R)* len(G)* len(B)

    for red in R:
        for blue in B:
            mx = max(red,blue)
            mn = min(blue,red)
            step = mx - mn


            if (mx + step) in G:
                count -= 1
            
            if (mn - step) in G:
                count -= 1
            if step % 2 ==0 and (mn + step // 2) in G:
                count -=1

    print(count)



                

            







if __name__ == "__main__":
    resolve()

Solution (Python):
def main():
    from itertools import accumulate as ac
    n = int(input())
    s = input()
    R_list = [0]*n
    G_list = [0]*n
    B_list = [0]*n
    for i, j in enumerate(s):
        if j == "R":
            R_list[i] += 1
        elif j == "G":
            G_list[i] += 1
        else:
            B_list[i] += 1
    R_list = list(ac(R_list[::-1]))[::-1]
    G_list = list(ac(G_list[::-1]))[::-1]
    B_list = list(ac(B_list[::-1]))[::-1]
    RG_list = [0]*n
    RB_list = [0]*n
    GR_list = [0]*n
    GB_list = [0]*n
    BR_list = [0]*n
    BG_list = [0]*n
    for i, j in enumerate(s[:-1]):
        if j == "R":
            RG_list[i] += G_list[i+1]
            RB_list[i] += B_list[i+1]
        elif j == "G":
            GR_list[i] += R_list[i+1]
            GB_list[i] += B_list[i+1]
        else:
            BR_list[i] += R_list[i+1]
            BG_list[i] += G_list[i+1]
    RG_list = list(ac(RG_list[::-1]))[::-1]
    RB_list = list(ac(RB_list[::-1]))[::-1]
    GR_list = list(ac(GR_list[::-1]))[::-1]
    GB_list = list(ac(GB_list[::-1]))[::-1]
    BR_list = list(ac(BR_list[::-1]))[::-1]
    BG_list = list(ac(BG_list[::-1]))[::-1]
    ans = 0
    for i in range(n-1):
        if s[i] == "R":
            ans += GB_list[i+1]
            ans += BG_list[i+1]
        elif s[i] == "G":
            ans += RB_list[i+1]
            ans += BR_list[i+1]
        else:
            ans += RG_list[i+1]
            ans += GR_list[i+1]
    for i in range(n):
        for j in range(i+1, n):
            k = 2*j-i
            if k >= n:
                continue
            else:
                if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                    ans -= 1
    print(ans)


main()

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for j in range(1, N - 1):
    for v in range(1, min(j+1, N - j)):
        if S[j - v] != S[j] and S[j + v] != S[j] and S[j - v] != S[j + v]:
            ans -= 1

print(ans)

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

C = Counter(S)
ans = C['R']*C['G']*C['B']

for w in range(1, (N+1)//2+1):
    for i in range(N-(w*2)):
        if S[i]!=S[i+w] and S[i]!=S[i+2*w] and S[i+w]!=S[i+2*w]:
            ans -= 1
print(ans)

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

total = 0
for j in range(1, n-1):
    left_d = {"R": set(), "G": set(), "B": set()}
    for k in range(j+1, n):
        left_d[s[k]].add(k-j)
    for i in range(j):
        if s[j] == s[i]:
            continue
        if s[i] == "R" and s[j] == "G" or s[i] == "G" and s[j] == "R":
            target = "B"
        if s[i] == "R" and s[j] == "B" or s[i] == "B" and s[j] == "R":
            target = "G"
        if s[i] == "B" and s[j] == "G" or s[i] == "G" and s[j] == "B":
            target = "R"
        total += len(left_d[target])
        if j-i in left_d[target]:
            total -= 1
        

print(total)

Solution (Python):
N=int(input())
S=list(input())
counts = 0
tot = 0
r = [0]*N
b = [0]*N
g = [0]*N
R=0
B=0
G=0
"""
if S[0] == "R":
    r[0] +=1
if S[0] == "G":
    g[0] +=1
if S[0] == "B":
    b[0] +=1

"""
for i in range(N):
    if S[i] == "R":
        R+=1

    if S[i] == "B":
        B+=1
        
    if S[i] == "G":
        G+=1



        

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


print(R*B*G-counts)

Solution (Python):
N, = (int(x) for x in input().split())
S, = ((x) for x in input().split())


import collections

cc = collections.Counter(S)
if len(cc) != 3:
    sol = 0
else:
    sol = 1
    for x in cc.values():
        sol *= x

for i in range(N):
    for j in range(i + 2, N, 2):
        if S[i] == S[j]:
            continue
        k = (i + j) // 2
        if S[k] not in (S[i], S[j]):
            sol -= 1

print(sol)

Solution (Python):
n = int(input())
string = input().strip()
r = string.count('R')
g = string.count('G')
h = string.count('B')

ans = r * g * h

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

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

RGBall = R*G*B
if N % 2 == 0:
    X = N // 2 - 1
else:
    X = N // 2
eql = 0
for i in range(1,X+1):
    for j in range(N):
        a = S[j]
        b = S[j+i]
        c = S[j+2*i]
        
        if a != b and b != c and c != a:
            eql += 1
        if j + 2*i == N-1:
            break

ans = RGBall - eql
print(ans)

Solution (Python):
import sys
from bisect import bisect_right as br
from bisect import bisect_left as bl
input = sys.stdin.readline
N = int(input())
S = list(input())[: -1]
rs = []
gs = []
bs = []
res = 0
for i in range(N):
  if S[i] == "R": rs.append(i)
  if S[i] == "G": gs.append(i)
  if S[i] == "B": bs.append(i)

for i in range(N):
  for j in range(i + 1, N):
    if S[i] == S[j]: continue
    d = j - i
    k = 0
    x = 0
    t = 0
    if S[i] == "R":
      if S[j] == "G":
        k = br(bs, j)
        x = bl(bs, j + d)
        t = len(bs) - k
        if len(bs) > x:
          t -= (j + d) == bs[x]
      elif S[j] == "B": 
        k = br(gs, j)
        x = bl(gs, j + d)
        t = len(gs) - k
        if len(gs) > x:
          t -= (j + d) == gs[x]
    elif S[i] == "G":
      if S[j] == "R":
        k = br(bs, j)
        x = bl(bs, j + d)
        t = len(bs) - k
        if len(bs) > x:
          t -= (j + d) == bs[x]
      elif S[j] == "B":
        k = br(rs, j)
        x = bl(rs, j + d)
        t = len(rs) - k
        if len(rs) > x:
          t -= (j + d) == rs[x]
    elif S[i] == "B":
      if S[j] == "R":
        k = br(gs, j)
        x = bl(gs, j + d)
        t = len(gs) - k
        if len(gs) > x:
          t -= (j + d) == gs[x]
      elif S[j] == "G":
        k = br(rs, j)
        x = bl(rs, j + d)
        t = len(rs) - k
        if len(rs) > x:
          t -= (j + d) == rs[x]
    res += t
    
print(res)

Solution (Python):
from sys import stdin
input = stdin.readline

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

ans = 0

R = 0
G = 0
B = 0

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

ans += R * G * B

for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k <= j or k >= N:
            continue
        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()

Lr = [0]*(N+2)
Lg = [0]*(N+2)
Lb = [0]*(N+2)
Rr = [0]*(N+2)
Rg = [0]*(N+2)
Rb = [0]*(N+2)
for i,s in enumerate(S):
    Lr[i+1] = Lr[i]
    Lg[i+1] = Lg[i]
    Lb[i+1] = Lb[i]
    if s=="R":
        Lr[i+1] += 1
    elif s=="G":
        Lg[i+1] += 1
    elif s=="B":
        Lb[i+1] += 1

for i,s in enumerate(S[::-1]):
    i = N-i
    Rr[i] = Rr[i+1]
    Rg[i] = Rg[i+1]
    Rb[i] = Rb[i+1]
    if s=="R":
        Rr[i] += 1
    elif s=="G":
        Rg[i] += 1
    elif s=="B":
        Rb[i] += 1

ans = 0
for i,s in enumerate(S):
    if s=="R":
        ans += Lg[i+1]*Rb[i+1] + Lb[i+1]*Rg[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "B" and S[i+j] == "G") or (S[i-j] == "G" and S[i+j] == "B"):
                ans -= 1
    elif s=="G":
        ans += Lr[i+1]*Rb[i+1] + Lb[i+1]*Rr[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "B" and S[i+j] == "R") or (S[i-j] == "R" and S[i+j] == "B"):
                ans -= 1
    elif s=="B":
        ans += Lg[i+1]*Rr[i+1] + Lr[i+1]*Rg[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "R" and S[i+j] == "G") or (S[i-j] == "G" and S[i+j] == "R"):
                ans -= 1
print(ans)

Solution (Python):
def f(x,y,z):
    ans=0
    for p in x:
        for q in y:
            if p<q and 2*q-p in z: ans+=1
            elif q<p and 2*p-q in z: ans+=1
    return ans

n,s=int(input()),input()
r,g,b=set(),set(),set()
for i in range(n):
    if s[i]=="R": r.add(i)
    elif s[i]=="G": g.add(i)
    else: b.add(i)
print(len(r)*len(g)*len(b)-(f(r,g,b)+f(r,b,g)+f(g,b,r)) )

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):
    if 2 * i + 1 > N:
        break
    for x in range(N):
        if x + 2 * i + 1 > N:
            break
        if S[x] != S[x+i] and S[x] != S[x+(2*i)] and S[x+i] != S[x+(2*i)]:
            ans -= 1

print(ans)

Solution (Python):
def main():
    from bisect import bisect_left

    N = int(input())
    s = input()

    Rs = []
    Gs = []
    Bs = []
    for i, c in enumerate(s):
        if c == 'R':
            Rs.append(i)
        elif c == 'G':
            Gs.append(i)
        else:
            Bs.append(i)

    s += 'Z' * 4000

    ans = 0
    for mid in Rs:
        for g in Gs:
            if g >= mid: break
            ans += len(Bs) - bisect_left(Bs, mid) - (1 if s[mid * 2 - g] == 'B' else 0)
        for b in Bs:
            if b >= mid: break
            ans += len(Gs) - bisect_left(Gs, mid) - (1 if s[mid * 2 - b] == 'G' else 0)
    for mid in Gs:
        for r in Rs:
            if r >= mid: break
            ans += len(Bs) - bisect_left(Bs, mid) - (1 if s[mid * 2 - r] == 'B' else 0)
        for b in Bs:
            if b >= mid: break
            ans += len(Rs) - bisect_left(Rs, mid) - (1 if s[mid * 2 - b] == 'R' else 0)
    for mid in Bs:
        for r in Rs:
            if r >= mid: break
            ans += len(Gs) - bisect_left(Gs, mid) - (1 if s[mid * 2 - r] == 'G' else 0)
        for g in Gs:
            if g >= mid: break
            ans += len(Rs) - bisect_left(Rs, mid) - (1 if s[mid * 2 - g] == 'R' else 0)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n = int(input())
s = input()
cnt = Counter(s)
ans = cnt["R"] * cnt["G"] * cnt["B"]


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

print(ans-rgb)

Solution (Python):
N = int(input())
S = list(input())
r = 0
g = 0
b = 0

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

ans = r*g*b

for i in range(N):
    for j in range(i+1,N):
        if 2*j - i <= N - 1:
            t = [ S[i],S[j],S[2*j-i] ]
            if len(set(t)) == 3:
                ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
r, g, b = 0, 0, 0
for c in s:
    if c == "R": r += 1
    elif c == "G": g += 1
    else: b += 1

ans = r * g * b
for i in range(n-1):
    for j in range(i+1, n):
        k = j + j-i
        if k >= n: continue
        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()

rn = S.count('R')
gn = S.count('G')
bn = S.count('B')

eqn = 0
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[i] != S[k]:
            eqn += 1

ans = rn * gn * bn - eqn

print(ans)

Solution (Python):


N = int(input())
s = input()
res = 0
for j in range(1,N-1):
    char = s[j]
    left_list = ['B', 'R']
    right_list = ['R', 'B']
    if char == 'B':
        left_list = ['G', 'R']
        right_list = ['R', 'G']
    elif char == 'R':
        left_list = ['B', 'G']
        right_list = ['G', 'B']
    for left, right in zip(left_list, right_list):
        left_idx = [j-i for i, x in enumerate(s[:j]) if x==left]
        right_idx = [i+1 for i, x in enumerate(s[j+1:]) if x==right]
        left_len = len(left_idx)
        right_len = len(right_idx)
        tmp = left_len * right_len
        tmp += len(set(left_idx) | set(right_idx)) - left_len - right_len
        res += tmp
print(res)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit

MOD = 10**9 + 7

N = int(readline())
S = np.array(list(read().rstrip()), np.int8)

R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))

@njit
def f(S):
    N = len(S)
    ret = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = j + j - i
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ret += 1
    return ret

answer = R * B * G - f(S)
print(answer)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
"""
i-j-k間が等しくなる組み合わせを求めてから、
Rの数*G*B - それ
で求まる？
"""
n = int(readline())
s = readline().rstrip().decode('utf-8')

if n == 3:
    print(0)
    exit()
dic1 = dict()
for i in s:
    if i in dic1:
        dic1[i] += 1
    else:
        dic1[i] = 1

if len(dic1) != 3:
    print(0)
    exit()
res = 1
for i in dic1.values():
    res *= i

i = 0
x,y,z = 0,0,0
count = 0
while True:
    if i >= n:
        break
    j = 1
    while True:
        x = i
        y = i+j
        z = i+2*j
        if z >= n:
            break
        else:
            if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
                count += 1 
        j += 1
        
    i += 1

print(res - count)

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
s = input()

color = 'RGB'
c_list = [[i for i in range(n) if s[i] == color[j]] for j in range(3)]


ans1 = len(c_list[0]) * len(c_list[1]) * len(c_list[2])
ans2 = 0
for i in range(n):
    for j in range(1, n):
        if i + 2 * j >= n:
            continue
        if s[i] != s[i + j] and s[i + j] != s[i + 2 * j] and s[i +
                                                               2 * j] != s[i]:
            ans2 += 1


ans = ans1 - ans2
print(ans)

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

sl = list(input())

sum_r = sl.count("R")
sum_g = sl.count("G")
sum_b = sl.count("B")

rl = []
gl = []
bl = []

for s in sl:
    if s == "R":
        sum_r -= 1
    if s == "G":
        sum_g -= 1
    if s == "B":
        sum_b -= 1
    rl.append(sum_r)
    gl.append(sum_g)
    bl.append(sum_b)
ans = 0
for i in range(n):
    for j in range(i+1, n):
        k = j + j - i
        if sl[i] == sl[j]:
            continue
        if "R" not in [sl[i], sl[j]]:
            ans += rl[j]
            if k < n:
                if sl[k] == "R":
                    ans -= 1
        if "G" not in [sl[i], sl[j]]:
            ans += gl[j]
            if k < n:
                if sl[k] == "G":
                    ans -= 1
        if "B" not in [sl[i], sl[j]]:
            ans += bl[j]
            if k < n:
                if sl[k] == "B":
                    ans -= 1
print(ans)

Solution (Python):

from collections import Counter, defaultdict
from math import gcd
import bisect

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

char_to_indices = defaultdict(list)
for i, c in enumerate(S):
    char_to_indices[c].append(i)

total = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        for c in ('G', 'B', 'R'):
            if c not in (S[i], S[j]):
                search = c
                break
        indices = char_to_indices[search]
        idx = bisect.bisect_left(indices, j+1)
        if idx != len(indices):
            total += len(indices) - idx
        exact_idx = bisect.bisect_left(indices, j + (j - i))
        if exact_idx != len(indices) and indices[exact_idx] == (2 * j - i):
            total -= 1
print(total)

Solution (Python):
from bisect import bisect_left, bisect_right

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

R = []
G = []
B = []

res = 0

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

for i in range(1, N - 1):
    if S[i] == 'R':
        res += bisect_left(G, i) * (len(B) - bisect_left(B, i))
        res += bisect_left(B, i) * (len(G) - bisect_left(G, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'G' and S[i + j] == 'B':
                res -= 1
            elif S[i - j] == 'B' and S[i + j] == 'G':
                res -= 1
    if S[i] == 'G':
        res += bisect_left(B, i) * (len(R) - bisect_left(R, i))
        res += bisect_left(R, i) * (len(B) - bisect_left(B, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'B' and S[i + j] == 'R':
                res -= 1
            elif S[i - j] == 'R' and S[i + j] == 'B':
                res -= 1
    if S[i] == 'B':
        res += bisect_left(R, i) * (len(G) - bisect_left(G, i))
        res += bisect_left(G, i) * (len(R) - bisect_left(R, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'G' and S[i + j] == 'R':
                res -= 1
            elif S[i - j] == 'R' and S[i + j] == 'G':
                res -= 1

print(res)

Solution (Python):
n = int(input())
s = input()
L = {"R": 0, "G": 0, "B": 0}
R = {"R": s.count("R"), "G": s.count("G"), "B": s.count("B")}
ans = 0
L[s[0]]+=1
R[s[0]]-=1
for i in range(1, n - 1):
	t = s[i]
	if t == "R":
		ans += L["G"] * R["B"] + L["B"] * R["G"]
	elif t == "G":
		ans += L["R"] * R["B"] + L["B"] * R["R"]
	else:
		ans += L["R"] * R["G"] + L["G"] * R["R"]
	for j in range(min(i,n-1-i)):
		ans -= t!=s[i-j-1]!=s[i+j+1]!=t
	L[t]+=1
	R[t]-=1
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
s = input().rstrip()

c = Counter(list(s))

num_r = c['R']
num_g = c['G']
num_b = c['B']

bad = 0
for i in range(n-2):
    for j in range(i+1, n):
        k = j + (j - i)
        if k >= n:
            break

        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            bad += 1

print(num_r * num_g * num_b - bad)

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

Solution (Python):

import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N = ir()
S = list(sr())
counter = Counter(S)
r = counter['R']
g = counter['G']
b = counter['B']
answer = r * g * b
for i in range(N-1):
    for j in range(i+1, N):
        if j + (j - i) >= N:
            continue
        x = S[i]; y = S[j]; z = S[j+j-i]
        if x != y and y != z and z != x:
            answer -= 1

print(answer)

Solution (Python):
import sys


def main():
    N = int(input())
    S = input()
    col = [[0 for _ in range(N+1)] for _ in range(3)]
    
    for i,st in enumerate(S):
        if st == "R":
            col[0][i+1] += 1
        elif st == "G":
            col[1][i+1] += 1
        else:
            col[2][i+1] += 1

    for i in range(3):
        for j in range(N):
            col[i][j+1] += col[i][j]
    
    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            if S[i] == S[j]:
                continue
            elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                ans += col[2][-1]-col[2][j]
                if N>j+(j-i) and S[j+(j-i)] == "B":
                    ans -= 1
            elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                ans += col[0][-1]-col[0][j]
                if N>j+(j-i) and S[j+(j-i)] == "R":
                    ans -= 1
            else:
                ans += col[1][-1]-col[1][j]
                if N>j+(j-i) and S[j+(j-i)] == "G":
                    ans -= 1
    
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    s = input().strip()
    r = g = b = 0
    for c in s:
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        elif c == 'B':
            b += 1
    ans = r * b * g

    valid = frozenset('RGB')
    for i in range(n):
        for j in range(1, min(i, n-1-i)+1):
            if frozenset(s[i] + s[i-j] + s[i+j]) == valid:
                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
ans = 0

for i in range(n):
    if s[i] == 'R':
        r += 1
    if s[i] == 'G':
        g += 1
    if s[i] == 'B':
        b += 1
rgb = {
    'R': r,
    'G': g,
    'B': b
}
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(n):
        if s[j] != 'G':
            continue
        mem = 0
        if (i+j)%2 == 0:
            k = (i+j)//2
            if s[k] == 'B':
                mem += 1
        if 2*i-j >= 0 and 2*i-j < n:
            if s[2*i-j] == 'B':
                mem += 1
        if 2*j-i >= 0 and 2*j-i < n:
            if s[2*j-i] == 'B':
                mem += 1
        ans += b-mem


print(ans)

Solution (Python):
n = int(input())
s = input()
rl = 0
gl = 0
bl = 0
for i in range(n):
	if s[i] == 'R':
		rl += 1
	elif s[i] == 'G':
		gl += 1
	else:
		bl += 1
ans = rl*bl*gl
cnt = 0
for i in range(1,n//2+3):
	j = 0
	while j+i+i < n:
		if (s[j] == 'R'):
			if (s[j+i] == 'B'):
				if (s[j+i+i] == 'G'):
					cnt += 1
			elif (s[j+i] == 'G'):
				if (s[j+i+i] == 'B'):
					cnt += 1
		elif (s[j] == 'G'):
			if (s[j+i] == 'B'):
				if (s[j+i+i] == 'R'):
					cnt += 1
			elif (s[j+i] == 'R'):
				if (s[j+i+i] == 'B'):
					cnt += 1
		else:
			if (s[j+i] == 'R'):
				if (s[j+i+i] == 'G'):
					cnt += 1
			elif (s[j+i] == 'G'):
				if (s[j+i+i] == 'R'):
					cnt += 1
		j+=1
print(ans-cnt)

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

r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b

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

print(ans)

Solution (Python):
n=int(input())
s=[i for i in input()]
import collections
c=collections.Counter(s)
if len(c)<3:
    print(0)
    quit()
ans=c["R"]*c["G"]*c["B"]
for i in range(1,(n+1)//2):
    for j in range(n):
        if j+i+i>=n:
            break
        if s[j]!=s[j+i] and s[j]!=s[j+i+i] and s[j+i]!=s[j+i+i]:
            ans-=1
print(ans)

Solution (Python):
import sys
from collections import Counter


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


def main():
    N = int(input())
    S = input()
    c = Counter(S)
    ans = c.get("R", 0) * c.get("G", 0) * c.get("B", 0)

    out = 0
    for start in range(N):
        for mid in range(start + 1, N):
            d = mid - start
            last = d + mid
            if last < N:
                if S[start] != S[mid] and S[mid] != S[last] and S[last] != S[start]:
                    out += 1

    print(ans - out)


if __name__ == "__main__":
    main()

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

count=[0]*3
for i in range(N-1,-1,-1):
    if S[i]=="R":
        count[0]+=1
    elif S[i]=="G":
        count[1]+=1
    else:
        count[2]+=1
        
cnt=0
for i in range(1,N//2+1):
    for j in range(N-2*i):
        a,b,c=S[j],S[j+i],S[j+2*i]
        if a!=b and a!=c and c!=b:
            cnt+=1

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

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
s=input().rstrip()
r=[0]
g=[0]
b=[0]
for i in range(n-1,-1,-1):
    if s[i]=='R':
        r.append(r[-1]+1)
        g.append(g[-1])
        b.append(b[-1])
    elif s[i]=='G':
        r.append(r[-1])
        g.append(g[-1]+1)
        b.append(b[-1])
    elif s[i]=='B':
        r.append(r[-1])
        g.append(g[-1])
        b.append(b[-1]+1)
r.reverse()
g.reverse()
b.reverse()
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        if s[i]!='R' and s[j]!='R':
            d=j-i
            k=j+d
            ans+=r[j+1]
            if k<n:
                if s[k]=='R':
                    ans-=1
        elif s[i]!='G' and s[j]!='G':
            d=j-i
            k=j+d
            ans+=g[j+1]
            if k<n:
                if s[k]=='G':
                    ans-=1
        elif s[i]!='B' and s[j]!='B':
            d=j-i
            k=j+d
            ans+=b[j+1]
            if k<n:
                if s[k]=='B':
                    ans-=1
print(ans)

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

nR = S.count('R')
nG = S.count('G')
nB = S.count('B')

ans = nR*nG*nB

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

print(ans)

Solution (Python):
n=int(input())
s=input()
rgb=[(0,0,0)]
for i in range(n):
	a=rgb[-1][0]
	b=rgb[-1][1]
	c=rgb[-1][2]
	if s[i]=="R":
		a+=1
	elif s[i]=="G":
		b+=1
	else:
		c+=1
	rgb.append((a,b,c))
ans=0
for i in range(n-2):
	for j in range(i+1,n-1):
		if s[i]!=s[j]:
			if "B" not in (s[i],s[j]):
				ans+=rgb[-1][2]-rgb[j+1][2]
				if 2*j-i<n:
					if s[2*j-i]=="B":
						ans-=1
			elif "G" not in (s[i],s[j]):
				ans+=rgb[-1][1]-rgb[j+1][1]
				if 2*j-i<n:
					if s[2*j-i]=="G":
						ans-=1
			else:
				ans+=rgb[-1][0]-rgb[j+1][0]
				if 2*j-i<n:
					if s[2*j-i]=="R":
						ans-=1
print(ans)