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()
assert len(S) == N
count = 0


for i in range(0, len(S)-2):
  for j in range(i+1, len(S)-1):
    k = j + (j - i)
    if k < len(S):
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        count += 1
numR = 0
numG = 0
numB = 0
for s in S:
  if s == 'R':
    numR += 1
  elif s == 'G':
    numG += 1
  else:
    numB += 1

print(numR * numG * numB - count)

Solution (Python):
from collections import Counter

def get_k(i, j):
    return 2*j - i

if __name__ == '__main__':
    N = int(input())
    S = input()
    C = Counter(S) 
    eq_cnt = 0
    cnt = C['R'] * C['G'] * C['B']
    for i in range(N):
        for j in range(i+1, N):
            k = get_k(i, j)
            if k >= N: continue
            if (S[i] != S[j] and S[i] != S[k] and S[j] != S[k]):
                eq_cnt += 1
    print(cnt - eq_cnt)

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

r = [0] * (n+1)
g = [0] * (n+1)
b = [0] * (n+1)
for i, c in enumerate(reversed(s), 1):
    if c == 'R':
        r[i] += r[i-1] + 1
    else:
        r[i] += r[i-1]
    
    if c == 'G':
        g[i] += g[i-1] + 1
    else:
        g[i] += g[i-1]
    
    if c == 'B':
        b[i] += b[i-1] + 1
    else:
        b[i] += b[i-1]

r = r[:-1][::-1]
g = g[:-1][::-1]
b = b[:-1][::-1]

ans=0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        se = set('RGB') - set(s[i] + s[j])
        c = se.pop()
        if c == 'R':
            ans += r[j]
            if 2*j-i < n and s[2*j-i] == 'R':
                ans -= 1
        elif c == 'G':
            
            ans += g[j]
            if 2*j-i < n and s[2*j-i] == 'G':
                ans -= 1
        elif c == 'B':
            ans += b[j]
            if 2*j-i < n and s[2*j-i] == 'B':
                ans -= 1
        
print(ans)

Solution (Python):

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

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

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

ans = all - cnt


print(ans)

Solution (Python):
N=int(input())
T=input()
S=[0 for _ in range(N)]
for i in range(N):
  if T[i]=='G':
    S[i]=1
  elif T[i]=='B':
    S[i]=2
    
ls=[(0,0,0) for _ in range(N)]
for i in range(N):
  if S[i]==0:
    ls[i]=(ls[i-1][0]+1,ls[i-1][1],ls[i-1][2])
  elif S[i]==1:
    ls[i]=(ls[i-1][0],ls[i-1][1]+1,ls[i-1][2])
  else:
    ls[i]=(ls[i-1][0],ls[i-1][1],ls[i-1][2]+1)


def count(x):
  val=0
  y=(x+1)%3
  z=(x+2)%3
  for i in range(N):
    if S[i]==x:
      for j in range(i+1,N):
        if S[j]==y:
          val+=ls[N-1][z]-ls[j][z]
          if 2*j-i<N and S[2*j-i]==z:
            val-=1
        if S[j]==z:
          val+=ls[N-1][y]-ls[j][y]
          if 2*j-i<N and S[2*j-i]==y:
            val-=1
  return val

print(count(0)+count(1)+count(2))

Solution (Python):
N = int(input())
S = input()
C = {"R", "G", "B"}
cumsum = {c:[0 for i in range(N)] for c in C}

for i in range(N):
  s = S[i]
  cumsum[s][i] = 1
for i in range(2, N+1):
  for c in C:
    cumsum[c][-i] = cumsum[c][-i] + cumsum[c][-(i-1)]

ans = 0
for i in range(N-2):
  s = S[i]
  a, b = C - {s}
  ans += cumsum[a][i]*cumsum[b][i]

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def inp():
    return int(input())
def inps():
    return input().rstrip()
def inpl():
    return list(map(int, input().split()))
def _debug(obj):
    print(obj, file=sys.stderr)














N = inp()
S = inps()

r = []
g = []
b = []

for i in range(N):
    if S[i] == "R":
        r.append(i)
    if S[i] == "G":
        g.append(i)
    if S[i] == "B":
        b.append(i)

