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



Sample input:
4
RRGB


Sample output:
1

Submission status: Runtime Error

Solution code:
from collections import Counter

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

if N == 1 or N == 2:
    ans = 0
else:
    c = Counter(S)
    tmp = c.most_common()
    rgb = tmp[0][1] * tmp[1][1] * tmp[2][1]

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

print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

c = Counter(S)
tmp = c.most_common()
rgb = tmp[0][1] * tmp[1][1] * tmp[2][1]

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

print(rgb - count)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

c = Counter(S)
tmp = c.most_common()
rgb = tmp[0][1] * tmp[1][1] * tmp[2][1]

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

print(rgb - count)

Submission status: Runtime Error

Solution code:
from numba import njit
import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

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


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


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

Submission status: Runtime Error

Solution code:
import collections
def f(s):
  h = {('R','G'): 'B', ('B','G'): 'R', ('R','B'): 'G',('R','G')[::-1]: 'B', ('B','G')[::-1]: 'R', ('R','B')[::-1]: 'G', }

  c = collections.Counter()
  cc = [None] * (len(s)+1)
  cc[-1] = c
  for jj in range(len(s)-1,-1,-1):
    c[s[jj]]+=1
    cc[jj] = c
    res = 0
    for i in range(len(s)):
      for j in range(i+1, len(s)):
        if s[j] != s[i]:
          res += cc[j+1][h[(s[i],s[j])]]

  return res
raw_input()
print f(raw_input())

Submission status: Runtime Error

Solution code:
import collections
def f(s):
  h = {('R','G'): 'B', ('B','G'): 'R', ('R','B'): 'G', }
  for k in h:
    h[k[::-1]] = h[k]
  c = collections.Counter()
  cc = [None] * (len(s)+1)
  cc[-1] = c
  for jj in range(len(s)-1,-1,-1):
    c[s[jj]]+=1
    cc[jj] = c
    res = 0
    for i in range(len(s)):
      for j in range(i+1, len(s)):
        if s[j] != s[i]:
          res += cc[j+1][h[(s[i],s[j])]]

  return res
raw_input()
print f(raw_input())

Submission status: Runtime Error

Solution code:
import collections
def f(s):
  	h = {('R','G'): 'B', ('B','G'): 'R', ('R','B'): 'G', }
    for k in h:
      	h[k[::-1]] = h[k]
    c = collections.Counter()
    cc = [None] * (len(s)+1)
    cc[-1] = c
    for jj in range(len(s)-1,-1,-1):
      	c[s[jj]]+=1
        cc[jj] = c
	res = 0
    for i in range(len(s)):
		for j in range(i+1, len(s)):
          	if s[j] != s[i]:
              	res += cc[j+1][h[(s[i],s[j])]]
                
	return res
raw_input()
print f(raw_input())

Submission status: Runtime Error

Solution code:
count = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            for k in range(j+1,n):
                if j-i != k-j:
                    K = s[k]
                    if s[i]!=K and s[j]!=K :
                        count += 1
print(count)

Submission status: Runtime Error

Solution code:
@njit
def solve():
    n, s = int(input()), input()
    a = ["R", "G", "B"]
    cnt = [0, 0, 0]
        for i in s:
        cnt[a.index(i)] += 1
    c = cnt[0] * cnt[1] * cnt[2]
    for i in range(1, n-1):
        for j in range(i+1, n):
            k = 2 * j - i
            if j-1 < k-1 < n:
                if s[i-1] != s[j-1] and s[j-1] != s[k-1] and s[k-1] != s[i-1]:
                    c -= 1
    return c
print(solve())

Submission status: Runtime Error

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

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

r = list()
g = list()
b = list()

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

bb = len(b)
gg = len(g)
rr = len(r)

sum = len(r) * len(g) * len(b)
for i in range(bb-1):
    for j in range(i+1,gg):
        if 2*j - i <= 0:
            k = 2*j - i
        if j - i == k - j:
            sum -=1
        #if abs(abs(g[j] - b[i]) - b[i]) in r:
