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()
d = dict()
for x in 'RGB':
    d[x] = s.count(x)
ans = 0
for j in range(n-1):
    for i in range(j):
        if s[i] != s[j]:
            for t in 'RGB':
                if s[i] != t != s[j]:
                    break
            ans += d[t] - ((2*j - i) < n and t == s[2*j - i])
    d[s[j]] -= 1
print(ans)

Solution (Python):
def resolve():
    n = int(input())
    s = input()
    r = [0] * n
    g = [0] * n
    b = [0] * n
    for i in range(n):
        if s[i] == "R":
            r[i] = 1
        elif s[i] == "G":
            g[i] = 1
        else:
            b[i] = 1
    ans = sum(r)*sum(g)*sum(b)

    for i in range(1, n // 2 + 1):
        j = 0
        while j + 2 * i <= n - 1:
            if {s[j], s[j + i], s[j + 2 * i]} == {"R", "G", "B"}:
                ans -= 1
            j += 1
    print(ans)
resolve()

Solution (Python):
N = int(input())
s = input()
R = s.count('R')
G = s.count('G')
B = s.count('B')

count = R*G*B
cnt = 0
for i in range(len(s)):
    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]:
            count -= 1
print(count)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()

nR = S.count("R")
nG = S.count("G")
nB = S.count("B")
ans = nR*nG*nB
for i in range(N):  
	for j in range(1, N//2+1):  
		if i+2*j >= N:
			break
		a, b, c = S[i], S[i+j], S[i+2*j]
		if a != b and b != c and c != a:
			ans -= 1
print(ans)

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

for i in range(N):
    try:
        for j in range(N):
            if S[i] != S[i + j] and S[i] != S[i + 2 * j] and S[i+j] != S[i + 2 * j]:
                countRGB -= 1
    except:
        pass
print(countRGB)

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]

ans = 0
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
        ans += num
    
    start = bisect.bisect_left(B, r_idx)
    for b_idx in B[start:]:
        diff = b_idx - r_idx
        num = G_ids[b_idx]
        if b_idx+diff in G_set:
            num -= 1
        ans += num

for g_idx in G:
    start = bisect.bisect_left(R, g_idx)
    for r_idx in R[start:]:
        diff = r_idx - g_idx
        num = B_ids[r_idx]
        if r_idx+diff in B_set:
            num -= 1
        ans += num
    
    start = bisect.bisect_left(B, g_idx)
    for b_idx in B[start:]:
        diff = b_idx - g_idx
        num = R_ids[b_idx]
        if b_idx+diff in R_set:
            num -= 1
        ans += num

for b_idx in B:
    start = bisect.bisect_left(G, b_idx)
    for g_idx in G[start:]:
        diff = g_idx - b_idx
        num = R_ids[g_idx]
        if g_idx+diff in R_set:
            num -= 1
        ans += num
    
    start = bisect.bisect_left(R, b_idx)
    for r_idx in R[start:]:
        diff = r_idx - b_idx
        num = G_ids[r_idx]
        if r_idx+diff in G_set:
            num -= 1
        ans += num


print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
S = input()
counter = defaultdict(int)
for s in S:
    counter[s] += 1
total = counter["R"] * counter["G"] * counter["B"]
for i in range(N):
    for j in range(i, N):
        k = j + (j - i)
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            total -= 1
print(total)

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

R = S.count('R')
G = S.count('G')
B = S.count('B')
tmp = 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[j]!=S[k] and S[k]!=S[i]:
            tmp-=1
print(tmp + R*G*B)

Solution (Python):
from collections import defaultdict

N = int(input())
S = input()
counter = defaultdict(int)
for s in S:
    counter[s] += 1
total = counter["R"] * counter["G"] * counter["B"]
for i in range(N):
    for j in range(i, N):
        k = j + (j - i)
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            total -= 1
print(total)

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

b = set(b)
cnt = len(b) * len(r) * len(g)
for i in r:
    original = i
    for j in g:
        i = original
        if i > j:
            i, j = j, i
        d = j - i
        if j + d in b:
            cnt -= 1
        if i - d in b:
            cnt -= 1
        if d % 2 == 0 and (i + j) // 2 in b:
            cnt -= 1
