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 = list(input())

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

for i in range(n):
  for j in range(n):
    if not i < j:
      continue
    k = j + (j-i)
    if k < n and "".join(sorted([s[i], s[j], s[k]])) == "BGR":
      ans -= 1

print(ans)

Solution (Python):
import math
N = int(input())
S = str(input())
r=0
g=0
b=0
error=0
n=0
 
if N%2==0:
    n=(N//2)-1
else:
    n=N//2
 
for i in range(N):
    if S[i]=='R':
        r+=1
    if S[i]=='G':
        g+=1
    if S[i]=='B':
        b+=1
 
for p in range(n+1):
    for i in range(N-(p*2)):
        if S[i]==S[i+p] or S[i+p]==S[i+2*p] or S[i]==S[i+2*p]:
            continue
        else:
            error+=1
d=r*g*b
 
print(d-error)

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)
    elif S[i] == 'B':
        B.append(i)
ans = len(R)*len(G)*len(B)
for i in range(1,(N+1)//2):
    for j in range(i,N-i):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = str(input())
r = []
g = []
b = []
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    else:
        b.append(i+1)
cnt=len(r)*len(g)*len(b)
if n<=3:
    print(0)
    exit()
for i in range(n-2):
    for j in range(i+1,n-1):
        if j*2-i<n:
            if s[i]!=s[j] and s[j]!=s[j*2-i] and s[j*2-i]!=s[i]:
                cnt-=1
print(cnt)

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)
    elif S[i] == 'B':
        B.append(i)
