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()
col={'R','G','B'}
r,g,b=[0]*n,[0]*n,[0]*n
rc,gc,bc=0,0,0
ans=0
for i in range(n-1,-1,-1):
    if (s[i]=='R'): rc+=1
    elif (s[i]=='G'): gc+=1
    elif (s[i]=='B'): bc+=1
    r[i],g[i],b[i]=rc,gc,bc
for i in range(n):
    for j in range(i+1,n):
        if s[i]!='R' and s[j]!='R' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='R':
                ans+=r[j]-1
            else: ans+=r[j]
        elif s[i]!='G' and s[j]!='G' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='G':
                ans+=g[j]-1
            else: ans+=g[j]
        elif s[i]!='B' and s[j]!='B' and s[i]!=s[j]:
            if j+j-i<n and s[j+j-i]=='B':
                ans+=b[j]-1
            else: ans+=b[j]
        #print(ans,i,j)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

s = input()

# print(s)
# brute force
rs = 0
gs = 0
bs = 0
for x in s:
    if x == 'R':
        rs += 1
    if x == 'G':
        gs += 1
    if x == 'B':
        bs += 1

i = 0
d = 1
count = 0
for i in range(0, n):
    d = 1
    while (i + 2*d < n):
        ok = s[i] != s[i+d] and s[i+d] != s[i+2*d] and s[i] != s[i+2*d]
        if ok:
            count += 1
        d += 1


print(rs * gs * bs - count)
# print(rs, gs, bs)

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