ans = 0
for i in r:
    for j in g:
        m = i
        n = j
        if m > n:
            m, n = n, m
        sa = abs(m - n)
        ans += len(b)
        if m - sa >= 0 and S[m - sa] == "B":
            ans -= 1
        if n + sa < N and S[n + sa] == "B":
            ans -= 1
        if sa % 2 == 0 and S[m + sa//2] == "B":
            ans -= 1
print(ans)

Solution (Python):
n=int(input())
S=str(input())
r=S.count('R')
g=S.count('G')
b=S.count('B')
cnt=r*g*b

for j in range(2, n, 2):
    for i in range(0, n-j):
        k = (2 * i + j) // 2
        if S[i] != S[i+j] and S[i] != S[k] and S[k] != S[i+j]:
            cnt -= 1

print(cnt)

Solution (Python):
def check_RGB(c1, c2, c3):
    l = [c1, c2, c3]
    return ("R" in l) and ("G" in l) and ("B" in l)
    
N = int(input())
S = input()

R, G, B = 0, 0, 0
for i, s in enumerate(S):
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    else:
        B += 1

result = R * G * B

width = 1
while width * 2 < N:
    i = 0
    first = i
    mid = i + width
    last = i + width*2

    while last < N:
        if check_RGB(S[first], S[mid], S[last]):
            result -= 1

        i += 1
        first = i
        mid = i + width
        last = i + width*2

    width += 1

print(result)

Solution (Python):
n = int(input())
s = input()
r,g,b = 0,0,0
for i in s:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    else:
        b += 1
ans_1 = r * g * b
ans_2 = 0
for i in range(n):
    for j in range(i+1,n):
        if s[i] == s[j]:
            continue
        k = j - i + j
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        
        ans_2 += 1

print(ans_1-ans_2)

Solution (Python):



import sys

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):
        try:
            k = 2 * j - i
            if (S[i] != S[j]) & (S[j] != S[k]) & (S[k] != S[i]):
                ans -= 1
        except:
            continue

print(ans)

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

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

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

print(r*g*b-cnt)

Solution (Python):
N=int(input())
S=input()
cntR,cntG,cntB=S.count('R'),S.count('G'),S.count('B')

