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: Accepted

Solution code:
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,n):
    if 2*j-i>=n:
      break
    else:
      if s[i]!=s[j]!=s[2*j-i]!=s[i]:
        ans-=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for j in range(n):
  J=s[j]
  for i in range(j):
    k=2*j-i
    if k<n:
      I=s[i]
      K=s[k]
      if I!=J!=K!=I:
        ans-=1   
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
ans = s.count('R') * s.count('G') * s.count('B')

for i in range(n - 1):
    for j in range(i + 1, n):
        k = j + j - i

        if k >= n:
            break

        if (s[i] != s[j]) & (s[i] != s[k]) & (s[j] != s[k]):
            ans -= 1
            
print(ans)

Submission status: Accepted

Solution code:
import math
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,math.ceil((i+n)/2)):
    if s[i] != s[j] and s[j] != s[j*2-i] and s[j*2-i] != s[i]:
      ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()

if len(set(S)) == 1 or N < 3:
    print(0)
    sys.exit()

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


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

Submission status: Accepted

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

r=set()
g=set()
b=set()

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

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in r:
    for ig in g:
        if (ir+ig)/2 in b: 
            isum=isum+1
for ir in r:
    for ib in b:
        if (ir+ib)/2  in g: 
            isum=isum+1
for ig in g:
    for ib in b:
        if (ig+ib)/2  in r: 
            isum=isum+1

isum=lenr*leng*lenb-isum
                
print(isum)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

R = []
G = []
B = []

for i in range(N):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)

base = len(R)*len(G)*len(B)

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

print(base-cnt)

Submission status: Accepted

Solution code:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
s = input().rstrip()

colname = {'R': 0, 'G': 1, 'B': 2}
color = [[], [], []]
for i in range(n):
    ci = colname[s[i]]
    color[ci].append(i + 1)
color = sorted(color, key=lambda x: len(x))

color[2] = set(color[2])
nc2 = len(color[2])

x = 0
for r in color[0]:
    for g in color[1]:
        x += nc2
        if (r + r - g) in color[2]:
            x -= 1
        if (g + g - r) in color[2]:
            x -= 1
        c = r + g
        if c % 2 == 0 and (c // 2) in color[2]:
            x -= 1
print(x)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
    
count = 0
for i in range(N-1):
  for j in range(i+1,N):
    if 2*j-i>=N:
      break
    if S[i]!=S[j] and S[2*j-i]!=S[i] and S[2*j-i]!=S[j]:
      count += 1

print(R*G*B-count)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for j in range(n):
  for i in range(j):
    if (k:=2*j-i)<n:
      if s[i]!=s[k]!=s[j]!=s[i]:
        ans-=1   
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
b=str(input())
sum=0

riron_sum=b.count('R')*b.count('G')*b.count('B')

for i in range(0,a):
    for n in range(1,a):
        j = i+n
        k = j+n
        #print(j)
        #print(k)
        if k>=a:
        	break

        if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
                riron_sum-=1

print(riron_sum)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    B.append(i)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(B))
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(G))
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & set(B))
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & set(G))
print(res)

Submission status: Accepted

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

nr=s.count('R')
ng=s.count('G')
nb=s.count('B')
ans=nr*ng*nb
cnt=0

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
t=s.count("R")*s.count("G")*s.count("B")
cnt=0
for i in range(n):
  for j in range(i,n):
    if 2*j-i>=n:
      break
    else:
      if s[i]!=s[j]!=s[2*j-i]!=s[i]:
        cnt+=1
print(t-cnt)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(map(str, input().split()))
def pl(a): print(" ".join(list(map(str, a))))
def ma():return map(int,input().split())
"""
k=ri()

n,k=ma()
for i in range(n):
    for

n=int(input())
s=input()
r=[0]*n
b=[0]*n
g=[0]*n

for i in range(n):
    if s[i]=="R":
"""

"""
from itertools import accumulate as acc
import operator as op
import copy as cp
import math; import numpy
from bisect import bisect_left as bisl
from bisect import bisect_right as bisr
###
"""



"""
import math
k=int(input())

cnt=0
for a in range(1,k+1):
    for b in range(1,k+1):
        ct=math.gcd(a,b)
        for c in range(1,k+1):
            cnt+=math.gcd(ct,c)

print(cnt)
"""
"""
n,k=map(int,input().split())
mod=10**9+7

import math
dj0=[[i for i in range(1,k+1)] for j in range(k)]
dj1=[i for i in range(1,k+1)]
dj2=[0]*k
for i in dj1:
    for j in range(1,k):
        dj0[i-1][j-1]=math.gcd(i,j)
        dj2[math.gcd(i,j)]+=1
"""
n=ri()
s=input()
ans = s.count("R")*s.count("G")*s.count("B")
for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k>=n:
            break
        ans-=s[k]!=s[i]!=s[j]!=s[k]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[ord(c)%3for c in input()]