#        if (abs(b[i] - abs(g[j] - b[i])) in r) or (b[i] + abs(g[j] - b[i]) in r):
#            sum -=1
        #elif abs(abs(g[j] - b[i]) - g[j]) in r:
#        elif (abs(g[j] - abs(g[j] - b[i])) in r) or (g[j] + abs(g[j] - b[i]) in r):
#            sum -=1
#        elif (abs(r[j] - abs(r[j] - b[i])) in g) or (r[j] + abs(r[j] - b[i]) in g):
#            sum -=1
#        for k in range(rr):
#            if abs(r[k] - g[j]) == abs(g[j] - b[i]) or abs(g[j] - b[i]) == abs(b[i] - r[k]) or abs(b[i] - r[k]) == abs(r[k] - g[j]):
#                count += 1
print(sum)

Submission status: Runtime Error

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

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

r = list()
g = list()
b = list()

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

bb = len(b)
gg = len(g)
rr = len(r)

sum = len(r) * len(g) * len(b)
for i in range(bb-1):
    for j in range(i+1,gg):
        if 2*j - i <= 0:
            k = 2*j - i
        if i - j == k - j:
            sum -=1
        #if abs(abs(g[j] - b[i]) - b[i]) in r:
#        if (abs(b[i] - abs(g[j] - b[i])) in r) or (b[i] + abs(g[j] - b[i]) in r):
#            sum -=1
        #elif abs(abs(g[j] - b[i]) - g[j]) in r:
#        elif (abs(g[j] - abs(g[j] - b[i])) in r) or (g[j] + abs(g[j] - b[i]) in r):
#            sum -=1
#        elif (abs(r[j] - abs(r[j] - b[i])) in g) or (r[j] + abs(r[j] - b[i]) in g):
#            sum -=1
#        for k in range(rr):
#            if abs(r[k] - g[j]) == abs(g[j] - b[i]) or abs(g[j] - b[i]) == abs(b[i] - r[k]) or abs(b[i] - r[k]) == abs(r[k] - g[j]):
#                count += 1
print(sum)

Submission status: Runtime Error

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

r1=[]
g1=[]
b1=[]
a=0
b=0
c=0
d=0
for i in range(n):
    if s[i]=="R":
		r1.append(i)
        a+=1
    elif s[i]=="G":
        g1.append(i)
        b+=1
    else:
        b1.append(i)
        c+=1
for i in range(a):
    for j in range(b):
        for k in range(c):
            if abs(g1[j]-r1[i])!=abs(b1[k]-g1[j]) or abs(r1[i]-g1[j])!=abs(b1[k]-r1[i]) or abs(r1[i]-b1[k])!=abs(g1[i]-b1[k]):
              d=d+1

print(d)

Submission status: Runtime Error

Solution code:
import collections

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

lenR=S.count("R")
lenG=S.count("G")
lenB=S.count("B")

count=0
for i in range(N-2):
    for j in range(i,N-1):
        if 2*j-i>N:
            continue
        if (S[i] != S[j]) and (S[i]!=S[2*j-i]) and (S[j]!=S[2*j-i]):
            count+=1
    
print(lenR*lenG*lenB-count)

Submission status: Runtime Error

Solution code:
import collections

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

lenR=S.count("R")
lenG=S.count("G")
lenB=S.count("B")

count=0
for i in range(N-2):
    for j in range(i,N-1):
        if 2*j-i>N:
            continue
        if (S[i] != S[j]) and (S[i]!=S[2*j-i]) and (S[j]!=S[2*j-i]):
                    count+=1
    
print(lenR*lenG*lenB-count)

Submission status: Runtime Error

Solution code:
import collections
N=int(input())
S=list(input())
s=collections.Counter(S)
m=s["R"]*s["G"]*s["B"]
s=0
for i in range(1,N-1):
  for j in range(i,N):
    k=2*j-i
    if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
      s+=1