print(cnt)

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

print(ans)

Solution (Python):
n = int(input())
s = input()
 
result = 0
 
if n <= 2:
    print(0)

for i in range(n-2):
    if s[i] == "R":
        a = s[i+1:].count("G")
        b = s[i+1:].count("B")
        result += a*b
        for j in range(1, (n+1-i)//2):
            if (s[i+j] == "G" and s[i+2*j] == "B") or (s[i+j] == "B" and s[i+2*j] == "G"):
                result -= 1
    elif s[i] == "G":
        a = s[i+1:].count("R")
        b = s[i+1:].count("B")
        result += a*b
        for j in range(1, (n+1-i)//2):
            if (s[i+j] == "R" and s[i+2*j] == "B") or (s[i+j] == "B" and s[i+2*j] == "R"):
                result -= 1
    elif s[i] == "B":
        a = s[i+1:].count("R")
        b = s[i+1:].count("G")
        result += a*b
        for j in range(1, (n+1-i)//2):
            if (s[i+j] == "R" and s[i+2*j] == "G") or (s[i+j] == "G" and s[i+2*j] == "R"):
                result -= 1
print(result)

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

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

Solution (Python):
import collections
N=int(input())
S=input()
lis=list(S)
C= collections.Counter(lis)
count=list(C.values())
c=0
for i in range(N):
    for j in range(i,(N+i-1) // 2+1):
        if(lis[i]!=lis[j]):
            if(lis[j]!=lis[2*j-i]):
                if(lis[i]!=lis[2*j-i]):
                    c+=1
if(len(count)==3):
    print(count[0]*count[1]*count[2]-c)
else:
    print(0)

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

Solution (Python):
import numpy as np
N = int(input())
S = input()

l_r = set()
l_g = set()
l_b = set()
for i in range(N):
  if S[i]=='R':
    l_r.add(i)
  if S[i]=='G':
    l_g.add(i)
  if S[i]=='B':
    l_b.add(i)

ans = len(l_r)*len(l_g)*len(l_b)

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      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

for i in range(n-1):
    for j in range(i+1,n):
        k=j+(j-i)
        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()
r = []
g = []
b = []
for i in range(N):
  if S[i] == 'R':
    r.append(i)
  elif S[i] == 'G':
    g.append(i)
  else:
    b.append(i)
ans = len(r) * len(g) * len(b)
sub = 0
for i in range(1,N//2+1):
  if 2 * i == N:
    break
  for k in range(N):
    if S[sub] != S[sub + i] and S[sub + i] != S[sub + 2 * i] and S[sub + 2 * i] != S[sub]:
      ans -= 1
    if sub + 2 * i + 1< N:
      sub += 1
    else:
      sub = 0
      break
print(ans)

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

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]

def notab(a, b):
    s = set((a,b))
    for c in "RGB":
        if c not in s:
            return c

n  = load()
s = list(load(str))

cntmap = dict()
for c in 'RGB':
    cnt = 0
    cntmap[c] = [0 for _ in range(n+1)]
    for i in range(n-1, -1, -1):
        if s[i] == c:
            cnt += 1
        cntmap[c][i] = cnt

cnt = 0
for i in range(0, n):
    first = s[i]
    for j in range(i + 1, n):
        if s[j] == first:
            continue
        second = s[j]
        target = notab(first, second)
        if j+(j-i) < n and s[j+(j-i)] == target:
            cnt += cntmap[target][j+1] - 1
        else:
            cnt += cntmap[target][j+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(1, 1+n//2):
    for j in range(1, n-i*2+1):
        if s[j-1] != s[j+i-1] and s[j-1] != s[j+i*2-1] and s[j+i-1] != s[j+i*2-1]:
                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 
cnt=0

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

Solution (Python):

import sys
input = sys.stdin.readline


def main():
    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 = (j - i) + j
            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):
n = int(input())
s = input()

r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count = r_count*g_count*b_count


for i,elem in enumerate(s):
    length = min(i,n-1-i)
    for j in range(1,length+1):
        elem_2 = s[i-j]
        elem_3 = s[i+j]
        if elem != elem_2 and elem != elem_3 and elem_2 != elem_3:
            count -= 1

print(count)

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

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

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

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

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

Solution (Python):
N = int(input())
S = input()
R = set()
G = set()
B = set()
for i in range(N):
  if S[i] == 'R':
    R.add(i)
  elif S[i] == 'G':
    G.add(i)
  else:
    B.add(i)


x = 0
for i in R:
  for j in G:
    if (i - j) % 2 == 0 and (i + j) // 2 in B:
      x -= 1
    if 2 * j - i in B:
      x -= 1
    if 2 * i - j in B:
      x -= 1
    
x += len(R) * len(B) * len(G)
print(x)

Solution (Python):
import sys
import numpy as np








def main():
    solve()


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

    ridxs = []
    gidxs = []
    bidxs = []

    rdic = dict()
    gdic = dict()
    bdic = dict()

    for i in range(N):
        s = S[i]
        if s == 'R':
            ridxs.append(i)
            rdic[i*2] = 1
        if s == 'G':
            gidxs.append(i)
            gdic[i*2] = 1
        if s == 'B':
            bidxs.append(i)
            bdic[i*2] = 1

    total = len(ridxs) * len(gidxs) * len(bidxs)

    for ri in ridxs:
        for gi in gidxs:
            if ri + gi in bdic:
                total -= 1

    for gi in gidxs:
        for bi in bidxs:
            if gi + bi in rdic:
                total -= 1

    for bi in bidxs:
        for ri in ridxs:
            if bi + ri in gdic:
                total -= 1

    print(total)
    return


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 = j - i + j
        if k < N:
            if {S[i], S[j], S[k]} == {"R", "G", "B"}:
                ans -= 1
print(ans)

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

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

ans = r*g*b

for i in range(n):
    for j in range(i+1,n):
        k = j + (j-i)
        if k<n and set([s[i],s[j],s[k]])==set(["R","G","B"]):
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if n <= k:
            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()

total = 0
m = 0

for j in range(1, N):

    ic = {
        "R": 0,
        "G": 0,
        "B": 0
    }

    kc = {
        "R": 0,
        "G": 0,
        "B": 0
    }

    m = 0

    for i in range(j-1, -1, -1):
        ic[S[i]] += 1
    
    for k in range(j+1, N):
        kc[S[k]] += 1

    for l in range(min(j+1, N-j)):
        if(S[j - l] != S[j + l] and S[j] != S[j + l] and S[j] != S[j - l]):
            m += 1

    if(S[j] == "R"):
        total += ic["G"] * kc["B"] + ic["B"] * kc["G"]
    elif(S[j] == "G"):
        total += ic["R"] * kc["B"] + ic["B"] * kc["R"]
    elif(S[j] == "B"):
        total += ic["R"] * kc["G"] + ic["G"] * kc["R"]

    total -= m

print(total)

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    s = input()
    d = Counter(s)
    ans = d['R'] * d['G'] * d['B']

    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j-i
            if s[i]!=s[j] and k < n 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()
SL = list(S)
cnt = 0
nr = SL.count('R')
ng = SL.count('G')
nb = SL.count('B')
ans = nr * ng * nb
cnt = 0
for i in range(len(S)):
    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()
ls = list(s)
all = ls.count('R')* ls.count('G')* ls.count('B')

cnt = 0

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

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

print(ans)

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

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

cnt = collections.Counter(S)
ans = cnt['R'] * cnt['G'] * cnt['B']

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

cR = len([1 for s in S if s == 'R'])
cG = len([1 for s in S if s == 'G'])
cB = len([1 for s in S if s == 'B'])

_all = cR * cG * cB


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

print(_all - ng_condition2)

Solution (Python):
N=int(input())
K=input()
dic = {}
ts = 'RGB'
for t in ts:
    p=[]
    for ir, r in enumerate(K):
        if r == t:
            p.append(ir)
    dic[t] = p
ans1 = len(dic['R']) * len(dic['G']) * len(dic['B'])

ans2 = 0

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

print(ans1 - ans2)

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

r = 0
g = 0
b = 0

for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    else:
        b += 1

ans = r*g*b

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

print(ans)

Solution (Python):
n = int(input())
s = input()
s_list = list(s)
ans = 0
total = s_list.count("R") * s_list.count("G") * s_list.count("B")
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = j + j - i
        if n >= k + 1:
            if (s_list[i] != s_list[j]) and (s_list[j] != s_list[k]) and (s_list[k] != s_list[i]):
                ans += 1
print(total - ans)

Solution (Python):
def rest_col(s1,s2):
  if(s1=="R"):
    if(s2=="R"):
      return -1
    elif(s2=="G"):
       return 2
    else:
       return 1
  if(s1=="G"):
    if(s2=="G"):
      return -1
    elif(s2=="R"):
       return 2
    else:
       return 0
  if(s1=="B"):
    if(s2=="B"):
      return -1
    elif(s2=="R"):
       return 1
    else:
       return 0

str_c=["R","G","B"]
n=int(input())
s=input()
count=[[0,0,0] for i in range(n)] 
if(s[n-1]=="R"):
  count[n-1][0]=1
if(s[n-1]=="G"):
  count[n-1][1]=1
if(s[n-1]=="B"):
  count[n-1][2]=1
for i in range(1,n):
  count[n-1-i][0]=count[n-i][0]
  count[n-1-i][1]=count[n-i][1]
  count[n-1-i][2]=count[n-i][2]
  if(s[n-1-i]=="R"):
    count[n-1-i][0]+=1
  if(s[n-1-i]=="G"):
    count[n-1-i][1]+=1
  if(s[n-1-i]=="B"):
    count[n-1-i][2]+=1
ans=0
for a in range(n-1):
  for b in range(a,n-1):
    r=rest_col(s[a],s[b])
    if(r!=-1):
      ans+=count[b+1][r]
      if(2*b-a<len(s)):
        if(s[2*b-a]==str_c[r]):
          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(i+1, n):
        k = 2*j - i
        if n <= k:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1

print(ans)

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

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

S = input()

ans = 0


sumR = [0]
sumG = [0]
sumB = [0]

for s in S:
    if s == "R":
        sumR.append(sumR[-1] + 1)
        sumG.append(sumG[-1])
        sumB.append(sumB[-1])
    elif s == "G":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1] + 1)
        sumB.append(sumB[-1])
    elif s == "B":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1])
        sumB.append(sumB[-1] + 1)


for i, s in enumerate(S):
    not_cnt = 0
    for k in range(1, min(i + 1, N - i)):
        L, R = S[i - k], S[i + k]
        if s != L and s != R and L != R:
            not_cnt += 1

    cnt = 0
    if s == "R":
        cnt += sumG[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumG[-1] - sumG[i + 1])
    elif s == "G":
        cnt += sumR[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumR[-1] - sumR[i + 1])
    elif s == "B":
        cnt += sumG[i] * (sumR[-1] - sumR[i + 1])
        cnt += sumR[i] * (sumG[-1] - sumG[i + 1])

    cnt -= not_cnt
    ans += cnt


print(ans)

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

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

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:
            if {s[i], s[j], s[k]} == {"R", "G", "B"}:
                ans -= 1

print(ans)

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

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




R_pos = []
G_pos = []
B_pos = []
for i in range(N):
    if S[i] == "R":
        R_pos.append(i)
    elif S[i] == "G":
        G_pos.append(i)
    elif S[i] == "B":
        B_pos.append(i)

answer = len(R_pos) * len(G_pos) * len(B_pos)
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[j] != S[k] and S[k] != S[i]:
            answer -= 1

print(answer)

Solution (Python):
N = int(input())
s = input()
R = s.count('R')
G = s.count('G')
B = s.count('B')

count = R*G*B
for d in range((N-1)//2):
    for i in range(N - (d+1)*2):
        if s[i] != s[i+d+1] and s[i] != s[i+2*(d+1)] and s[i+d+1] != s[i+2*(d+1)]:
            count -= 1
print(count)

Solution (Python):
from sys import stdin


def get_result(data):
    
    S = data[1][0]
    r_index = [i for i in range(len(S)) if S[i] == 'R']
    g_index = [i for i in range(len(S)) if S[i] == 'G']
    b_index = set([i for i in range(len(S)) if S[i] == 'B'])
    all_case = len(r_index) * len(g_index) * len(b_index)
    invalid_case = 0
    for i in r_index:
        for j in g_index:
            s_i, s_j = sorted([i, j])
            tmp_max_b = s_j + (s_j-s_i)
            if tmp_max_b in b_index:
                invalid_case += 1
            tmp_min_b = s_i - (s_j-s_i)
            if tmp_min_b in b_index:
                invalid_case += 1
            tmp_mid_b = (s_i + s_j) / 2
            if tmp_mid_b in b_index:
                invalid_case += 1
    return all_case - invalid_case


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

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):
def main():
    n = int(input())
    s = list(input())
    r = 0
    g = 0
    b = 0
    
    for i, rgb in enumerate(s):
        if rgb == 'R':
            r += 1
        elif rgb == 'G':
            g += 1
        else:
            b += 1


    
    ans = 0
    mid = 0
    
    for i, rgb in enumerate(s):
        

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


    print(r * g * b - mid)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

if __name__=='__main__':
    main()

Solution (Python):
n = int(input())
s = list(input())
r = 0
g = 0
b = 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):
        if s[i] == s[j]:
            continue
        k = j * 2 - i
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        ans -= 1

