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()
R = 0
G = 0
B = 0
notToAllow = 0


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

total = R*G*B

for i in range(0,N):
    for j in range(0,(N//2)+1):
        if(j+j+i < N):
            if ((S[i] != S[i+j])):
                if(S[i] != S[i+j+j]):
                    if(S[i+j] != S[i+j+j]):
                        notToAllow = notToAllow + 1

print(R*G*B - notToAllow)

Solution (Python):
N = int(input())
S = input()
R = 0
G = 0
B = 0
notToAllow = 0


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

total = R*G*B

for i in range(0,N):
    for j in range(0,(N//2)+1):
        if(j+j+i < N):
            if ((S[i] != S[i+j])):
                if(S[i] != S[i+j+j]):
                    if(S[i+j] != S[i+j+j]):
                        notToAllow = notToAllow + 1

print(R*G*B - notToAllow)

Solution (Python):
d = input()
s = input()

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

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

Solution (Python):
import bisect

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

	tb_pos = {'BG': None, 'BR': None, 'GR': None}
	for k in tb_pos:
		tb_pos[k] = [0] * (N + 1)
	
	rs = 0
	for i in range(N):
		for j in range(i + 1, N):
			if S[i] == S[j]:
				continue
			d = j - i
			c1, c2 = S[i], S[j]
			if c1 > c2:
				c1, c2 = c2, c1
			word = c1 + c2
			if j + d < N and S[j + d] not in word:
				rs -= 1
			tb_pos[word][j + 1] += 1 

	for i in range(1, N + 1):
		for k in tb_pos:
			tb_pos[k][i] += tb_pos[k][i - 1]
				
	for j in range(2, N):
		word = None
		if S[j] == 'B':
			word = 'GR'
		elif S[j] == 'G':
			word = 'BR'
		else:
			word = 'BG'
		
		rs += tb_pos[word][j + 1]

	print(rs)
	return 0 

main()

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

to_int = {"R":0, "G":1, "B":2}
cnt = [0] * 3
for char in s:
    cnt[to_int[char]] += 1

ans = 1
for num in cnt:
    ans *= num

for i in range(n):
    for length in range(1, n):
        if i - length < 0 or i + length >= n:
            break
        if len(set([to_int[s[i - j * length]] for j in (-1, 0, 1)])) == 3:
            ans -= 1

print(ans)

Solution (Python):
from itertools import permutations

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
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

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

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            continue
        for x, y, z in permutations(lst):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

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

for c in s:
    if c == "R":
        r += 1
    elif c == "G":
        g += 1
    elif c == "B":
        b += 1

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

print(r*g*b-sub)

Solution (Python):
from collections import Counter

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

c=Counter(s)
ans1=c['B']*c['R']*c['G']

lim= n//2 if n%2==1 else n//2-1

for i in range(1,lim+1):
  for j in range(i,n-i):
    right=s[j-i]
    middle=s[j]
    left=s[j+i]
    if right!=middle and middle!=left and left!=right:
      ans1-=1
print(ans1)

Solution (Python):
n = int(input())
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 = j + j - i
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            cnt += 1

print(r * b * g - cnt)

Solution (Python):


N = int(input())
S = input()
S = S[::-1]

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

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

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

a = r * g * b

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

print(a)

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+(j-i)
    if N-1<k:
      break
 
    if S[j]!=S[i] and S[j]!=S[k] and S[k]!=S[i]:
        ans-=1
        
print(ans)

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

    R_count = 0
    G_count = 0
    B_count = 0

    for s in S:
        if s == "R":
            R_count += 1
        elif s == "G":
            G_count += 1
        else:
            B_count += 1

    
    answer = R_count * G_count * B_count

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

            
            if k >= N:
                continue

            
            
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                answer -= 1
            
    print(answer)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools

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

r = len(list(filter(lambda x: x == "R", S)))
g = len(list(filter(lambda x: x == "G", S)))
b = len(list(filter(lambda x: x == "B", S)))



ans = r * g * b
RGB = 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+2*i]) in RGB:
            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=j+(j-i)
    if N-1<k:
      break

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

Solution (Python):
N = int(input())
S = list(map(str, 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-1:
            break
        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 = 0
g = 0 
for i in s:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  
c = r*g*(n-r-g)
for j in range(1, n-1):
  for i in range(1, min(j, n - j -1) + 1):
    if (s[j] != s[j + i] ) and (s[j] != s[j - i]) and (s[j - i] != s[j + i]):
      c -= 1
print(c)

Solution (Python):
from itertools import product

x1 = int(input())
x2 = input()

R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = {i for i, x in enumerate(x2) if x == "B"}

R = len(R_i)
G = len(G_i)
B = len(B_i)

xs = map(sorted, product(R_i, G_i))

s = 0
for a in xs:
    k = a[1]*2 - a[0]  
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2  
    if j in B_i:
        s +=1
print(R*G*B - s)

Solution (Python):
from itertools import product

x1 = int(input())
x2 = input()

R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = {i for i, x in enumerate(x2) if x == "B"}

R = len(R_i)
G = len(G_i)
B = len(B_i)

xs = map(sorted, product(R_i, G_i))

s = 0
for a in xs:
    k = a[1]*2 - a[0]  
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2
    if j.is_integer():
        j = int(j)
        if j in B_i:
            s +=1
print(R*G*B - s)

Solution (Python):
import sys
A=[a.rstrip() for a in sys.stdin.readlines()]
N=int(A[0])
S="".join(["A",A[1]])

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

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

Solution (Python):
from collections import defaultdict

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

rgb = {'R': 0, 'G': 0, 'B': 0}
for i in s:
    rgb[i] += 1

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

print(rgb['R']*rgb['G']*rgb['B'] - c)

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

num_R = len([c for c in S if c == "R"])
num_G = len([c for c in S if c == "G"])
num_B = len([c for c in S if c == "B"])

total = num_R * num_G * num_B

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

print(total - count)

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

num_R = len([c for c in S if c == "R"])
num_G = len([c for c in S if c == "G"])
num_B = len([c for c in S if c == "B"])

total = num_R * num_G * num_B

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

print(total - count)

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:
            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*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):
import bisect
N=int(input())
S=list(input())
ans=0
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)
ans=len(B)*len(R)*len(G)
B.append(10000)
for r in R:
    for g in G:
        left=min(r,g)-abs(r-g)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for i in (left,right,middle):
            if i>0:
                a=bisect.bisect_left(B,i)
                if B[a]==i:
                    ans=ans-1
print(ans)

Solution (Python):


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

n_r, n_g, n_b = 0, 0, 0
for i in range(N):
    if S[i] == 'R':
        n_r += 1
    elif S[i] == 'G':
        n_g += 1
    else:
        n_b += 1


n_ignore = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k >= N:
            continue
        cond_first = i < j < k
        cond_second = S[i] != S[j] and S[i] != S[k] and S[j] != S[k]
        if cond_first and cond_second:
            n_ignore += 1

res = n_r * n_g * n_b - n_ignore
print(res)

Solution (Python):

N = int(input())
S = input()
count = 0
r = g = b = 0
for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1

_all = 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(_all-sub)

Solution (Python):

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

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

ans=0

for k in range(N//2-2+1 if N&1==0 else N//2-1+1):
    i=0
    while i+2*(k+1)<=N-1:
        ls=[S[i],S[i+k+1],S[i+2*(k+1)]]
        if "R" in ls and "G" in ls and "B" in ls:
            ans+=1
        i+=1

print(Rn*Gn*Bn-ans)

Solution (Python):
from collections import defaultdict


def main():
    n = int(input())
    S = input()
    '''
    r_inds = []
    g_inds = []
    b_inds = []
    for i in range(len(S)):
        if S[i] == "R":
            r_inds.append(i)
        elif S[i] == "G":
            g_inds.append(i)
        else:
            b_inds.append(i)

    total_comb = len(r_inds)*len(g_inds)*len(b_inds)
    ded = 0
    for i in r_inds:
        for j in g_inds:
            s_ind = min(i, j)
            b_ind = max(i, j)
            diff = b_ind - s_ind
            bigger_ind = b_ind + diff
            smaller_ind = s_ind - diff
            if bigger_ind < len(S) and bigger_ind in b_inds:
                ded += 1
            if smaller_ind >= 0 and smaller_ind in b_inds:
                ded += 1
            if diff % 2 == 0:
                mid_diff = diff // 2
                mid_ind = s_ind + mid_diff
                if mid_ind in b_inds:
                    ded += 1
    print(total_comb - ded)
    '''
    color_num = defaultdict(int)
    num_subst = 0

    for i in range(len(S) - 1):
        color_num[S[i]] += 1 
        for j in range(i + 1, len(S)):
            if S[i] != S[j]:
                diff = j - i
                idx = j + diff
                if idx < len(S) and S[idx] != S[i] and S[idx] != S[j]:
                    num_subst += 1
    color_num[S[len(S) - 1]] += 1 
    total_num_comb = color_num["R"] * color_num["G"] * color_num["B"]
    print(total_num_comb - num_subst)

if __name__ == "__main__":
    main()

Solution (Python):


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

Rcnt = 0
Gcnt = 0

excep = 0

for i in range(len(S)):
    if S[i] == 'R':
        Rcnt += 1
    elif S[i] == 'G':
        Gcnt += 1
    for j in range(1, len(S)):
        if i+j+j < len(S) and S[i] != S[i+j]:
            if S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                excep += 1
        elif i+j+j >= len(S):
            break

print(Rcnt * Gcnt * (N - Rcnt - Gcnt) - excep)

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

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

cnt = 0

for i in range(n-1):
    for j in range(i+1, n):
        if 2*j - i > n-1:
            continue
        if li[i] != li[j]:
            if li[2*j-i] != li[i] and li[2*j-i] != li[j]:
                cnt += 1
print(R*G*B - cnt)

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

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

cnt = 0

for i in range(n-1):
    for j in range(i+1, n):
        if 2*j - i > n-1:
            continue
        if li[i] != li[j]:
            if li[2*j-i] != li[i] and li[2*j-i] != li[j]:
                cnt += 1
print(R*G*B - cnt)

Solution (Python):
import sys

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

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

for i in range(n):
  c = s[i]
  if c == 'R':
    r.append(i)
  elif c == 'G':
    g.append(i)
  else:
    b.append(i)




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

for i in range(n-2):
  for d in range(1, n):
    j = i + d
    k = 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 = list(input()) 

cou = 0

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

print(S.count("R")*S.count("G")*S.count("B")-cou)

Solution (Python):
n = int(input())
s = input()
r_ary = []
g_ary = []
b_ary = []
for i in (s):
    if i == 'R':
        r_ary.append(i)
    elif i == 'G':
        g_ary.append(i)
    elif i == 'B':
        b_ary.append(i)
r = len(r_ary)
g = len(g_ary)
b = len(b_ary)
first_conditions = r*g*b
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            first_conditions -= 1
print(first_conditions)

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  
    S =     _in[1]   
    
    cnt = S.count('R')*S.count('G')*S.count('B')
    for i in range(N):
        for j in range(i+1,N):
            k = 2*j-i
            if k<N:
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    cnt -= 1
    
    print(cnt)

Solution (Python):
n = int(input())
s = input()
r_ary = []
g_ary = []
b_ary = []
for i in (s):
    if i == 'R':
        r_ary.append(i)
    elif i == 'G':
        g_ary.append(i)
    elif i == 'B':
        b_ary.append(i)
r = len(r_ary)
g = len(g_ary)
b = len(b_ary)
first_conditions = r*g*b
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            first_conditions -= 1
print(first_conditions)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    S = input()
    r,g,b = "R","G","B"

    def get_other(x,y):
        if x in [r,g] and y in [r,g]:
            return b
        if x in [r,b] and y in [r,b]:
            return g
        if x in [g,b] and y in [g,b]:
            return r

    rs = [0] * (N+1)
    gs = [0] * (N+1)
    bs = [0] * (N+1)

    for i in range(N):
        rs[i+1] = ((S[i] == r) + 0) + rs[i]
        gs[i+1] = ((S[i] == g) + 0) + gs[i]
        bs[i+1] = ((S[i] == b) + 0) + bs[i]

    s_dic = {r:rs,b:bs,g:gs}

    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            if S[i] != S[j]:
                not_k = 2 * j - i
                other = get_other(S[i],S[j])

                counter = s_dic[other]

                ans += counter[N] - counter[j+1]
                if not_k < len(S) and S[not_k] == other:
                    ans -= 1

    print(ans)



if __name__ == "__main__":
  main()

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

Solution (Python):

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



R_index=[]
G_index=[]
B_index=[]
for i,v in enumerate(S):
    if v =="R":
        R_index.append(i)
    if v =="G":
        G_index.append(i)
    if v =="B":
        B_index.append(i)


R_B =[]
B_G =[]
count_ij_jk_onaji =0
for r in R_index:
    for g in G_index:
        if 0<=g-(r-g) and g-(r-g)<N  :
            
            if S[g-(r-g)]=="B" :
                count_ij_jk_onaji +=1
               
for b in B_index:
    for r in R_index:
        if 0<=r-(b-r) and r-(b-r)<N :
            
            if S[r-(b-r)] == "G" :
                count_ij_jk_onaji +=1
                
for g in G_index:
    for b in B_index:
        if 0<=b-(g-b) and b-(g-b)<N :
            
            if S[b-(g-b)] =="R":
                count_ij_jk_onaji +=1
                

            

kotae_all = len(R_index) * len(G_index) * len(B_index) 
print(kotae_all -count_ij_jk_onaji)

Solution (Python):
def main():
  N=int(input())
  S=[1 if x=='R' else (2 if x=='G' else 3) for x in [*input()]]

  t=S.count(1)*S.count(2)*S.count(3)
  for i in range(N-2):
    j=1
    while i+2*j<N:
      if S[i]*S[i+j]*S[i+2*j]==6:
        t-=1
      j+=1

  print(t)

main()

Solution (Python):
N=int(input())
S=[1 if x=='R' else (2 if x=='G' else 3) for x in [*input()]]

t=S.count(1)*S.count(2)*S.count(3)
for i in range(N-2):
  j=1
  while i+2*j<N:
    if S[i]*S[i+j]*S[i+2*j]==6:
      t-=1
    j+=1

print(t)

Solution (Python):
import sys
from collections import Counter
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    c = Counter(S)
    r = c["R"]
    g = c["G"]
    b = c["B"]

    all_num = r*g*b
    sub = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]: continue
            k = j + (j - i)
            if k >= N or S[k] == S[i] or S[k] == S[j]: continue
            sub += 1
    print(all_num - sub)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    r = S.count("R")
    g = S.count("G")
    b = N-r-g

    all_num = r*g*b
    sub = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]: continue
            k = j + (j - i)
            if k >= N or S[k] == S[i] or S[k] == S[j]: continue
            sub += 1
    print(all_num - sub)

if __name__ == "__main__":
    main()

Solution (Python):


from collections import Counter

n=int(input())
s = input()
sc= Counter(list(s))
ans= 0
try:
    ans =  sc["R"]*sc["G"]*sc["B"]
except:
    print(0)
    exit()

for j in range(n):
    for i in range(j):
        if 2*j-i<n:
            flag = True
            for c in {"R","G","B"}:
                if c not in {s[i],s[j],s[2*j-i]}:
                    flag = False
            if flag:
                ans-=1
                

print(ans)

Solution (Python):
import itertools
n = int(input())
s = str(input())
r = {}
b = {}
g = {}
for i in range(len(s)):
    if s[i] == 'R':
        r[i+1] = s[i]
    elif s[i] == 'B':
        b[i+1] = s[i]
    else:
        g[i+1] = s[i]
ans = len(r) * len(b) * len(g)
for i in r.keys():
    for j in b.keys():
        diff = abs(j-i)
        minb = min(i,j)-diff
        maxb = max(i,j)+diff
        if maxb in g.keys():
            ans -= 1
        if minb in g.keys():
            ans -= 1
        if((i+j)%2==0):
            if((i+j)//2 in g.keys()):
                ans -=1
print(ans)

Solution (Python):
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    mx = S.count('R') * S.count('G') * S.count('B')
    for i in range(N):
        for j in range(i + 1, N):
            k = j + j - i
            if k < N:
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    mx -= 1

    print(mx)


if __name__ == '__main__':
    main()

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

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

for i in range(N):
    if S[i] == 'R':
        r = r+1
    else:
      if S[i] == 'G':
        g = g+1
      else:
        b = b+1
        
n = (N+1)//2
  
remove = 0
for i in range(1,n+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]:
            remove = remove+1
            
print(r*g*b-remove)

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

Solution (Python):
import itertools
n = int(input())
s = str(input())
r = {}
b = {}
g = {}
for i in range(len(s)):
    if s[i] == 'R':
        r[i+1] = s[i]
    elif s[i] == 'B':
        b[i+1] = s[i]
    else:
        g[i+1] = s[i]
ans = len(r) * len(b) * len(g)
for i in r.keys():
    for j in b.keys():
        diff = abs(j-i)
        minb = min(i,j)-diff
        maxb = max(i,j)+diff
        if maxb in g.keys():
            ans -= 1
        if minb in g.keys():
            ans -= 1
        if((i+j)%2==0):
            if((i+j)//2 in g.keys()):
                ans -=1
print(ans)

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


"""
脳死で全探索で探すとO(n^3）で時間がかかる。
→1つめの条件で予め絞った後に、そこからさらに絞り込むやり方をやる。（補集合を引くパターン）
j-i=k-jを満たすもの（すなわちk=2j-i）を求めて、あとで引き算すればOK.
≠は＝で考えて引き算のほうがよい。
"""

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


val = 0

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

print((r_count * g_count * b_count) - val)

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:
            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")

cnt = 0

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

print(R * G * B - cnt)

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
bad = 0
for i in range(n):
    gap = 0
    while i+(gap+1)*2 <= n-1:
        if s[i] != s[i+gap+1] and s[i+gap+1] != s[i+(gap+1)*2]\
                and s[i] != s[i+(gap+1)*2]:
            bad += 1
        gap += 1
print(r*g*b-bad)

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

N = int(input())
S = input()[:-1]
accR = [0]
accG = [0]
accB = [0]

for i in range(N):
    accR.append(accR[-1]+(1 if S[i]=='R' else 0))
    accG.append(accG[-1]+(1 if S[i]=='G' else 0))
    accB.append(accB[-1]+(1 if S[i]=='B' else 0))

ans = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i]==S[j]:
            continue
        
        s = {S[i], S[j]}
        k = 2*j-i
        
        if 'R' not in s:
            ans += accR[N]-accR[j+1]
            
            if k<N and S[k]=='R':
                ans -= 1
        elif 'G' not in s:
            ans += accG[N]-accG[j+1]
            
            if k<N and S[k]=='G':
                ans -= 1
        else:
            ans += accB[N]-accB[j+1]
            
            if k<N and S[k]=='B':
                ans -= 1
    
print(ans)

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

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

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

print(cnt-sub)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
s = str(input())
r = 0
g = 0
for i in range(n):
  if s[i] == "R":
    r += 1
  elif s[i] == "G":
    g += 1
c = r*g*(n-r-g)
for j in range(1,n-1):
  for i in range(1,min(n-j-1,j)+1):
    if (s[j-i] != s[j]) and (s[j] != s[j+i]) and (s[j+i] != s[j-i]):
      c -= 1
print(c)

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



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



ans = r * g * b

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

print(ans)

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


rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')


ans = rcnt * gcnt * bcnt



sub = 0
for i in range(n):
    for j in range(i+1, n):

        
        k = j + (j - i)
        if n <= k: break

        
        alp = set()
        alp.add(s[i])
        alp.add(s[j])
        alp.add(s[k])

        if len(alp) == 3: sub += 1

ans -= sub

print(ans)

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

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

    r_list = set()
    g_list = set()
    b_list = set()

    for i in range(n):
        if s[i] == 'R':
            r_list.add(i)
        elif s[i] == 'G':
            g_list.add(i)
        elif s[i] == 'B':
            b_list.add(i)

    for r in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

if __name__ == '__main__':
    main()

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

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

    r_list = set()
    g_list = set()
    b_list = set()

    for i in range(n):
        if s[i] == 'R':
            r_list.add(i)
        elif s[i] == 'G':
            g_list.add(i)
        elif s[i] == 'B':
            b_list.add(i)

    for r in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

if __name__ == '__main__':
    main()

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

rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

sub = 0
for i in range(n):
    for j in range(i+1, n):
        k = j + (j - i)
        if n <= k: break

        alp = set()
        alp.add(s[i])
        alp.add(s[j])
        alp.add(s[k])

        if len(alp) == 3: sub += 1

ans = rcnt * gcnt * bcnt - sub
print(ans)

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

r = 0
g = 0
b = 0

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

for c in range(n):
    center = s[c]
    for dif in range(n):
        if dif + c > n - 1 or c - dif < 0:
            continue
        left = s[c - dif]
        right = s[c + dif]
        if left != center and center != right and right != left:
            ans -= 1
print(ans)