ans = len(R)*len(G)*len(B)
for i in range(1,(N+1)//2):
    for j in range(i,N-i):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

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

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 = 2*j-i
        if k < N and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1

print(ans)

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

li = [[] for _ in range(3)]

for i in range(n):
    if s[i] == 'R':
        li[0].append(i)
    elif s[i] == 'G':
        li[1].append(i)
    else:
        li[2].append(i)

ans = 0

for i in li[0]:
    for j in li[1]:
        if i > j:
            sub1 = i - j
            if i + sub1 < n:
                if s[i + sub1] == 'B':
                    ans += 1
            if j - sub1 >= 0:
                if s[j - sub1] == 'B':
                    ans += 1
        else:
            sub1 = j - i
            if j + sub1 < n:
                if s[j + sub1] == 'B':
                    ans += 1
            if  i- sub1 >= 0:
                if s[i - sub1] == 'B':
                    ans += 1
        if (i + j) % 2 == 0 and s[(i+j)//2] == 'B':
            ans += 1

print(len(li[0])*len(li[1])*len(li[2])-ans)

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

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = str(readline().rstrip().decode('utf-8'))
    r = s.count("R")
    g = s.count("G")
    b = s.count("B")
    cnt = r * g * 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[i] != s[k]:
                    cnt -= 1
    print(cnt)


if __name__ == '__main__':
    solve()

Solution (Python):
N=int(input())
S=str(input())
t=0
r=0
b=0
g=0
for i in range(N):
    if S[i]=='R':
        r+=1
    elif S[i]=='B':
        b+=1
    else:
        g+=1
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            continue
        else:
            try:
                if S[2*j-i]!=S[j] and S[2*j-i]!=S[i]:
                    t+=1
                else:
                    continue
            except IndexError:
                break
print (r*b*g-t)

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = str(readline().rstrip().decode('utf-8'))
    r = s.count("R")
    g = s.count("G")
    b = s.count("B")
    cnt = r * g * 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[i] != s[k]:
                    cnt -= 1
    print(cnt)


if __name__ == '__main__':
    solve()

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd




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

r_list = []
g_list = []
b_list = []

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



R_count = len(r_list)
G_count = len(g_list)
B_count = len(b_list)




b_num_list = [0 for i in range(N)]
for i in range(B_count):
    b_num_list[b_list[i]] += 1

    

counter = R_count * G_count * B_count
NG_list = []
for i in range(R_count):
    for j in range(G_count):
        r = r_list[i]
        g = g_list[j]
        hit_count = 0
        
        
        if(g+g-r >= 0 and g+g-r <= N-1): 
            
            if(b_num_list[g+g-r] == 1):
                hit_count += 1
        
        if( (g+r)%2 == 0 and b_num_list[int((g+r)/2)] == 1):
            hit_count+=1
        
        if(r+r-g >= 0 and r+r-g <= N-1 ):
            if(b_num_list[r+r-g] == 1):
                hit_count+=1
        counter -= hit_count
print(counter)

Solution (Python):
def check(pos, c):
  if pos < 0 or pos >= len(S):
    return False
  return S[pos] == c
N = int(input())
S = input()
R_pos = [i for i, c in enumerate(S) if c == 'R']
G_pos = [i for i, c in enumerate(S) if c == 'G']
B_pos = [i for i, c in enumerate(S) if c == 'B']
B_l = len(B_pos)
res = 0
for i in R_pos:
  for j in G_pos:
    res += B_l
    dis = abs(i - j)
    res -= check(min(i, j) - dis, 'B')
    res -= check(max(i, j) + dis, 'B')
    if dis % 2 == 0:
      res -= check(max(i, j) - dis // 2, 'B')
print(res)

Solution (Python):
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = str(readline().rstrip().decode('utf-8'))
    c = collections.Counter(list(s))
    left = collections.defaultdict(int)
    cnt = 0
    for i in range(n - 2):
        left[s[i]] += 1
        mid = collections.defaultdict(int)
        for k, v in left.items():
            mid[k] += v
        for j in range(i + 1, n - 1):
            mid[s[j]] += 1
            if s[i] != s[j]:
                if [s[i], s[j]].count("R") == 0:
                    cnt += c["R"] - mid["R"]
                    if j + j - i < n:
                        if s[j+j-i] == "R":
                            cnt -= 1
                elif [s[i], s[j]].count("G") == 0:
                    cnt += c["G"] - mid["G"]
                    if j + j - i < n:
                        if s[j+j-i] == "G":
                            cnt -= 1
                elif [s[i], s[j]].count("B") == 0:
                    cnt += c["B"] - mid["B"]
                    if j + j - i < n:
                        if s[j+j-i] == "B":
                            cnt -= 1
    print(cnt)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
import collections
import copy


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = str(readline().rstrip().decode('utf-8'))
    c = collections.Counter(list(s))
    left = collections.defaultdict(int)
    cnt = 0
    for i in range(n - 2):
        left[s[i]] += 1
        mid = copy.deepcopy(left)
        for j in range(i + 1, n - 1):
            mid[s[j]] += 1
            if s[i] != s[j]:
                if [s[i], s[j]].count("R") == 0:
                    cnt += c["R"] - mid["R"]
                    if j + j - i < n:
                        if s[j+j-i] == "R":
                            cnt -= 1
                elif [s[i], s[j]].count("G") == 0:
                    cnt += c["G"] - mid["G"]
                    if j + j - i < n:
                        if s[j+j-i] == "G":
                            cnt -= 1
                elif [s[i], s[j]].count("B") == 0:
                    cnt += c["B"] - mid["B"]
                    if j + j - i < n:
                        if s[j+j-i] == "B":
                            cnt -= 1
    print(cnt)


if __name__ == '__main__':
    solve()

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-1):
  for d in range(1, N):
    j = i + d
    k = j + d
    if j >= N or 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=s.count("R")
g=s.count("G")
b=s.count("B")

tmp=r*g*b

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

print(tmp)

Solution (Python):
n = int(input())
s = input()
d = {'R':0,'G':0,'B':0}
for c in s:
    d[c]+=1
all = d['R']*d['G']*d['B']
cnt = 0
for i in range(n):
    for j in range(i+1, n+1):
        k = (j*2)-i
        if k >= n:
            continue
        if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
            cnt += 1
print(all-cnt)

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

def main():
    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
            
    res = R*G*B
    for i in range(n - 2):
        for j in range(i + 1, i + 1 + (n - 1 - i) // 2):
            k = j + (j - i)
            if s[i] != s[j] != s[k] != s[i]:
                res -= 1
    print(res)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())-1
S = str(input())
r,g,b = 0,0,0
rgb = str()
d = 0
for n in range(N+1):
    rgb = str(S[n])
    if rgb == "R":
        r += 1
    if rgb == "G":
        g += 1
    if rgb == "B":
        b += 1
    rang = int((N - n - 2) / 2)
    for j in range(rang,-1,-1):
        if S[n+j+1] != rgb and n+2*j+2 <= N:
           
            if S[n+2*j+2] != rgb:
                if S[n+2*j+2] != S[n+j+1]:
                    d -= 1
                    
                else:
                    pass
            else:
                pass
        else:
            pass
        

Answer = r*g*b + d

print(Answer)

Solution (Python):
n = int(input())
s = input()
x = [0,0,0]
for i in range(n):
    x[ord(s[i])%3] += 1
 
ans = x[0]*x[1]*x[2]

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

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

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Solution (Python):
import itertools

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

r = 0
g = 0
b = 0

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))
for s in S:
  if s == 'R': r += 1
  if s == 'G': g += 1
  if s == 'B': b += 1