# print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items(): a *= 1 if k==s else v
    b = sum([len(set(s+S[:i+1][::-(j+1)][:3]))//3 for j in range(i//2)])
    ans += a-b
    memo[s] += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a = [str(c) for c in s]
ans=0
r=0
g=0
b=0
er=0
for i in range(n):
    if a[i]=='R':
        r+=1
        ans=ans+g*b
    elif a[i]=='G':
        g+=1
        ans=ans+r*b
    else:
        b+=1
        ans=ans+g*r
for i in range(n-1):
    for j in range(i+1,n):
        k=j+(j-i)
        if k<n:
            if a[i]!=a[j] and a[j]!=a[k] and a[k]!=a[i]:
                er+=1
print(ans-er)

Submission status: Accepted

Solution code:
# 2020/04/12
# AtCoder Beginner Contest 162 - D

# Input
n = int(input())
s = input()

# Calc
acmsum = [[0] * (n+1) for i in range(3)]

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

ans = 0
for i in range(1, n-1):
    j = 1
    x = 0
    while(i+j < n and i-j >= 0):
        if s[i-j] != s[i] and s[i-j] != s[i+j] and s[i] != s[i+j]:
            x = x + 1
        j = j + 1
    
    if s[i] == 'R':
        ans = ans + (acmsum[1][i]) * (acmsum[2][n] - acmsum[2][i]) - x
        ans = ans + (acmsum[2][i]) * (acmsum[1][n] - acmsum[1][i])
        
    elif s[i] == 'G':
        ans = ans + (acmsum[0][i]) * (acmsum[2][n] - acmsum[2][i]) - x
        ans = ans + (acmsum[2][i]) * (acmsum[0][n] - acmsum[0][i])
        
    else:
        ans = ans + (acmsum[1][i]) * (acmsum[0][n] - acmsum[0][i]) - x
        ans = ans + (acmsum[0][i]) * (acmsum[1][n] - acmsum[1][i])

# Output
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
count = 0
i = 0
j = 1
while (i) < n:
    j = 1
    while (i+2*j) < n:
        if s[i] != s[i+j] and s[i+2*j] not in [s[i],s[i+j]]:
            count += 1
        j += 1
    i += 1
r,g,b = 0,0,0
for p in s:
    if p == 'R':
        r += 1
    elif p == 'G':
        g += 1
    else:
        b += 1
print(r*g*b-count)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=list(input())
c=collections.Counter(s)
d=c["R"]*c["G"]*c["B"]
ln=n//3
cnt=0
for y in range(1,n+1):
    for x in range(0,n+1):
        if x+2*y>=n:break
        g0=s[x]
        g1=s[x+y]
        g2=s[x+2*y]
        if g0!=g1 and g1!=g2 and g0!=g2:cnt+=1
print(d-cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
res=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)

res=len(g)*len(b)*len(r)
r=set(r)
g=set(g)
b=set(b)
for i in r:
  for j in g:
    if (i+j)%2==0 and int((i+j)/2) in b:
      res-=1
    if abs(i-j)+max(i,j) in b:
      res-=1
    if min(i,j)-abs(i-j) in b:
      res-=1
print(res)

Submission status: Accepted

Solution code:
n = int(input())
string = input()
red = 0
gre = 0
blu = 0
for i in range(n):
    if(string[i] == 'R'):
        red+=1
    elif(string[i] == 'G'):
        gre += 1
    else:
        blu += 1
sum = red * gre * blu
for i in range(n):
    if(i > n - i - 1):
        num = n - i - 1
    else:
        num = i
    for l in range(1,num + 1):
        if(string[i - l] != string[i] and string[i - l] != string[i + l] and string[i + l] != string[i]):
            sum -= 1
print(sum)

Submission status: Accepted

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

def b_search(A,key):
    left,right,diff = 0,len(A),0
    output = False
    count = 0
    while left < right and count < 15:
        mid = (left+right)//2
        if A[mid] == key:
            return True
        elif A[mid] < key:
            left = mid
        else:
            right = mid
        count += 1
        #print(left,mid,right)

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

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

if len(R) <= len(G) and len(B) <= len(G):
    G,B = B,G
elif len(G) <= len(R) and len(B) <= len(R):
    R,B = B,R

#最長はB
ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        tmp = 0
        if R[i] < G[j]:
            #b < r < g or r < b < g or r < g < b
            if b_search(B,R[i]-(G[j]-R[i])):
                tmp += 1
            if (G[j]-R[i]) % 2 == 0 and b_search(B,(G[j]+R[i])//2):
                tmp += 1
            if b_search(B,G[j]+(G[j]-R[i])):
                tmp += 1
            ans += len(B)-tmp
        else:
            #b < g < r or g < b < r or g < r < b
            if b_search(B,G[j]-(R[i]-G[j])):
                tmp += 1
            if (R[i]-G[j]) % 2 == 0 and b_search(B,(R[i]+G[j])//2):
                tmp += 1
            if b_search(B,R[i]+(R[i]-G[j])):
                tmp += 1
            ans += len(B)-tmp

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
s = input()
d = 0
for i in range(1, math.ceil(n / 2)):
    for j in range(i, n - i):
        if s[j - i] != s[j] and s[j] != s[j + i] and s[j - i] != s[j + i]:
            d += 1
print(s.count('R') * s.count('G') * s.count('B') - d)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
rn = [0 for _ in range(N)]
gn = [0 for _ in range(N)]
bn = [0 for _ in range(N)]
rgn = [0 for _ in range(N)]
gbn = [0 for _ in range(N)]
brn = [0 for _ in range(N)]
for i, s in enumerate(S):
    if i == 0:
        if s == "R":
            rn[i] = 1
            rgn[i] = 1
            brn[i] = 1
        if s == "G":
            gn[i] = 1
            rgn[i] = 1
            gbn[i] = 1
        if s == "B":
            bn[i] = 1
            gbn[i] = 1
            brn[i] = 1
    else:
        if s == "R":
            rn[i] = rn[i-1] + 1
            gn[i] = gn[i-1]
            bn[i] = bn[i-1]
            rgn[i] = rgn[i-1] + 1
            gbn[i] = gbn[i-1]
            brn[i] = brn[i-1] + 1
        if s == "G":
            rn[i] = rn[i-1]
            gn[i] = gn[i-1] + 1
            bn[i] = bn[i-1]
            rgn[i] = rgn[i-1] + 1
            gbn[i] = gbn[i-1] + 1    
            brn[i] = brn[i-1]       
        if s == "B":
            rn[i] = rn[i-1]
            gn[i] = gn[i-1]
            bn[i] = bn[i-1] + 1
            rgn[i] = rgn[i-1]
            gbn[i] = gbn[i-1] + 1
            brn[i] = brn[i-1] + 1
ans = 0
for i, s in enumerate(S):
    if i == 0 or i == N-1:
        continue
    if s == "R":
        ans += gn[i-1] * (bn[-1] - bn[i])
        ans += bn[i-1] * (gn[-1] - gn[i])
    if s == "G":
        ans += rn[i-1] * (bn[-1] - bn[i])
        ans += bn[i-1] * (rn[-1] - rn[i])
    if s == "B":
        ans += rn[i-1] * (gn[-1] - gn[i])
        ans += gn[i-1] * (rn[-1] - rn[i])
    for j in range(min(i, N - i - 1)):
        if S[i-j-1] != S[i+j+1] and S[i-j-1] != s and S[i+j+1] != s:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=list(str(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=G*B*R
for a in range(N-1):
	for d in range(1,(N-a-1)//2+1):
		if S[a]!=S[a+d] and S[a+d]!=S[a+d*2] and S[a]!=S[a+d*2]:
			ans-=1
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]


N = int(input())
S = str(input())
dp = [[0 for j in range(3)] for i in range(N)]
ans = 0
temp = temp2 = temp3 = 0

RGB = [["R", "G"], ["R", "B"], ["B", "R"], ["B", "G"], ["G", "R"], ["G", "B"]]


for j in range(6):
  for i in range(N):
    if(S[i]== RGB[j][0]):
      temp += 1
    elif(S[i]==RGB[j][1]):
      temp2 += temp
    else:
      temp3 += temp2
  ans += temp3
  temp = temp2 = temp3 = 0

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


print(ans)

Submission status: Accepted

Solution code:

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

r = s.count("R")
g = s.count("G")
ans += r*g*(N-r-g)
print(ans)

Submission status: Accepted

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

kv = {'R':[],'G':[],'B':[]}
for i in range(N):
    kv[S[i]].append(i)

a = sorted([[len(kv['R']),'R'],[len(kv['G']),'G'],[len(kv['B']),'B']], key=lambda x:-x[0])
a1, a2, a3 = a[0][1],a[1][1],a[2][1]

ans = a[0][0]*a[1][0]*a[2][0]
set_a3 = set(kv[a3])
for one in kv[a1]:
    for t in kv[a2]:
        ans-=3-len(set([2*t-one,2*one-t,(t+one)/2])-set_a3)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
sum = 0
for i in range(N):
  if S[i] == "R":
    sum += S[i+1:].count("G")*S[i+1:].count("B")
  elif S[i] == "G":
    sum += S[i+1:].count("R")*S[i+1:].count("B")
  elif S[i] == "B":
    sum += S[i+1:].count("G")*S[i+1:].count("R")
for a in range(N):
  for b in range(a+1, N):
    if S[a] == S[b] or 2*b-a >= N:
      pass
    elif S[b] != S[2*b-a] and S[2*b-a] != S[a]:
      sum -= 1
print(sum)

Submission status: Accepted

Solution code:
n = int(input())
List = list(input())
count = List.count("R") * List.count("G") *List.count("B")
for w in range(1, n // 3 * 3 + 1):
    for s in range(n - 2 * w):
        if List[s] != List[s + w] and List[s] != List[s + w * 2] and List[s + w] != List[s + w * 2]:
            count -= 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r,g,b = 0,0,0
for i in S:
    if i == 'R':
        r+=1
    if i == 'G':
        g+=1       
    if i == 'B':
        b+=1 

count = 0
for i in range(1,N):
    for j in range(N):
        if j+i*2 >= N:
            break
        if S[j] != S[j+i] and S[j] != S[j+2*i] and S[j+i] != S[j+2*i]:
                count+=1
print(r*g*b-count)

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

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

    ar = []
    ab = []
    ag = []
    for i, si in enumerate(S):
        if si == "R":
            ar.append(i)
        elif si == "G":
            ag.append(i)
        else:
            ab.append(i)

    aa = [(ar, "R"), (ab, "B"), (ag, "G")]
    aa.sort(key=lambda x: len(x[0]))
    ans = 0
    ck = aa[2][1]
    ans = len(ar)*len(ab)*len(ag)
    for si in aa[0][0]:
        for sj in aa[1][0]:
            for k in [2*si-sj, 2*sj-si]:
                if 0 <= k < N and S[k] == ck:
                    ans -= 1
            k = si + sj
            if k & 1:
                continue
            k //=2
            if k < N and S[k] == ck:
                ans -= 1
    print(ans)





if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
moji = {'R', 'G', 'B'}

count = 0
for j in range(1, N-1):
    sj = S[j]
    diff_m = moji - {sj}
    rem_moji = list(diff_m)
    for mi, mj in zip(rem_moji, rem_moji[::-1]):
        ci = S.count(mi, 0, j)
        ck = S.count(mj, j+1, N)
        count += ci * ck

    l = min([j+1, N - j])
    for d in range(1, l):
        si = S[j-d]
        sk = S[j+d]
        if si != sj and sj != sk and sk != si:
            count -= 1

print(count)

Submission status: Accepted

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

ret = 0
for i in range(1,N):
    mid = S[i]
    rest = list(set(RGB)-set([mid]))
    left0, right0 = S[:i].count(rest[0]), S[i+1:].count(rest[0])
    left1, right1 = S[:i].count(rest[1]), S[i+1:].count(rest[1])
    tmp = left0*right1 + left1*right0
    for j in range(min(i,N-i-1)):
        ss = [S[i-j-1],S[i+j+1]]
        if ss == rest or ss == rest[::-1]:
            tmp -= 1

    ret += tmp
print(ret)

Submission status: Accepted

Solution code:
import bisect
import itertools

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

RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
RGB.sort(key=lambda x:len(x))
len_B=len(RGB[2])
max_B=max(RGB[2])
min_B=min(RGB[2])
#for r in RGB[0]:
#    for g in RGB[1]:
bool_B=[False for i in range(n)]
for i in RGB[2]:
    bool_B[i]=True

for rg in itertools.product(RGB[0],RGB[1]):
    r=rg[0]
    g=rg[1]
    dif = abs(r - g)

    up = max(r,g)+dif
    down = min(r,g)-dif

    if max_B >=up:
        if bool_B[up]:
            total-=1
    if min_B <= down and max_B >= down:
        if bool_B[down]:
            total-=1
    if dif % 2 == 0:
        center = (r+g)//2
        if min_B<=center and max_B >= center:
            if bool_B[center]:
                total-=1
print(total)

Submission status: Accepted

Solution code:
n=int(input())

s=input()

s=list(s)

Rcheck=[0]*n

Bcheck=[0]*n

Gcheck=[0]*n

for i in range(n):
    if s[i] == 'R':
        Rcheck[i] += 1
    if s[i] == 'B':
        Bcheck[i] += 1
    if s[i] == 'G':
        Gcheck[i] += 1

for i in range(1,n):
    Rcheck[i] += Rcheck[i-1]
    Bcheck[i] += Bcheck[i-1]
    Gcheck[i] += Gcheck[i-1]

ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        #print(ans)
        if s[i] == s[j]:
            continue
        memo = [s[i],s[j]]
        #print(memo)

        if 'R' not in memo:
            ans += Rcheck[-1] - Rcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'R':
                ans -= 1
        elif 'B' not in memo:
            ans += Bcheck[-1] - Bcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'B':
                ans -= 1
        elif 'G' not in memo:
            ans += Gcheck[-1] - Gcheck[j]
            if 2*(j+1)-(i+1)-1 < n and s[2*(j+1)-(i+1)-1] == 'G':
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
# (R, B)の間にあるGみたいな感じ 6種類あるのか？ 6*N^2
n = int(input())
s = input()
rs, gs, bs = [0] * n, [0] * n, [0] * n
if s[0] == "R":
    rs[0] = 1
elif s[0] == "G":
    gs[0] = 1
elif s[0] == "B":
    bs[0] = 1
for i in range(1, n):
    rs[i] = 1 + rs[i - 1] if s[i] == "R" else rs[i - 1]
    gs[i] = 1 + gs[i - 1] if s[i] == "G" else gs[i - 1]
    bs[i] = 1 + bs[i - 1] if s[i] == "B" else bs[i - 1]
ans = 0
for i in range(0, n):
    for j in range(i + 2, n):
        if (s[i] == "R" and s[j] == "B") or (s[i] == "B" and s[j] == "R"):
            ans += gs[j]
            ans -= gs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "G":
                ans -= 1
        elif (s[i] == "R" and s[j] == "G") or (s[i] == "G" and s[j] == "R"):
            ans += bs[j]
            ans -= bs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "B":
                ans -= 1
        elif (s[i] == "B" and s[j] == "G") or (s[i] == "G" and s[j] == "B"):
            ans += rs[j]
            ans -= rs[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == "R":
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bad = [0] * n
pair = 0
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair += 1
        if 2 * i - j >= 0:
            bad[2 * i - j] += 1
        if (j - i) % 2 == 0:
            bad[(i + j) // 2] += 1
        if 2 * j - i < n:
            bad[2 * j - i] += 1
if s[-1] == "B":
    b_num.append(n - 1)

for k in b_num:
    ans += pair - bad[k]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = []
g = []
b = []
s = input()
blen = 0
glen = 0
rlen = 0
for i in range(n):
    if(s[i]=='R'):
        r.append(i)
        rlen += 1
    elif(s[i]=='G'):
        g.append(i)
        glen += 1
    else:
        b.append(i)
        blen += 1

ans = 0
minus = 0
t = 0
for i in r:
    for j in g:
        mn = min(i,j)
        mx = max(i,j)

        mid2 = mx + (mx-mn)
        mid3 = mn - (mx-mn)

        if((mx-mn)%2==0):
            mid1 = mn + (mx-mn)//2
            if(mid1 >= 0 and mid1 < n):
                if(s[mid1]=='B'):
                    minus += 1
        if(mid2 >= 0 and mid2 < n):
            if(s[mid2] == 'B'):
                minus += 1
                #print("2",i,j,mid2,s[i],s[j],s[mid2])

                
        if(mid3 >= 0 and mid3 < n):
            if(s[mid3]=='B'):
                minus += 1
                #print("3",i,j,mid3,s[i],s[j],s[mid3])

        t += blen
                

ans = rlen * glen * blen
ans -= minus
print(ans)

Submission status: Accepted

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

r,g,b,rg,gb,br,rgb = 0,0,0,0,0,0,0,

for c in S:
    if c == 'R':
        r += 1
        rg += g
        br += b
        rgb += gb
    elif c == 'G':
        g += 1
        rg += r
        gb += b
        rgb += br
    else:
        b += 1
        gb += g
        br += r
        rgb += rg


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

print(rgb-cnt)

Submission status: Accepted

Solution code:
import itertools
import bisect
n = int(input())
s = input()
listR = []
listG = []
listB = []
counter = 0
for i in range(len(s)):
    if s[i] == "R":
        listR.append(i)
    elif s[i] == "G":
        listG.append(i)
    elif s[i] == "B":
        listB.append(i)

listB = sorted(listB)
def binary_search_loop(data, target):
    imin = 0
    imax = len(data)-1
    while imin <= imax:
        imid = imin + (imax - imin) // 2
        if target == data[imid]:
            return True
        elif target < data[imid]:
            imax = imid -1
        else:
            imin = imid + 1
    return False
for bits in itertools.product(listR, listG):
    diff = max(bits) - min(bits)
    remover = 0
    if sum(bits) % 2 == 0:
        mid = int(sum(bits) / 2)
    else:
        mid = 1000000
    if binary_search_loop(listB, max(bits) + diff):
        remover += 1
    if binary_search_loop(listB, min(bits) - diff):
        remover += 1
    if binary_search_loop(listB, mid):
        remover += 1
    counter += (len(listB) - remover)
print(counter)

Submission status: Accepted

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

x = S.count("R")
y = S.count("G")

total = x*y*(N-x-y)

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

print(total - count)

Submission status: Accepted

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


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

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

    temp = r * g * b
    minus = 0
    for i in range(1, n):
        for j in range(1, min(i + 1, abs(n - i))):
            if len(set([s[i], s[i - j], s[i + j]])) == 3:
                minus += 1

    return temp - minus


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import math

def solve():
    N = int(input())
    S = input()
    R = []
    G = []
    B = []

    # color separate
    for i in range(len(S)):
        if S[i] == 'R':
            R.append(i)
        elif S[i] == 'G':
            G.append(i)
        elif S[i] == 'B':
            B.append(i)

    # after color map
    lR = len(R)
    lG = len(G)
    lB = len(B)
    RGBmap = []
    for i in range(len(S)):
        if S[i] == 'R':
            lR -= 1
        elif S[i] == 'G':
            lG -= 1
        elif S[i] == 'B':
            lB -= 1
        RGBmap.append([lR, lG, lB])

    # calculate
    sum = 0
    for i in range(len(S)):
        if S[i] == 'R':
            a1 = G
            a2 = B
            b1 = 2
            b2 = 1
            c1 = 'B'
            c2 = 'G'
        elif S[i] == 'G':
            a1 = R
            a2 = B
            b1 = 2
            b2 = 0
            c1 = 'B'
            c2 = 'R'
        else:
            a1 = G
            a2 = R
            b1 = 0
            b2 = 1
            c1 = 'R'
            c2 = 'G'
        for a in a1:
            if i < a:
                sumb = RGBmap[a][b1]
                if sumb == 0:
                    continue
                if a + (a - i) < len(S) and S[a + (a - i)] == c1:
                    sumb -= 1
                sum += sumb
        for a in a2:
            if i < a:
                sumb = RGBmap[a][b2]
                if sumb == 0:
                    continue
                if a + (a - i) < len(S) and S[a + (a - i)] == c2:
                    sumb -= 1
                sum += sumb
    print(sum)
 

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

ans = 0

for i in range(1,N-1):
    if S[i] == 'R':
        leftG = S[:i].count('G')
        rightG = S[i+1:].count('G')
        leftB = S[:i].count('B')
        rightB = S[i+1:].count('B')

        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'G' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'G'):
                ans -= 1

for i in range(1,N-1):
    if S[i] == 'G':
        leftG = S[:i].count('R')
        rightG = S[i+1:].count('R')
        leftB = S[:i].count('B')
        rightB = S[i+1:].count('B')
            
        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
                ans -= 1

                
for i in range(1,N-1):
    if S[i] == 'B':
        leftG = S[:i].count('R')
        rightG = S[i+1:].count('R')
        leftB = S[:i].count('G')
        rightB = S[i+1:].count('G')
            
        ans += leftB*rightG+leftG*rightB
        for j in range(1,min(i,N-i-1)+1):
            if (S[i-j] == 'R' and S[i+j] == 'G') or (S[i-j] == 'G' and S[i+j] == 'R'):
                ans -= 1
            
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(s)
ans = c["R"]*c["G"]*c["B"]

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

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
S = str(input())
r=0
g=0
b=0
error=0
n=0

if N%2==0:
    n=(N//2)-1
else:
    n=N//2

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

for p in range(n+1):
    for i in range(N-(p*2)):
        if S[i]==S[i+p] or S[i+p]==S[i+2*p] or S[i]==S[i+2*p]:
            continue
        else:
            error+=1
d=r*g*b

print(d-error)

Submission status: Accepted

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

count = 0
r=0
g=0
b=0
for i in range(0,n):
  if s[i] == 'R':
    r = r + 1
  if s[i]=='G':
    g = g+1
  if s[i]=='B':
    b = b+1
    
for i in range(0,n):
  ii = s[i]
  for j in range(i+1, n):
    jj = s[j]
    if ii==jj:
      continue
    if (2*j-i)<=(n-1):
      if ii!=s[2*j-i] and jj!=s[2*j-i]:
        count = count + 1
print(r*g*b-count)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

N = I()
S = input()

R = [0] * N
G = [0] * N
B = [0] * N

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

    if S[N-i-1] == "B":
        b += 1
        B[N-i-1] = b
    else:
        B[N-i-1] = b

ans = 0
for i, s in enumerate(S):
    if i > N-3:
        break

    if s == "R":
        ans += G[i+1] * B[i+1]
    if s == "G":
        ans += R[i+1] * B[i+1]
    if s == "B":
        ans += R[i+1] * G[i+1]
    
    j = 1
    while i + j*2 < N:
        if s == "R":
            if (S[i+j] == "G" and S[i+j+j] == "B") or (S[i+j] == "B" and S[i+j+j] == "G"):
                ans -= 1
        if s == "G":
            if (S[i+j] == "R" and S[i+j+j] == "B") or (S[i+j] == "B" and S[i+j+j] == "R"):
                ans -= 1
        if s == "B":
            if (S[i+j] == "G" and S[i+j+j] == "R") or (S[i+j] == "R" and S[i+j+j] == "G"):
                ans -= 1
        
        j += 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
from itertools import permutations


def main():
    n = int(input())
    s = input()
    ind = [[] for _ in range(3)]
    for i in range(n):
        if s[i] == "R":
            ind[0].append(i)
        elif s[i] == "G":
            ind[1].append(i)
        else:
            ind[2].append(i)
    length = [len(ind[i]) for i in range(3)]
    ind_set = [set(ind[i]) for i in range(3)]
    answer = 0
    for p1, p2, p3 in permutations([0, 1, 2]):
        for j in ind[p2]:
            i = bisect_right(ind[p1], j)
            k = bisect_left(ind[p3], j)
            if length[p1] < i or length[p3] <= k:
                continue
            answer += i * (length[p3] - k)
            for ii in ind[p1]:
                target = 2 * j - ii
                t_ind = bisect_left(ind[p3], target)
                if target in ind_set[p3] and k <= t_ind < length[p3]:
                    answer -= 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
n = int(input())
# = map(int, input().split())
s = input()
# = list(map(int, input().split()))

rgb = [0, 0, 0]
for i in range(n):
    if s[i] == 'R':
        rgb[0] += 1
    elif s[i] == 'G':
        rgb[1] += 1
    else:
        rgb[2] += 1

le = []
ri = []
past = [0, 0, 0]
unt = copy.copy(rgb)

for i in range(n):
    p = copy.copy(past)
    u = copy.copy(unt)
    if s[i] == 'R':
        unt[0] -= 1
        u[0] -= 1
        le.append(p)
        ri.append(u)
        past[0] += 1
    elif s[i] == 'G':
        unt[1] -= 1
        u[1] -= 1
        le.append(p)
        ri.append(u)
        past[1] += 1
    else:
        unt[2] -= 1
        u[2] -= 1
        le.append(p)
        ri.append(u)
        past[2] += 1
ans = 0
for i in range(n):
    if s[i] == 'R':
        ans += le[i][1]*ri[i][2] + le[i][2]*ri[i][1]
    elif s[i] == 'G':
        ans += le[i][0]*ri[i][2] + le[i][2]*ri[i][0]
    else:
        ans += le[i][0]*ri[i][1] + le[i][1]*ri[i][0]

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(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)
base = len(R) * len(G) * len(B)
B = set(B)
no_num = 0
for r in R:
    for g in G:
        y = max(r,g)
        x = min(r,g)
        tmp = int((x+y)/2)
        if (x+y)%2==0 and (tmp in B):
            no_num += 1
        tmp = 2*y-x
        if tmp in B:
            no_num += 1
        tmp = 2*x-y
        if tmp in B:
            no_num += 1
print(base-no_num)

Submission status: Accepted

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

    c = 0

    r_indices = tuple([i for i in range(N) if S[i] == 'R'])
    g_indices = tuple([i for i in range(N) if S[i] == 'G'])
    b_indices = tuple([i for i in range(N) if S[i] == 'B'])

    # print(r_indices)
    # print(g_indices)
    # print(b_indices)

    avoid = 0

    for center in range(1, N - 1):
        # print('###')
        # print('center = ', center)
        # print(min(center, N - center - 1))

        for width in range(1, min(center + 1, N - center)):
            # print('c, w = ', center, width)
            little = center - width
            great = center + width
            # print('little=', little)
            # print('center=', center)
            # print('great=', great)

            if S[little] != S[great] and S[center] != S[great] and S[little] != S[center]:
                avoid += 1

    total = len(r_indices) * len(g_indices) * len(b_indices)

    print(total - avoid)


if __name__ == '__main__':
    main()