d=[0]*3
for i in s:d[i]+=1
a=0
for i in range(n):
    d[s[i]]-=1
    b=d.copy()
    for j in range(i+1,n):
        b[s[j]]-=1
        if s[i]==s[j]:continue
        c=(0,2,1,0)[s[i]+s[j]]
        a+=b[c]
        k=j+j-i
        if k<n and s[k]==c:
            a-=1
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
b=str(input())
sum=0

riron_sum=b.count('R')*b.count('G')*b.count('B')

for i in range(0,a):
    for n in range(0,a+1):
        j = i+n
        k = j+n
        #print(j)
        #print(k)
        if k>=a:
        	break

        if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
                riron_sum-=1

print(riron_sum)

Submission status: Accepted

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

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

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

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(str,input()))
L=[0,0,0]
for i in range(N):
  if S[i]=="R":
    L[0]+=1
  elif S[i]=="G":
    L[1]+=1
  elif S[i]=="B":
    L[2]+=1
ans=L[0]*L[1]*L[2]

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

print(str(ans))

Submission status: Accepted

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

ans = 0
for j in range(1, N-1):
    left_S = S[0:j]
    right_S = S[j+1:N]
    temp_left = [0, 0, 0]
    temp_right = [0, 0, 0]
    for i in range(len(left_S)):
        if left_S[i] == 'G':
            temp_left[0] += 1
        elif left_S[i] == 'B':
            temp_left[1] += 1
        else:
            temp_left[2] += 1
    for i in range(len(right_S)):        
        if right_S[i] == 'G':
            temp_right[0] += 1
        elif right_S[i] == 'B':
            temp_right[1] += 1
        else:
            temp_right[2] += 1
    if S[j] == 'G':
        ans += temp_left[1] * temp_right[2]
        ans += temp_left[2] * temp_right[1]
    elif S[j] == 'B':
        ans += temp_left[0] * temp_right[2]
        ans += temp_left[2] * temp_right[0]
    else:
        ans += temp_left[0] * temp_right[1]
        ans += temp_left[1] * temp_right[0]

for j in range(1, N-1):
    for i in range(1, min(len(S[0:j+1]), len(S[j:N]))):
        if S[j-i] != S[j] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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-2):
  for j in range(i+1,n-1):
    k=2*j-i
    if k>=n:
      continue
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()
#c = Counter(S)
#print (c.most_common())
#ls = list(c.values())
ans = S.count('R') * S.count('G') * S.count('B')


for i in range(N):
    for j in range(1,N):
        a = i + j
        b = i + j + j
        if (b >= N):
            break
        else:
            l = S[i]
            m = S[a]
            r = S[b]
            if l!=m and m!=r and r!=l:
                ans -= 1
        # if (i-j<0 or i+j>=N):
        #     break
        # l = S[i-j]
        # m = S[i]
        # r = S[i+j]
        # if l!=m and m!=r and r!=l:
        #     ans -= 1
print (ans)

Submission status: Accepted

Solution code:
# from bisect import bisect_left

n = int(input())
s = input()
RL = []
GL = []
BL = []
for i in range(1, n + 1):
    word = s[i - 1]
    if word == "R":
        RL.append(i)
    elif word == "G":
        GL.append(i)
    else:
        BL.append(i)
nagasa_r = len(RL)
nagasa_g = len(GL)
nagasa_b = len(BL)
SBL = set(BL)
ans = 0
if nagasa_b == 0 or nagasa_g == 0 or nagasa_r == 0:
    ans = 0