print(m-s)

Submission status: Runtime Error

Solution code:
def quest_d():
    N = int(input())
    S = input()

    index_dict = {
        'R':0,
        'G':0,
        'B':0
    }
    for index in range(len(S)):
        index_dict[S[index]] = index_dict[S[index]] + 1

    if index_dict['R'] * index_dict['G'] * index_dict['B'] == 0:
        print(0)
        return

    rrr = combi_three(index_dict['R'])
    ggg = combi_three(index_dict['G'])
    bbb = combi_three(index_dict['B'])
    rrg = combi_three(index_dict['R'] + index_dict['G'])
    ggb = combi_three(index_dict['G'] + index_dict['B'])
    bbr = combi_three(index_dict['B'] + index_dict['R'])

    i = 0
    sum = 0
    while i < len(S):
        a = S[i]
        j = i
        while j < len(S) - 1:
            j += 1
            if S[j] == a:
                continue

            k = 2 * j - i
            if k < len(S):
                b = S[j]
                if S[k] != a and S[k] != b:
                    sum += 1
        i += 1
    print(combi_three(N) - sum - rrg - bbr - ggb + rrr + ggg + bbb)

if __name__ == '__main__':
    quest_d()

Submission status: Runtime Error

Solution code:
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            if (rind[i]+gind[j]!=2*bind[k])and(gind[j]+bind[k]!=rind[i])and(bind[k]+rind[i]!=gin[j]):
                ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

############################################

# read data for n sequences.
N = int(input())
data = input()

out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in np.asarray([d for d in "RGB"]):
                    if i!=d and j!=d:
                        #sum1 = np.sum(np.asarray([d for d in data[jj+1:]])==d)
                        sum1 = 0
                        for l in data[jj+1:]:
                            if d == l:
                                sum1 +=1
                        let = d
                out+=sum1
                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

Submission status: Runtime Error

Solution code:
import numpy as np
#N = int(input())
#S = str(input())
s = np.array(list(S))
cnt = 0
for i in range(N-2):
    f1 = np.ones(N) * True
    f1[:i] =  False # set False at index of less than and equal to i-1
    f2 = np.where(s != s[i], True, False) #  not same color
    ff = np.array(f1*f2,dtype=bool)
    J = np.arange(N)[ff]
    print("J = ",J)
    for j in J:
        f3 = np.ones(N) * True
        f3[:j] = False  # set False at index of less than and equal to i-1
        f4 = f2*np.where(s != s[j], True, False)  # not same color
        ff = np.array(f3*f4,dtype=bool)
        K = np.arange(N)[ff]
        print("K = ", K)

    for j in J:
        for k in range(j,N):
            if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                cnt +=1
print(cnt)

Submission status: Runtime Error

Solution code:
def main():
 
  import sys
  input() = sys.stdin.readline()[:-1]
  
  import collections
 
  N = int(input())
  S = list(input())
 
  CNT_S = collections.Counter(S)
 
  cnt_r = CNT_S['R']
  cnt_b = CNT_S['B']
  cnt_g = CNT_S['G']
 
  total = cnt_r * cnt_g * cnt_b
  subdu = 0
 
 
  for pos in range(N):
      for i in range(1, N):
 
          index_r = pos
          index_m = pos + i
          index_l = pos + 2 * i
 
          if (index_l < N and index_m < N and index_r < N):
              if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                  subdu += 1
 
 
  ans = total - subdu
 
  print(ans)
  
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s_li = list(input().rstrip())

    r_li = []
    g_li = []
    b_li = []

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

    if (len(r_li) < 1 or len(g_li) < 1) or len(b_li) < 1:
        print(0)
    else:
        all_c = len(r_li) * len(g_li) * len(b_li)

        for i in range(n):
            for j in range(i+1,n):
                k = 2*j-i
                if k >= n:
                    continue

                if (s_li[i] != s_li[j] and s_li[j] != s_li[k]) and s_li[i] != s_li[k]:
                    all_c -= 1

        print(all_c)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