out = r * g * b
for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

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

r=[i for i, _x in enumerate(s) if _x == 'R']
g=[i for i, _x in enumerate(s) if _x == 'G']
b=[i for i, _x in enumerate(s) if _x == 'B']

ans=len(r)*len(g)*len(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[i]!=s[k]:
        ans-=1
        
print(ans)

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

r=[i for i, _x in enumerate(s) if _x == 'R']
g=[i for i, _x in enumerate(s) if _x == 'G']
b=[i for i, _x in enumerate(s) if _x == 'B']

ans=len(r)*len(g)*len(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[i]!=s[k]:
        ans-=1
        
print(ans)

Solution (Python):
import sys

if __name__ == '__main__':
    input = sys.stdin.readline
    n = int(input())
    s = list(input())
    s.remove('\n')
    r = 0
    g = 0
    b = 0
    for i in s:
        if i == "R":
            r += 1
        elif i == "G":
            g += 1
        else:
            b += 1
    ans = r * g * b
    for i in range(n):
        for j in range(i, n):
            out = j * 2 - i
            if out >= n:
                continue
            if len({s[i], s[j], s[out]}) == 3:
                ans -= 1
    print(ans)

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

N = int(input())
S = list(input())
from collections import Counter
c = Counter(S)
ans = c['R']*c['B']*c['G']
for i in range(N-1):
    for j in range(i+1,N):
        if 2*j - i >= N:
            continue
        se = [S[i],S[j],S[2*j-i]]
        se = set(se)
        if len(se) == 3:
            ans -=1

print(ans)

Solution (Python):
import sys


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

Solution (Python):
n = int(input())
s = input()
cnt = 0
for d in range((n-1)//2 + 1):
  for i in range(n-2*d):
    if s[i] != s[i+d] and s[i] != s[i+2*d] and s[i+d] != s[i+2*d]:
      cnt += 1
r_cnt = 0
g_cnt = 0
b_cnt = 0
for j in range(n):
  if s[j] == 'R':
    r_cnt += 1
  elif s[j] == 'G':
    g_cnt += 1
  elif s[j] == 'B':
    b_cnt += 1
ans = r_cnt * g_cnt * b_cnt - cnt
print(ans)

Solution (Python):
n = int(input())
s = input()
if n <= 2:
  print(0)
  quit()
r,g,b = 0,0,0
for i in range(n):
  r += 1*(s[i]=="R")
  g += 1*(s[i]=="G")
  b += 1*(s[i]=="B")
ans = r*g*b

for i in range(n-2):
  for j in range(i+1,n):
    l = [i,j,2*j-i]
    if l[2] >= n:
      break
    r,g,b = 0,0,0
    for k in l:
      r += 1*(s[k]=="R")
      g += 1*(s[k]=="G")
      b += 1*(s[k]=="B")
    if r*g*b == 1:
      ans -= 1
print(ans)

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

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

cnt = r * g * b

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

print(cnt)

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

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

for x in range(n):
  for y in range(n):
    if not x < y:
      continue
    z = y + (y-x)
    if z < n and "".join( sorted([s[x], s[y], s[z]]) ) == "BGR":
      ans -= 1
      
print(ans)

Solution (Python):
n = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
 
ans = r_cnt * g_cnt * b_cnt
 
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            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*b*g

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    
    n = int(input())
    S = input()
    res = 0
    for center in range(1, n - 1):
        letters = ['R', 'G', 'B']
        letters.remove(S[center])
        a = S[:center].count(letters[0])
        b = S[center+1:].count(letters[1])
        res += a * b
        a = S[:center].count(letters[1])
        b = S[center+1:].count(letters[0])
        res += a * b

    
    for d in range(1, n):
        for center in range(n):
            left = center - d
            right = center + d
            if 0 <= left and right < n:
                T = {S[left], S[center], S[right]}
                res -= len(T) == 3

    print(res)
resolve()

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

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

cnt = 0

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

Solution (Python):
n=int(input())
l=input()
a=l.count('R')
b=l.count('G')
c=l.count('B')
d=a*b*c
i=0
count=0
lis=list(l)
while i<=n-3:
    j=i+1
    while i+2*(j-i)<=n-1:
        if lis[i]!=lis[j] and lis[j]!=lis[i+2*(j-i)] and lis[i]!=lis[i+2*(j-i)]:
            count+=1
            j+=1
        else:
            j+=1
    i+=1
print(d-count)

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

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

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

print(ans)

Solution (Python):

n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
cnt= 0
for i in range(n):
    for j in range(i,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(r*g*b-cnt)

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(i + 1, n):
        k = 2 * j - i
        if 1 <= k < n:
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                res -= 1
print(res)

Solution (Python):


n = int(input())
s = input()
x = [0] * n
num_r, num_g, num_b = 0, 0, 0
for i in range(n):
    if s[i] == 'R':
        x[i] = 1
        num_r += 1
    elif s[i] == 'G':
        x[i] = 2
        num_g += 1
    elif s[i] == 'B':
        x[i] = 4
        num_b += 1

num = num_r * num_g * num_b

for i in range(n):
    for j in range(1, n):
        if i - j < 0 or n <= i + j:
            break
        else:
            if (x[i - j] + x[i] + x[i + j]) == 7:
                num -= 1
print(num)

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

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

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

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

n_R = S.count('R')
n_G = S.count('G')
n_B = N - n_R - n_G
ans = n_R * n_G * n_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
from math import ceil

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

c = Counter(s)
res = c['R'] * c['G'] * c['B']
for i in range(n-2):
    for j in range(i+1, i+ceil((n-i)/2)):
        k = j + j - i
        if len({s[i], s[j], s[k]}) == 3:
            res -= 1

print(res)

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

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


ng=0
for i in range(N):
    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]:
            ng+=1

ans=r*g*b
print(ans-ng)

Solution (Python):
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_count, g_count, b_count = [S.count(x) for x in 'RGB']
    ans = r_count * g_count * b_count
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + (j - i)
            if k < N and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

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

for i in range(n):
    if s[i]=='R':
        r+=1
    if s[i]=='G':
        g+=1
    if s[i]=='B':
        b+=1

ans=r*g*b

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

Solution (Python):
N = int(input())
N -= 1
S = str(input())
count = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
    for j in range(i,N+1):
        k = 2*j - i
        if k > N:
            pass
        elif S[i] == S[j] or S[i] == S[k] or S[j] == S[k]:
            pass
        else:
            count -= 1
print(count)

Solution (Python):
def main():
    n = int(input())
    l = list(input())
    R = [0]
    G = [0]
    B = [0]
    Rl = set()
    Gl = set()
    Bl = set()

    for i in range(n):
        if l[i] == "R":
            R.append(R[-1]+1)
            G.append(G[-1])
            B.append(B[-1])
            Rl.add(i+1)
        if l[i] == "G":
            R.append(R[-1])
            G.append(G[-1]+1)
            B.append(B[-1])
            Gl.add(i+1)
        if l[i] == "B":
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1]+1)
            Bl.add(i+1)
    ans = 0


    for i in range(1,n-1):
        if l[i] == "R":
            ans += G[i]*(B[-1]-B[i+1])
            ans += B[i]*(G[-1]-G[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Gl and i-j+1 in Bl:
                        ans -= 1
                    if j+i+1 in Bl and i-j+1 in Gl:
                        ans -= 1
        if l[i] == "G":
            ans += R[i]*(B[-1]-B[i+1])
            ans += B[i]*(R[-1]-R[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Rl and i-j+1 in Bl:
                        ans -= 1
                    if j+i+1 in Bl and i-j+1 in Rl:
                        ans -= 1
        if l[i] == "B":
            ans += G[i]*(R[-1]-R[i+1])
            ans += R[i]*(G[-1]-G[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Gl and i-j+1 in Rl:
                        ans -= 1
                    if j+i+1 in Rl and i-j+1 in Gl:
                        ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    l = list(input())
    R = [0]
    G = [0]
    B = [0]
    Rl = set()
    Gl = set()
    Bl = set()

    for i in range(n):
        if l[i] == "R":
            R.append(R[-1]+1)
            G.append(G[-1])
            B.append(B[-1])
            Rl.add(i+1)
        if l[i] == "G":
            R.append(R[-1])
            G.append(G[-1]+1)
            B.append(B[-1])
            Gl.add(i+1)
        if l[i] == "B":
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1]+1)
            Bl.add(i+1)
    ans = 0


    for i in range(1,n-1):
        if l[i] == "R":
            ans += G[i]*(B[-1]-B[i+1])
            ans += B[i]*(G[-1]-G[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Gl and i-j+1 in Bl:
                        ans -= 1
                    if j+i+1 in Bl and i-j+1 in Gl:
                        ans -= 1
        if l[i] == "G":
            ans += R[i]*(B[-1]-B[i+1])
            ans += B[i]*(R[-1]-R[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Rl and i-j+1 in Bl:
                        ans -= 1
                    if j+i+1 in Bl and i-j+1 in Rl:
                        ans -= 1
        if l[i] == "B":
            ans += G[i]*(R[-1]-R[i+1])
            ans += R[i]*(G[-1]-G[i+1])
            for j in range(i+1):
                if j+i+1 > n or i-j+1 < 0:
                    break
                else:
                    if j+i+1 in Gl and i-j+1 in Rl:
                        ans -= 1
                    if j+i+1 in Rl and i-j+1 in Gl:
                        ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

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

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

ans=r*g*b
for i in range (n):
  for 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()
a=S.count('R')
b=S.count('G')
c=S.count('B')
ans=a*b*c
for i in range(N-2):
  for j in range(i+1,N-1):
    if S[i]!=S[j]:
      if (2*j-i)<=N-1 and S[i]!=S[2*j-i]!=S[j]:
        ans-=1     
print(ans)

Solution (Python):
from collections import*
n=int(input())
s=input()
c=0
r=s.count('R')
g=s.count('G')
b=s.count('B')
rgb=r*g*b

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

print(rgb)

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

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

        if k >= n:
            break
        elif 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()
s_l = list(s)

a = s_l.count('R') * s_l.count('G') * s_l.count('B')
cnt = 0

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

Solution (Python):
from itertools import*
n=int(input())
s=input()

*x,=accumulate(i=="R" for i in s)
*y,=accumulate(i=="G" for i in s)
*z,=accumulate(i=="B" for i in s)
x2=list(accumulate(i=="R" for i in s[::-1]))[::-1]
y2=list(accumulate(i=="G" for i in s[::-1]))[::-1]
z2=list(accumulate(i=="B" for i in s[::-1]))[::-1]

ans=0
if n<4:
    print(0)
else:
    for i in range(1,n-1):
        p=s[i]
        for j in range(1,min(i,n-i-1)+1):
            q,r=s[i-j],s[i+j]
            ans-=(p!=q and q!=r and r!=p)
                
    
    for i in range(1,n-1):
        if s[i]=="R":
            ans+=y[i-1]*z2[i+1]
            ans+=z[i-1]*y2[i+1]
        elif s[i]=="G":
            ans+=x[i-1]*z2[i+1]
            ans+=z[i-1]*x2[i+1]
        elif s[i]=="B":
            ans+=x[i-1]*y2[i+1]
            ans+=y[i-1]*x2[i+1]
    print(ans)

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

Solution (Python):
N=int(input())
S=list(input())
 
def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'
 
r=[]
g=[]
b=[]
for i in range(N):
    r.append(S[i:].count('R'))
    g.append(S[i:].count('G'))
    b.append(S[i:].count('B'))
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

Solution (Python):
NumString = int(input())
String = input()

r_string_num, g_string_num, b_string_num = 0, 0, 0
for i_string in String:
    if i_string == 'R':
        r_string_num += 1
    elif i_string == 'G':
        g_string_num += 1
    else:
        b_string_num += 1
    
total_ans = r_string_num * g_string_num * b_string_num

exclude_num = 0
for i in range(NumString-2):
    for j in range(i+1, NumString-1):
        if (2*j - i) < NumString and String[i] != String[j] and \
            String[j] != String[2*j-i] and String[i] != String[2*j-i]:
            exclude_num += 1

total_ans -= exclude_num

print(total_ans)