else:
    out_count = 0
    for r in RL:
        for g in GL:
            bigger = max(r, g)
            smaller = min(r, g)
            delta = bigger - smaller
            if (r + g) % 2 == 0:
                outnumber = [bigger + delta, smaller - delta, (r + g) // 2]
            else:
                outnumber = [bigger + delta, smaller - delta]
            for num in outnumber:
                if num in SBL:
                    out_count += 1
            ans += nagasa_b
    ans -= out_count
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

def solve():
    N = int(input())
    Ss = input().rstrip()

    Rs, Gs, Bs = [], [], []
    numRs = [0] * N
    numGs = [0] * N
    numBs = [0] * N
    for i in range(N):
        S = Ss[i]
        if S == 'R':
            Rs.append(i)
            numRs[i] = 1
        elif S == 'G':
            Gs.append(i)
            numGs[i] = 1
        else:
            Bs.append(i)
            numBs[i] = 1

    lenR, lenG, lenB = len(Rs), len(Gs), len(Bs)
    setR = set(Rs)
    setG = set(Gs)
    setB = set(Bs)

    accNumRevRs = list(accumulate(numRs[::-1]))[::-1]
    accNumRevGs = list(accumulate(numGs[::-1]))[::-1]
    accNumRevBs = list(accumulate(numBs[::-1]))[::-1]

    ans = 0
    for j in range(N):
        S = Ss[j]
        if S == 'R':
            for i in Gs:
                if i > j:
                    break
                ans += accNumRevBs[j]
                k = 2*j-i
                if k in setB:
                    ans -= 1
            for i in Bs:
                if i > j:
                    break
                ans += accNumRevGs[j]
                k = 2*j-i
                if k in setG:
                    ans -= 1
        elif S == 'G':
            for i in Rs:
                if i > j:
                    break
                ans += accNumRevBs[j]
                k = 2*j-i
                if k in setB:
                    ans -= 1
            for i in Bs:
                if i > j:
                    break
                ans += accNumRevRs[j]
                k = 2*j-i
                if k in setR:
                    ans -= 1
        else:
            for i in Rs:
                if i > j:
                    break
                ans += accNumRevGs[j]
                k = 2*j-i
                if k in setG:
                    ans -= 1
            for i in Gs:
                if i > j:
                    break
                ans += accNumRevRs[j]
                k = 2*j-i
                if k in setR:
                    ans -= 1

    print(ans)


solve()

Submission status: Accepted

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

RN = S.count('R')
GN = S.count('G')
BN = S.count('B')

ans = RN * GN * BN

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[k] != S[i]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
# D - RGB Triplets

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

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

ans = r * g * b

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
d = dict()
for x in 'RGB':
    d[x] = s.count(x)
ans = 0
for j in range(n-1):
    for i in range(j):
        if s[i] != s[j]:
            for t in 'RGB':
                if s[i] != t != s[j]:
                    break
            ans += d[t] - ((2*j - i) < n and t == s[2*j - i])
    d[s[j]] -= 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

count = R*G*B
cnt = 0
for i in range(len(s)):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            count -= 1
print(count)

Submission status: Accepted

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

N = INT()
S = input()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools
import bisect

N = int(input())
S = input()
R_set = set()
G_set = set()
B_set = set()
R = []
G = []
B = []
R_ids = [0] * N
G_ids = [0] * N
B_ids = [0] * N

for i, s in enumerate(S):
    if s == 'R':
        R_set.add(i)
        R.append(i)
        R_ids[i] = 1
    elif s == 'G':
        G_set.add(i)
        G.append(i)
        G_ids[i] = 1
    else:
        B_set.add(i)
        B.append(i)
        B_ids[i] = 1


R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]
G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]
B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]

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

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

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


print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

Submission status: Accepted

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

R = S.count('R')
G = S.count('G')
B = S.count('B')
tmp = 0
for i in range(N):
    for j in range(i+1, N):
        k = j + (j - i)
        if k >= N:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            tmp-=1
print(tmp + R*G*B)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
R = s.count("R")
G = s.count("G")
B = s.count("B")
ans = R*G*B

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r = []
g = []
b = []
for i in range(N):
  if S[i] == 'R':
    r.append(i)
  elif S[i] == 'G':
    g.append(i)
  else:
    b.append(i)
ans = len(r) * len(g) * len(b)
sub = 0
for i in range(1,N//2+1):
  if 2 * i == N:
    break
  for k in range(N):
    if S[sub] != S[sub + i] and S[sub + i] != S[sub + 2 * i] and S[sub + 2 * i] != S[sub]:
      ans -= 1
    if sub + 2 * i + 1< N:
      sub += 1
    else:
      sub = 0
      break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

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

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

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

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

cnt = 0
for i in range(0, n):
    first = s[i]
    for j in range(i + 1, n):
        if s[j] == first:
            continue
        second = s[j]
        target = notab(first, second)
        if j+(j-i) < n and s[j+(j-i)] == target:
            cnt += cntmap[target][j+1] - 1
        else:
            cnt += cntmap[target][j+1] 
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

nr=s.count('R')
ng=s.count('G')
nb=s.count('B')
ans=nr*ng*nb 
cnt=0

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline


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

    ans = s.count('R') * s.count('G') * s.count('B')
    # print(ans)
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            k = (j - i) + j
            if k >= n:
                # print(i, j, k)
                break

            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

count = r_count*g_count*b_count


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

print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

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