arr = []
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)))
tot = R*G*B

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]) & (s[i] != s[k]) & (s[j] != s[k]):
      tot -= 1

print(tot)

Submission status: Runtime Error

Solution code:
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 s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            ans-=1

print(ans)

Submission status: Runtime Error

Solution code:
import collections
 
N = int(input())
S = list(input())
 
CNT_S = collections.Counter(S)
 
cnt_r = CNT_S['R']
cnt_b = CNT_S['B']
cnt_g = CNT_S['G']
 
total = cnt_r * cnt_g * cnt_b
subdu = 0
 
 
for pos in range(N):
    for i in range(1, N):
    
        index_r = pos
        index_m = pos + i
        index_l = pos + 2 * i
        
        if (index_l < N and index_m < N and index_r < N):
            if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                subdu += 1
 
 
ans = total - subdu
 
print(ans)

Submission status: Runtime Error

Solution code:
import collections

n = int(input())
s = input()
res = 0
# for i in range(0, n):
#     for j in range(i+1,n):
#         if s[i] != s[j]:
#             for k in range(j+1,n):
#                 if j-i!=k-j and s[i] != s[k] and s[j] != s[k]:
#                     res += 1
#
# print(res)
num = collections.Counter(s)
# print(num)
num_r = num["R"]
num_g = num["G"]
num_b = n - num_r - num_g
count_2 = 0
for x in range(1,int(n/2)+1):
    for i in range(n-2*x-i):
        # print(x)
        # print(i)
        # print()
        if (i + 2 * x < n and s[i] != s[i + x] and s[i + x] != s[i + 2 * x] and s[i] != s[i + 2 * x] ):
            count_2 += 1
            # print(x)
            # print(i)
            # print()

res = num_r * num_g * num_b - count_2
# print(num_r)
# print(num_g)
# print(num_b)
print(res)

Submission status: Runtime Error

Solution code:
def diff_is_unequal(a, b, c):
    s, m, l = sorted([a, b, c])
    return (m - s) != (l - m)

input()
indices = dict()
for idx, c in enumerate(input()):
    indices[c] = indices.get(c, []) + [idx]

print(sum(diff_is_unequal(r, g, b) for b in indices["B"] for g in indices["G"] for r in indices["R"]))

Submission status: Runtime Error

Solution code:
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(i+1,n):
        if 2*j - i <= n - 1:
            k = 2*j - i
            if len(set(s[i],s[j],s[k])) == 3:
                cnt += 1
print(R*G*B - cnt)

Submission status: Runtime Error

Solution code:
import itertools
N=int(input())
A=list(input())
j=0
cnt_d=0
n=N//3+1
res=0
for i in range(1,n+1):
    for j in range(1,n+1-i):
        B=A[j]+A[j+i]+A[j+2*i]
        # print(n,B)
        if ('R' in B)& ('G' in B)&('B' in B):
            cnt_d=cnt_d+1       
for S in list(itertools.combinations(A,3)):
    if ('R' in S)& ('G' in S)&('B' in S):
        # print(S)
        res=res+1
print(res-cnt_d)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
compte=0
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)
 
for i in range(1,n-1):
    if n%2==0:
        if i<n//2:
            for j in range(1,i+1):
                if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                    compte+=1
        else:
            for j in range(1,n-i):
                if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                    compte+=1
    else:
            if i<(n-1)//2:
                for j in range(1,i+1):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        compte+=1
            elif i==(n-1)//2:
                for j in range(1,n-i):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        compte+=1
            elif i>(n-1)//2:
                for j in range(1,n-i):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        compte+=1
 
print(len(r)*len(g)*len(b)-count)

Submission status: Runtime Error

Solution code:
S = str(input()) 
N = int(input())

r = S.count('R')
g = S.count('G')
b = S.count('B')
total = r * g * b