ans=cntR*cntG*cntB

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

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, 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, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
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 TUPLE(): return tuple(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()

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

cnt = 0
for l, r in combinations(range(N), 2):
	if l%2 != r%2:
		continue
	m = (l+r)//2
	if len(set([S[l], S[m], S[r]])) == 3:
		cnt += 1

print(R*G*B - cnt)

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

Solution (Python):
from collections import *

N=int(input())
S=input()
c=Counter(S)

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

for i in range(N):
  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 collections import *

N=int(input())
S=input()
c=Counter(S)

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

for i in range(N):
  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 collections import *

N=int(input())
S=input()
c=Counter(S)

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

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

print(ans)

Solution (Python):
import bisect

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    for i, s in enumerate(S):
        if s == 'R':
            R.append(i)
        elif s == 'G':
            G.append(i)
        else:
            B.append(i)

    global ans
    ans = 0
    count(R, G, B)
    count(R, B, G)
    count(G, R, B)
    count(G, B, R)
    count(B, R, G)
    count(B, G, R)
    print(ans)


def count(I, J, K):
    global ans
    for j in J:
        i_list = I[:bisect.bisect_left(I, j)]
        k_list = set(K[bisect.bisect_right(K, j):])
        remove = len([i for i in i_list if (2 * j - i) in k_list])
        ans += len(i_list) * len(k_list) - remove


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()
    dict = {'R':0, 'B':0, 'G':0}
    for i in range(N):
        dict[S[i]] += 1
    ans = dict['R']*dict['B']*dict['G']
    for i in range(N-2):
        if (N-i)%2 == 0:
            tmp = int((N-i)/2)-1
        else:
            tmp = (N-i)//2
        for j in range(1,tmp+1):
            if S[i]!=S[i+j] and S[i]!=S[i+2*j] and S[i+j]!=S[i+2*j]:
                ans = ans - 1

    return ans

print(main())

Solution (Python):
from collections import Counter

n=int(input())
s=input()
c=Counter(s)

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

lim=n//2
for i in range(1,n//2+1):
  for j in range(i,n-i):
    l=s[j-i]
    m=s[j]
    r=s[j+i]
    if l!=m and m!=r and r!=l:
      ans-=1

print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from collections import Counter

n=int(input())
s=input()
c=Counter(s)

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

lim=n//2
for i in range(1,n//2+1):
  for j in range(i,n-i):
    l=s[j-i]
    m=s[j]
    r=s[j+i]
    if l!=m and m!=r and r!=l:
      ans-=1

print(ans)

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

from collections import Counter

c=Counter(s)





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

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

print((r*g*b)-cnt)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
S = [''] + LS2()

R = [0]*(N+1)
G = [0]*(N+1)
B = [0]*(N+1)
for i in range(1,N+1):
    s = S[i]
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if s == 'R':
        R[i] += 1
    elif s == 'G':
        G[i] += 1
    else:
        B[i] += 1

ans = 0
for i in range(2,N):
    if S[i] == 'R':
        ans += G[i-1]*(B[-1]-B[i])
        ans += B[i-1]*(G[-1]-G[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'G' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'G'):
                    ans -= 1

    elif S[i] == 'G':
        ans += R[i-1]*(B[-1]-B[i])
        ans += B[i-1]*(R[-1]-R[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
                    ans -= 1
    else:
        ans += G[i-1]*(R[-1]-R[i])
        ans += R[i-1]*(G[-1]-G[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                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):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
S = list(input())
from collections import Counter

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

for i in range(1, N):
    for j in range(N):
        first, second, third = j, j+i, j+i*2
        if third>=N:
            break
        if len(set((S[first], S[second], S[third])))==3:
            ans -= 1

print(ans)

Solution (Python):
import sys
from collections import Counter
N = int(input())
S = list(input())
Sc = Counter(S)
R = Sc["R"]
G = Sc["G"]
B = Sc["B"]
if R == 0 or G == 0 or B == 0:
    print(0)
    sys.exit()
else:
    ans = R*G*B
for i in range(1, N//2+2):
    for j in range(N):
        if j+i+i >= N:
            break
        if S[j] == S[j+i]:
            continue
        if S[j] == S[j+i+i]:
            continue
        if S[j+i] == S[j+i+i]:
            continue
        ans -= 1
print(ans)

Solution (Python):
import sys
from collections import Counter

input = sys.stdin.readline
N = int(input())
S = input().strip()

counter = Counter(S)
if len(counter) < 3:
    print(0)
    sys.exit()

exclude = 0
max_w = N // 2
for w in range(1, max_w+1):
    for i in range(N):
        if N <= i + 2*w:
            continue
        if S[i] != S[i+w] and S[i] != S[i+2*w] and S[i+w] != S[i+2*w]:
            exclude += 1


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

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
S = [''] + LS2()

R = [0]*(N+1)
G = [0]*(N+1)
B = [0]*(N+1)
for i in range(1,N+1):
    s = S[i]
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if s == 'R':
        R[i] += 1
    elif s == 'G':
        G[i] += 1
    else:
        B[i] += 1

ans = 0
for i in range(2,N):
    if S[i] == 'R':
        ans += G[i-1]*(B[-1]-B[i])
        ans += B[i-1]*(G[-1]-G[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'G' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'G'):
                    ans -= 1

    elif S[i] == 'G':
        ans += R[i-1]*(B[-1]-B[i])
        ans += B[i-1]*(R[-1]-R[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
                    ans -= 1
    else:
        ans += G[i-1]*(R[-1]-R[i])
        ans += R[i-1]*(G[-1]-G[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                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):
n = int(input())
s = input()

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

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

print(ans)

Solution (Python):
N = int(input())
S = input().strip()
C = {"R":[],"G":[],"B":[]}
for i in range(N):
    C[S[i]].append(i)
C["B"] = set(C["B"])
cnt = 0
for r in C["R"]:
    for g in C["G"]:
        if r<g:
            i = r
            j = g
        else:
            i = g
            j = r
        cnt += len(C["B"])
        if i-(j-i) in C["B"]:
            cnt -= 1
        if j+(j-i) in C["B"]:
            cnt -= 1
        if (j+i)%2==0 and (j+i)//2 in C["B"]:
            cnt -= 1
print(cnt)

Solution (Python):
N=int(input())
S=input()
cnt=0
for i in range(N):
    left=i-1
    right=i+1
    while 0<=left and right<N:
        if S[i]!=S[left] and S[i]!=S[right] and S[left]!=S[right]:
            cnt+=1
        left-=1
        right+=1
x=S.count('R')
y=S.count('G')
z=S.count('B')
print(x*y*z-cnt)

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

i = 1
count = 0
while i * 2 < N:
    n = 0
    while n + i * 2 < N:
        if (S[n] != S[n+i]) and (S[n+i] != S[n+2*i]) and (S[n] != S[n+i*2]):
            count += 1
            
        n += 1
    i += 1

print(S.count('G') * S.count('R') * S.count('B') - count)

Solution (Python):
def main():
    n = int(input())
    s = input()
    if n < 4:
        print(0)
        return
    ans = s.count("R") * s.count("G") * s.count("B")

    for i, si in enumerate(s[:-2]):
        for j, sj in enumerate(s[i+1:-1]):
            if si == sj:
                continue

            if i + 2*j+2 < n and si != s[i + 2*j+2] != sj:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

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

R_list = [0]*N
G_list = [0]*N
B_list = [0]*N

for key,val in enumerate(S):
    if val=='R':
        R_list[key] = R_list[key-1]+1
        G_list[key] = G_list[key-1]
        B_list[key] = B_list[key-1]
    elif val=='G':
        R_list[key] = R_list[key-1]
        G_list[key] = G_list[key-1]+1
        B_list[key] = B_list[key-1]
    elif val=='B':
        R_list[key] = R_list[key-1]
        G_list[key] = G_list[key-1]
        B_list[key] = B_list[key-1]+1

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        a=2*(j+1)-(i+1)-1
        check = [S[i],S[j]]
        if 'R' not in check:
            ans += R_list[-1]-R_list[j]
            if a<N and S[a]=='R':
                ans -= 1
        elif 'G' not in check:
            ans += G_list[-1]-G_list[j]
            if a<N and S[a]=='G':
                ans -= 1
        elif 'B' not in check:
            ans += B_list[-1]-B_list[j]
            if a<N and S[a]=='B':
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r, g, b = s.count("R"), s.count("G"), s.count("B")
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        try:
            k = j + (j - i)
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt += 1
        except:
            pass
print(r * g * b - cnt)

Solution (Python):
import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
N=INT()
S=input()
if N<3:
  print(0)
  exit()
r=S.count("R")
g=S.count("G")
b=S.count("B")
ans=r*g*b
for i in range(N-2):
  for j in range(i+1,N-1):
    k=j*2-i
    if k>=N:
      continue
    if S[j]!=S[i] and S[k]!=S[i] and S[j]!=S[k]:
      ans-=1
print(ans)

Solution (Python):
n=int(input())
S=str(input())
r=S.count('R')
g=S.count('G')
b=S.count('B')
cnt=r*g*b
for i in range(n):
    for a in range(1,n):
        j=i+a
        k=j+a
        if k>=n:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            cnt-=1

print(cnt)

Solution (Python):

def main():
    '''
    
    n = int(input())
    s = input()
    cnt = 0
    for i in range(len(s)):
        x = s[i]
        for j in range(i+1, len(s)):
            y = s[j]
            if x == y:
                continue
            for k in range(j+1, len(s)):
                z = s[k]
                if z == x or z == y:
                    continue
                if j - i == k - j:
                    continue
                cnt += 1
                
    print(cnt)
    '''
    n = int(input())
    s = input()
    cnt = [0]*3
    ans = 1
    for i in s:
        if i == 'R':cnt[0]+=1
        elif i == 'G':cnt[1]+=1
        else:cnt[2]+=1
    for i in cnt:
        ans *= i
    for i in range(n):
        for j in range(i+1,n):
            k = j + (j - i)
            if k < n:
                if s[i] == s[j]: continue
                if s[j] == s[k]: continue
                if s[k] == s[i]: continue
                ans -= 1
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import itertools
from functools import reduce
from functools import lru_cache



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

def main():

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

    R=[0]*N
    G=[0]*N
    B=[0]*N
    r=0
    g  =0
    b=0
    count =0
    for i in range(N-1,-1,-1):
        if S[i] =="R":
            r+=1
        elif S[i] =="G":
            g+=1
        else:
            b+=1
        R[i]=r
        G[i]=g
        B[i]=b

    count =0
    for i in range(N):



        for j in range(i+1,N):
            SE = set(["R", "G", "B"])
            I = S[i]
            SE.remove(I)
            J =S[j]
            if I==J:
                continue
            SE.remove(J)
            if "G" in SE:
                count+=G[j]
                if j+j-i <N and S[j+j-i] =="G":
                    count -=1

            elif "R" in SE:
                count+=R[j]
                if j+j-i <N and S[j+j-i] =="R":
                    count -=1
            else:
                count+=B[j]
                if j+j-i <N and S[j+j-i] =="B":
                    count -=1



    print(count)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s,rem=input(),0
ans = s.count('R') * s.count('B') * s.count('G')

for i in range(1, (n-1) // 2 + 1):
  for j in range(i*2, n):
    if s[j]+s[j-i]+s[j-2*i] in ("RGB","RBG","GRB","GBR","BGR","BRG"):
      rem += 1
    
print(ans - rem)

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

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

res = r*g*b

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

print(res)

Solution (Python):
def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))
def inNL(n):
    return [list(map(int,input().split())) for i in range(n)]


n = inN()
s = input()


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

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]:
                cnt += 1
print(r*g*b - cnt)

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

Solution (Python):
import sys
import math
import bisect
from collections import defaultdict,deque


def inar():
    return [int(el) for el in input().split()]



def main():
    n=int(input())
    string=input()
    r=[]
    g=[]
    b=[]
    for i in range(n):
        if string[i]=="R":
            r.append(i)
        elif string[i]=="G":
            g.append(i)
        else:
            b.append(i)
    ans=0
    r.sort()
    g.sort()
    b.sort()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    for i in range(len(r)):
        for j in range(len(g)):
            ans+=len(b)
            chota=min(g[j],r[i])
            bada=max(g[j],r[i])
            diff=bada-chota

            left=bisect.bisect_left(b,bada+diff)
            right=bisect.bisect_left(b,chota-diff)

            lol=(bada+chota)
            if lol%2==0:
                beech=lol//2
                ind=bisect.bisect_left(b,beech)
                if ind<len(b) and b[ind]==beech:
                    ans-=1
            if (left<len(b) and b[left]==bada+diff):
                ans-=1
            if (right<len(b) and b[right]==chota-diff):
                ans-=1

            
    print(ans)
    
    


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = list(input())
R = []
G = []
B = [0 for _ in range(N)]
b_cnt = 0
for i in range(N):
  if S[i] == 'R':
    R.append(i)
  elif S[i] == 'G':
    G.append(i)
  else:
    B[i] = 1
    b_cnt += 1
answer = 0
for r in R:
  for g in G:
    answer += b_cnt
    if (g-r)%2 == 0 and B[(r+g)//2] == 1:
      answer -= 1
    if 0 <= 2*g-r < N and B[2*g-r] == 1:
      answer -= 1
    if 0 <= 2*r-g < N and B[2*r-g] == 1:
      answer -= 1
print(answer)

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

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

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

res=0
R=[]
G=[]
B=[]
r,g,b=0,0,0
for i in range(N):
    if S[i]=='R':
        r+=1
    elif S[i]=='G':
        g+=1
    else:
        b+=1
    R.append(r)
    G.append(g)
    B.append(b)
    
for i in range(N):
    for j in range(N):
        if not i<j:
            continue
        k=S[2*j-i] if 2*j-i<N else 'A'
        if set([S[i],S[j]])==set(['R','G']):
            res+=B[-1]-B[j]-(k=='B')
        elif set([S[i],S[j]])==set(['B','G']):
            res+=R[-1]-R[j]-(k=='R')
        elif set([S[i],S[j]])==set(['R','B']):
            res+=G[-1]-G[j]-(k=='G')
print(res)

Solution (Python):
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

    cnt = 0
    for j in range(1, n):
        for i in range(j):
            k = 2 * j - i
            if k >= n:
                continue
            if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
                continue
            cnt += 1

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

    cnt = 0
    for i in range(n):
        for j in range(i + 1, n):
            k = 2 * j - i
            if k >= n:
                continue
            if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
                continue
            cnt += 1

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

    cnt = 0
    for i in range(n):
        for j in range(i + 1, n):
            k = 2 * j - i

            if i in r_set and j in g_set and k in b_set:
                cnt += 1
            elif i in r_set and j in b_set and k in g_set:
                cnt += 1
            elif i in g_set and j in r_set and k in b_set:
                cnt += 1
            elif i in g_set and j in b_set and k in r_set:
                cnt += 1
            elif i in b_set and j in r_set and k in g_set:
                cnt += 1
            elif i in b_set and j in g_set and k in r_set:
                cnt += 1

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
cnt=0
for i in range(n-1):
    for j in range(1, min(i+1, n-i)):
            if (s[i] != s[i-j]) and (s[i] != s[i+j]) and (s[i-j] != s[i+j]):
                cnt += 1
print(r*g*b-cnt)

Solution (Python):
n = int(input())
s = input()
total = 1
for c in 'RGB':
  total *= s.count(c)
for i in range(1, n - 1):
  for j in range(1, min(i + 1, n - i)):
    if s[i] != s[i - j] and s[i - j] != s[i + j] and s[i] != s[i + j]:
      total -= 1
print(total)