print(ans)

Solution (Python):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
S = S()


def check(i, j, k):
    return j-i != k-j

chars = list(S)
cnts = Counter(chars)

cnt = 0

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

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

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

Solution (Python):


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

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

for i in range(len(S)):
    for d in range(len(S)):
        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()
r = set()
g = set()
b = 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)
a = len(r) * len(b) * len(g)
for i in r:
    for j in b:
        k = abs(i - j)
        if i + k in g:
            a -= 1
        if i - k in g:
            a -= 1
        if j + k in g:
            a -= 1
        if j - k in g:
            a -= 1
        if (i + j) / 2 in g:
            a -= 1
print(a)

Solution (Python):
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(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      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()
all=s.count('R')*s.count('G')*s.count('B')
a=0
for i in range(N):
  for j in range(1,(N-i)//2+1 ):
    if i + 2*j>=N:
      break
    if s[i]!=s[i+j] and s[i]!=s[i+2*j] and s[i+j] !=s[i+2*j]:
      a += 1
print(all-a)

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    r = [0] * n
    g = [0] * n
    b = [0] * n
    
    for i, rgb in enumerate(s):
        if rgb == 'R':
            r[i] += 1
        elif rgb == 'G':
            g[i] += 1
        else:
            b[i] += 1

        if i != 0:
            r[i] += r[i-1]
            g[i] += g[i-1]
            b[i] += b[i-1]
    
    ans = 0
    mid = [0] * n
    
    for i, rgb in enumerate(s):
        

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


    print(r[-1] * g[-1] * b[-1] - sum(mid))
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

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
    elif c == "G":
        g += 1
    else:
        b += 1
ans = r * g * b
sub = 0
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 or S[k] == S[i] or S[k] == S[j]:
            continue
        sub += 1
print(ans - sub)

Solution (Python):
import collections
N = int(input())
S = input()
minus = 0
for i in range(0, N):
    for j in range(1, N):
        if i+j*2 >= N:
            break
        if S[i] != S[i+j]:
            if S[i] != S[i+j*2]:
                if S[i+j] != S[i+j*2]:
                    minus += 1

count_dict = collections.Counter(S)


print(count_dict["R"]*count_dict["G"]*count_dict["B"]-minus)

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]=="B":
        b+=1
    else:
        g+=1
ans=r*b*g

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

print(ans)

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

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

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

print(ans)

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

res = 0

Rn = S.count('R')
Gn = S.count('G')
Bn = S.count('B')

res = Rn * Gn * Bn

for p in range(1, N - 1):
  for q in range(1, min(p, N - p - 1) + 1):
    i, j, k = p - q, p, p + q
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      res -= 1

print(res)