k = 0
count = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if(i>=j or j >=k or i >=k): continue
        if(i==j or j==k or k==i or k<0 or k>=N): continue
        if(S[i] == S[j] or S[j] == S[k] or S[k] == S[i]): continue
        
        count += 1
        
print(total - count)

Submission status: Runtime Error

Solution code:
S=str(input()) 
N = int(input())

r = S.count('R')
g = S.count('G')
b = S.count('B')
total = r * g * b

k = 0
count = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if(i>=j or j >=k or i >=k): continue
        if(i==j or j==k or k==i or k<0 or k>=N): continue
        if(S[i] == S[j] or S[j] == S[k] or S[k] == S[i]): continue
        
        count += 1
        
print(total - count)

Submission status: Runtime Error

Solution code:
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, 2):
    if s[i] != s[j] and s[i] != s[(i+j)/2] and s[(i+j)/2] != s[j]:
      count -= 1
print(count)

Submission status: Runtime Error

Solution code:
n, s = input().split(' ')
n = int(n)

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

Submission status: Runtime Error

Solution code:
import bisect
n = int(raw_input())
s = raw_input()
d = {}
for i in s:
	d[i] = []
for i in range(len(s)):
	d[s[i]].append(i)
p = d['R']
q = d['G']
r = d['B']
p1 = len(p)
q1 = len(q)
r1 = len(r)
z = 0
for i in range(len(p)):
	for j in range(len(q)):
		l = sorted([p[i],q[j]])
		to = bisect.bisect_left(r,l[1])
		isTrue = bisect.bisect_left(r[to:],2*l[1]-l[0])
		try:
			if (r[to:])[isTrue] == 2*l[1]-l[0]:
				z+=r1-to-1
			else:
				z+=r1-to
		except:
			z+=r1-to
for i in range(len(q)):
	for j in range(len(r)):
		l = sorted([q[i],r[j]])
		to = bisect.bisect_left(p,l[1])
		isTrue = bisect.bisect_left(p[to:],2*l[1]-l[0])
		try:
			if (p[to:])[isTrue] == 2*l[1]-l[0]:
				z+=p1-to-1
			else:
				z+=p1-to
		except:
			z+=p1-to
		
for i in range(len(p)):
	for j in range(len(r)):
		l = sorted([p[i],r[j]])
		to = bisect.bisect_left(q,l[1])
		isTrue = bisect.bisect_left(q[to:],2*l[1]-l[0])
		try:
			if (q[to:])[isTrue] == 2*l[1]-l[0]:
				z+=q1-to-1
			else:
				z+=q1-to
		except:
			z+=q1-to

print(z)

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
s = input()
d = {}
for i in s:
	d[i] = []
for i in range(len(s)):
	d[s[i]].append(i)
p = d['R']
q = d['G']
r = d['B']
p1 = len(p)
q1 = len(q)
r1 = len(r)
z = 0
for i in range(len(p)):
	for j in range(len(q)):
		l = sorted([p[i],q[j]])
		to = bisect.bisect_left(r,l[1])
		isTrue = bisect.bisect_left(r[to:],2*l[1]-l[0])
		try:
			if (r[to:])[isTrue] == 2*l[1]-l[0]:
				z+=r1-to-1
			else:
				z+=r1-to
		except:
			z+=r1-to
for i in range(len(q)):
	for j in range(len(r)):
		l = sorted([q[i],r[j]])
		to = bisect.bisect_left(p,l[1])
		isTrue = bisect.bisect_left(p[to:],2*l[1]-l[0])
		try:
			if (p[to:])[isTrue] == 2*l[1]-l[0]:
				z+=p1-to-1
			else:
				z+=p1-to
		except:
			z+=p1-to
		
for i in range(len(p)):
	for j in range(len(r)):
		l = sorted([p[i],r[j]])
		to = bisect.bisect_left(q,l[1])
		isTrue = bisect.bisect_left(q[to:],2*l[1]-l[0])
		try:
			if (q[to:])[isTrue] == 2*l[1]-l[0]:
				z+=q1-to-1
			else:
				z+=q1-to
		except:
			z+=q1-to

print(z)

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
s = input()
d = {}
for i in s:
	d[i] = []
for i in range(len(s)):
	d[s[i]].append(i)
p = d['R']
q = d['G']
r = d['B']
z = 0
for i in range(len(p)):
	for j in range(len(q)):
		l = sorted([p[i],q[j]])
		to = bisect.bisect_left(r,l[1])
		isTrue = bisect.bisect_left(r[to:],2*l[1]-l[0])
		try:
			if (r[to:])[isTrue] == 2*l[1]-l[0]:
				z+=len(r[to:])-1
			else:
				z+=len(r[to:])
		except:
			z+=len(r[to:])
for i in range(len(q)):
	for j in range(len(r)):
		l = sorted([q[i],r[j]])
		to = bisect.bisect_left(p,l[1])
		isTrue = bisect.bisect_left(p[to:],2*l[1]-l[0])
		try:
			if (p[to:])[isTrue] == 2*l[1]-l[0]:
				z+=len(p[to:])-1
			else:
				z+=len(p[to:])
		except:
			z+=len(p[to:])
		
for i in range(len(p)):
	for j in range(len(r)):
		l = sorted([p[i],r[j]])
		to = bisect.bisect_left(q,l[1])
		isTrue = bisect.bisect_left(q[to:],2*l[1]-l[0])
		try:
			if (q[to:])[isTrue] == 2*l[1]-l[0]:
				z+=len(q[to:])-1
			else:
				z+=len(q[to:])
		except:
			z+=len(q[to:])

print(z)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
d = {}
for i in s:
	d[i] = []
for i in range(len(s)):
	d[s[i]].append(i)
p = d['R']
q = d['G']
r = d['B']
z = 0
for i in range(len(p)):
	for j in range(len(q)):
		for k in range(len(r)):
			l = sorted([p[i],q[j],r[k]])
			if l[1]-l[0]!=l[2]-l[1]:
				z+=1
print(z)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
sum = 0
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

sum = r*g*b

for i in range(N):
    for j in range(N):
        if i+2*j < N:    
	    a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1

print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
sum = 0
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
 
sum = r*g*b
 
for i in range(N):
    for j in range(N):
        if i+2*j < N:    
	    a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        
 
print(sum)

Submission status: Runtime Error

Solution code:
from functools import lru_cache

@lru_cache(maxsize=1000)
N = int(input())
S =input()
a = 0
for i in range(N-2):
  for j in range(i,N-1):
    for k in range(j,N):
      if S[i] != S[j] and S[i] != S[k] and S[k] != S[j] and j -i != k-j:
        a+= 1
        print(a)

Submission status: Runtime Error

Solution code:
from itertools import accumlate
N=int(input())
s=input()
R=[]
G=[]
B=[]
ans=0
#RGBの累積和を作る
for i in range(N):
  if s[i]=="R":
    R.append(s[i])
  elif s[i]=="G":
    G.append(s[i])
  else:
    B.append(s[i])
R.accumlate()
G.accumlate()
B.accumlate()
#作った

#全探索する
for i in range(N):
  a=s[i]
  for j in range(i+1,N):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if "R" not in a+b:
      ans+=R[N]-R[j+1]
      if s[k]=="R":
        ans-=1
    if "G" not in a+b:
      ans+=G[N]-G[j+1]
      if s[k]=="G":
        ans-=1
    if "B" not in a+b:
      ans+=B[N]-B[j+1]
      if s[k]=="B":
        ans-=1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
s = list(input())
counts = dict(Counter(s))
r = 0;g = 0;b= 0
for i in s:
    if i=="R":
        r += 1
    elif i=="G":
        g += 1
    else:
        b += 1
all = r*g*b
sub = 0
for i in range(N-1):
    for j in range(i+1,N):
        if s[i]==s[j]:
            continue
        k = 2*j - i
        if k >= N:
            continue
        else:
            if (s[k]!=s[i] and s[k]!=s[j]):
                sub += 1
print(all-sub)

Submission status: Runtime Error

Solution code:
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] & s[j]!=s[k] & s[k]!=s[i]:
            ans-=1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
c=0
for i in range(1,n+1):
    for j in range(i+1,n+1):
        if if s[i-1]==s[j-1]:
            continue
        for k in range(j+1,n+1):
            if s[k-1]!=s[j-1] and s[i-1]!=s[k-1]:
                if j-i != k-j:
                    c+=1
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
c=0
for i in range(1,n+1):
    for j in range(i+1,n+1):
        if if s[i-1]==s[j-1]:
            break
        for k in range(j+1,n+1):
            if s[k-1]!=s[j-1] and s[i-1]!=s[k-1]:
                if j-i != k-j:
                    c+=1
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
r,g,b=[],[],[]
ans=0
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)
for i in range(len(r)):
  for j in range(len(g)):
    l=[r[i],g[j],b[2*j-i]]
    l.sort()
    if l[1]-l[0]!=l[2]-l[1]:
      ans+=1
print(ans)

Submission status: Runtime Error

Solution code:

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

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

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

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter
from numpy import prod

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3:
            cut+=1
d=Counter(s)
print(prod(list(d.values()))-cut)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter
from numpy import prod

n,=I()
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3:
            cut+=1
d=Counter(s)
print(prod(list(d.values()))-cut)

Submission status: Runtime Error

Solution code:
N = input();
S = input();
A = []; 

for i in range(len(S)):
  A.append(S[i]);

num = 0;

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if j-i != k-j:
        if A[i] != A[j] and A[j] != A[k] and A[k] != A[i]:
          num += 1;
        else:
          pass
      else:
        pass
      
print(num)

Submission status: Runtime Error

Solution code:

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

SN = []

for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)

rem2 = ['RRR', 'GGG', 'BBB']

count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1

print(ans-count)

Submission status: Runtime Error

Solution code:
from itertools import product

k = int(input())
s = input()

temp_dict = {
    "R": [],
    "G": [],
    "B": [],
}
for i, c in enumerate(s):
    temp_dict[c].append(i)

ans = 0
temp_list = list(product(temp_dict["R"], temp_dict["G"], temp_dict["B"]))
for temp in temp_list:
    temp.sort()
    if temp[0] - temp[1] != temp[1] - temp[2]:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
temp_dict = {
    "R": [],
    "G": [],
    "B": [],
}
for i, c in enumerate(s):
    temp_dict[c].append(i)

ans = 0
temp_list = list(product(temp_dict["R"], temp_dict["G"], temp_dict["B"]))
for temp in temp_list:
    temp = sorted(list(temp))
    if temp[0] - temp[1] != temp[1] - temp[2]:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
            if k < N:
                s = S[i] + S[j] + S[k]
                if ("R" in s) & ("G" in s) & ("B" in s):
                    ans -= 1

print(ans)

Submission status: Runtime Error

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

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

tmp=0

for i in range(N):
  for j in range(i,N):
    if j-i+j < N:
      if S[i]==S[j] and S[j]==S[j-i+j] and S[i]==S[j-i+j]
        tmp += 1

ans = ans - tmp
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd
N=int(input())
S=input()

S_list = pd.Series([x for x in S])
r = sum(S_list == "R")
g = sum(S_list == "G")
b = sum(S_list == "B")
 
ans = r * g * b
 
for i in range(N):
  for k in [N - k_ - 1 for k_ in range(N)]:
    tar_j = i + k
    if (tar_j > 2 * i) & (tar_j < 2 * k):
      if np.mod(tar_j, 2) == 0:
        j = int(tar_j / 2)
        s = S[i] + S[j] + S[k]
        if ("R" in s) & ("G" in s) & ("B" in s):
          ans -= 1
    else:
      break
print(ans)