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

sameRangeRGBs = [0] * N
for itvl in range(1, (N-1)//2 + 1):
    for i in range(itvl, N - itvl):
        l = S[i-itvl]
        m = S[i]
        r = S[i+itvl]
        if l == m or m == r or r == l:
            continue
        sameRangeRGBs[i] += 1

accRL = [0]
accGL = [0]
accBL = [0]
numR = 0
numG = 0
numB = 0
for c in S:
    if c == 'R':
        numR += 1
    elif c == 'G':
        numG += 1
    elif c == 'B':
        numB += 1
    accRL.append(numR)
    accGL.append(numG)
    accBL.append(numB)

accRR = [0]
accGR = [0]
accBR = [0]
numR = 0
numG = 0
numB = 0
for c in S[::-1]:
    if c == 'R':
        numR += 1
    elif c == 'G':
        numG += 1
    elif c == 'B':
        numB += 1
    accRR.append(numR)
    accGR.append(numG)
    accBR.append(numB)

ans = 0
for i in range(N):
    c = S[i]
    if c == 'R':
        ans += accGL[i] * accBR[N-1-i] + accBL[i] * accGR[N-1-i] - sameRangeRGBs[i]
    elif c == 'G':
        ans += accRL[i] * accBR[N-1-i] + accBL[i] * accRR[N-1-i] - sameRangeRGBs[i]
    elif c == 'B':
        ans += accGL[i] * accRR[N-1-i] + accRL[i] * accGR[N-1-i] - sameRangeRGBs[i]
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
S = input().replace("R", "1").replace("G", "2").replace("B", "3")
RGB = [[0]*n for _ in range(4)]
for i, s in enumerate(S):
    RGB[int(s)][i] = 1
for i in range(4):
    RGB[i] = list(accumulate(RGB[i]))
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        t = int(S[i]) ^ int(S[j])
        ans += RGB[t][-1] - RGB[t][j]
        if 2*j-i < n and int(S[2*j-i]) == t:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main(N=None, S=None):
    if not N or not S:
        N = int(input())
        S = input()
    c = Counter(S)
    ans = c["R"]*c["G"]*c["B"]
    for hop in range(1, N):
        for start in range(N):
            i = start
            j = start + hop
            k = start + hop*2
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1
            # print(i, j, k, ans)
    print(ans)
    return ans


if __name__ == "__main__":
    # assert main(4, "RRGB") == 1
    # assert main(39, "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB") == 1800
    main()

Submission status: Accepted

Solution code:
import bisect

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

r = []
g = []
b = []
for i, s in enumerate(S):
    if s == 'R':
        r.append(i)
    if s == 'G':
        g.append(i)
    if s == 'B':
        b.append(i)

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

blen = len(b)
for rr in r:
    for gg in g:
        nb = (rr + gg) / 2
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

        nb = 2 * rr - gg
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

        nb = 2 * gg - rr
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

print(count)

Submission status: Accepted

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

N = int(input())
S = list(input())
R = []
G = []
B = set()
for i in range(N):
    if S[i] =="R":
        R.append(i)
    if S[i] =="G":
        G.append(i)
    if S[i] =="B":
        B.add(i)
M =len(B)
cnt = 0
for r in R:
    for g in G:
        a = (r+g)/2
        b = 2*g-r
        c = 2*r-g
        if a in B:
            cnt-=1
        if b in B:
            cnt-=1
        if c in B:
            cnt-=1
        cnt += M
        
print(cnt)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = input()
c = collections.Counter([s for s in S])
RGB = c.most_common()
if len(RGB) != 3:
  result = 0
else:
  rgb = set(["R", "G", "B"])
  result = RGB[0][1] * RGB[1][1] * RGB[2][1]
  for i in range(1, N//2+1):
    for j in range(N-i*2):
      if set([S[j], S[j+i], S[j+i*2]]) == rgb:
        result -= 1
print(result)

Submission status: Accepted

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

for i in range(1, 2000):
  for j in range(1, N - 2 * i + 1):
    if S[j - 1] != S[j + i - 1]:
      if S[j + 2 * i - 1] != S[j + i - 1]:
        if S[j - 1] != S[j + 2 * i - 1]:
          ans -= 1
            
print(max(ans, 0))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 20:58:46 2020

@author: Kanaru Sato
"""

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

rdic = {}
gdic = {}
bdic = {}

for i in range(len(s)):
    if s[i] == "R":
        if i not in rdic:
            rdic[i] = 1
    elif s[i] == "G":
        if i not in gdic:
            gdic[i] = 1
    elif s[i] == "B":
        if i not in bdic:
            bdic[i] = 1

bnum = len(bdic)            
count = 0
for rplace in rdic:
    for gplace in gdic:
        if rplace >= gplace:
            ex1 = gplace - (rplace- gplace)
            ex2 = rplace + (rplace- gplace)
            ex3 = (rplace + gplace)/2
            e1 = 1 if ex1 in bdic else 0
            e2 = 1 if ex2 in bdic else 0
            e3 = 1 if ex3 in bdic else 0
            count += bnum - (e1 + e2 + e3)
        elif rplace < gplace:
            ex1 = rplace - (gplace- rplace)
            ex2 = gplace + (gplace- rplace)
            ex3 = (rplace + gplace)/2
            e1 = 1 if ex1 in bdic else 0
            e2 = 1 if ex2 in bdic else 0
            e3 = 1 if ex3 in bdic else 0
            count += bnum - (e1 + e2 + e3)

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
if n < 3:
    print(0)
    exit()
elif len(c.keys()) < 3:
    print(0)
    exit()
ans = 0
for i in range(n):
    x = s[i]
    ans += mul(*[v for k, v in c.items()if k != x])
    c[x] -= 1
    for j in range(i+1, n):
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
slen = len(s)
d = Counter(s)
res = d['R']*d['G']*d['B']
#print(res)
for start in range(slen):
    for step in range(1, slen):
        i = start
        j = i + step
        k = j + step
        if k >= slen:
            break
        ijk = [s[i], s[j], s[k]]
        # print("ijk is", ijk)
        if 'R' in ijk and 'G' in ijk and 'B' in ijk:
            res -= 1

print(res)

Submission status: Accepted

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

def count(index,Set):
    if index in Set:
        return 1
    else:
        return 0

L=[[],[],set()]
for x in range(N):
    if S[x]=='R':
        L[0].append(x)
    elif S[x]=='G':
        L[1].append(x)
    else:
        L[2].add(x)

number=len(L[2])

ans=0
for i in L[0]:
    for j in L[1]:
        if (i+j)%2==0:
            m=(i+j)//2
            res1=number-count(m,L[2])-count(2*i-j,L[2])-count(2*j-i,L[2])
            ans+=max(0,res1)
        else:
            res2=number-count(2*i-j,L[2])-count(2*j-i,L[2])
            ans+=max(0,res2)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = []
G = []
B = []

def check(startI, list1, list2, char1, char2):
  num1, num2 = 0, 0
  sub_ = 0
  for num in list1:
    if num > startI:
      if num + (num-startI) < N and S[num+(num-startI)] == char2:
        sub_ += 1
      num1 += 1
  for num in list2:
    if num > startI:
      if num + (num-startI) < N and S[num+(num-startI)] == char1:
        sub_ += 1
      num2 += 1
  return num1 * num2 - sub_


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)

sum_ = 0
for i in range(N):
  if S[i] == 'R':
    sum_ += check(i, G, B, 'G', 'B')
  elif S[i] == 'G':
    sum_ += check(i, R, B, 'R', 'B')
  elif S[i] == 'B':
    sum_ += check(i, R, G, 'R', 'G')

print(sum_)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
s = input()

#indexより右のr,g,bの数を持つ配列を作る
r_list = [0] * n
g_list = [0] * n
b_list = [0] * n
s = s[::-1]
for i,v in enumerate(s):
    if v == "R":
        r_list[i]=1
    elif v=="G":
        g_list[i]=1
    else:
        b_list[i]=1




from itertools import accumulate
#[1,3,6,10,15]
r_list = list(accumulate(r_list))
b_list = list(accumulate(b_list))
g_list = list(accumulate(g_list))

r_list.reverse()
g_list.reverse()
b_list.reverse()
s = s[::-1]

ret = 0
for i,v in enumerate(s[0:n-1]):
    if v=="R":
        ret+=(g_list[i+1] * b_list[i+1])
    elif v=="G":
        ret+=(r_list[i+1] * b_list[i+1])
    else:
        ret+=(r_list[i+1] * g_list[i+1])
    step = 0
    while True:
        step+=1
        if i+step*2 >= n:
            break
        if v!=s[i+step] and v!= s[i+step*2] and s[i+step]!=s[i+step*2]:
            ret-=1

print(ret)

Submission status: Accepted

Solution code:
#[int(i) for i in input().split()]
n = int(input())
s = input()
ls_r = []
ls_g = []
ls_b = []
len_r = 0
len_g = 0
len_b = 0

if n <=3:
    print(0)
else:
    ans = 0
    for i in range(n):
        if s[i] == "R":
            ls_r.append(i)
            len_r += 1
        elif s[i] == "G":
            ls_g.append(i)
            len_g += 1
        else:
            ls_b.append(i)
            len_b += 1
    for b in ls_b:
        for g in ls_g:
            tmp = 0
            if b < g:
                if 2 * b - g >= 0:
                    if s[2*b-g] == "R":
                        tmp -= 1
                if 2 * g - b <= n-1:
                    if s[2*g-b] == "R":
                        tmp -= 1
                if (g - b) % 2 == 0:
                    if s[(g+b)//2] == "R":
                        tmp -= 1
                ans += tmp+len_r
            else:
                if 2 * g - b >= 0:
                    if s[2*g-b] == "R":
                        tmp -= 1
                if 2 * b - g <= n-1:
                    if s[2*b-g] == "R":
                        tmp -= 1
                if (b - g) % 2 == 0:
                    if s[(g+b)//2] == "R":
                        tmp -= 1
                ans += tmp+len_r
    print(ans)

Submission status: Accepted

Solution code:
def rgb(s):
    if s=="R":return 0
    if s=="G":return 1
    if s=="B":return 2
def rgb2(s,t):
    if (s=="R" and t =="G" ) or (t=="R" and s =="G" ):
        return 2
    elif (s=="R" and t =="B" ) or (s=="B" and t =="R" ):
        return 1
    else:return 0


n = int(input())
s = input()
co = [[0]*3 for i in range(n+1)]
for i in range(1,n+1):
    for j in range(3):
        co[i][j] = co[i-1][j]
    co[i][rgb(s[i-1])] = co[i-1][rgb(s[i-1])] + 1
ans = 0
for i in range(n):
    w1 = s[i]
    for j in range(i+1,n):
        w2 = s[j]
        d = j-i
        if w1!=w2:
            ans += co[-1][rgb2(w1,w2)] - co[j][rgb2(w1,w2)]
            if j+d <= n-1:
                if rgb(s[j+d]) == rgb2(w1,w2):
                    ans-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = [0,0,0]

for i in range(n):
    if(s[i] == 'R'):
        l[0] += 1
    elif(s[i] == 'G'):
        l[1] += 1
    else:
        l[2] += 1
p = l[0]*l[1]*l[2]
q = 0
for i in range(1,(n+1)//2):
    for j in range(n-i*2):
        if(s[j] != s[j+i] and s[j+i] != s[j+2*i] and s[j+i*2] != s[j]):
            q += 1
print(p-q)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = str(input())
rn = collections.Counter(s)['R']
gn = collections.Counter(s)['G']
bn = collections.Counter(s)['B']
ans = rn*gn*bn
if ans == 0:
     print(0)
elif n==3 and ans ==1:
     print(0)
elif n%2==0:
     for i in range(1,n//2):
          for j in range(n-2*i):
               if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
                    ans-=1
     print(ans)
else:
     for i in range(1,n//2+1):
          for j in range(n-2*i):
               if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
                    ans-=1
     print(ans)

Submission status: Accepted

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

right=[[0]*(N+1) for _ in range(3)]
dic={'R':0,'G':1,'B':2}

for i in range(N,0,-1):
    for j in range(3):
        right[j][i-1]=right[j][i]

    c=S[i-1]
    right[dic[c]][i-1]+=1

cnt=0

for i in range(N-2):
    for j in range(i,N-1):
        a=S[i]
        b=S[j]
        if a==b:
            continue
        k=0
        while dic[a]==k or dic[b]==k:
            k+=1
        
        if 2*j-i<N and dic[S[2*j-i]]==k:
            cnt+=right[k][j+1]-1
        else:
            cnt+=right[k][j+1]
            

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
k = [0] * N
for i in range(N):
    if S[i] == "R":
        k[i] = 2
    elif S[i] == "G":
        k[i] = 3
    else:
        k[i] = 5
a = k.count(2)
b = k.count(3)
c = N - a - b
ans = a*b*c
for i in range (1,(N + 1)//2):
    for j in range (N - 2 * i):
        if k[j] * k[j + i] * k[j + 2 * i] == 30:
            ans = ans - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right as bi_r, bisect_left as bi_l

n, s = sys.stdin.read().split()
n = int(n)
s = s.replace('R', '0')
s = s.replace('G', '1')
s = s.replace('B', '2')
s = [int(x) for x in s]

def main():
    res = [[] for _ in range(3)]
    for i in range(n):
        res[s[i]].append(i)
    tot = 0
    for j in range(1, n - 1):
        cnt = 0
        m = s[j]
        if m == 0:
            lr = [(1, 2), (2, 1)]
        elif m == 1:
            lr = [(0, 2), (2, 0)]
        else:
            lr = [(0, 1), (1, 0)]
        for l, r in lr:
            cnt += bi_r(res[l], j) * (len(res[r]) - bi_l(res[r], j))
        i = j - 1
        k = j + 1
        while True:
            if i < 0 or k >= n:
                break
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1
            i -= 1
            k += 1
        tot += cnt
    print(tot)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
import sys
n=int(input())
if n<=2:
  print(0)
  sys.exit()
s=input()
S=list(s)
t=collections.Counter(S)
R=t['R']
G=t['G']
B=t['B']
num=R*G*B
cnt=0
for i in range(1,(n//2)+1):
  for j in range(n):
    if j+(2*i)>n-1:
      continue
    if s[j]=='R':
      if s[j+i]=='G':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='G':
          cnt+=1
    if s[j]=='G':
      if s[j+i]=='R':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='R':
          cnt+=1
    if s[j]=='B':
      if s[j+i]=='G':
        if s[j+(2*i)]=='R':
          cnt+=1
      if s[j+i]=='R':
        if s[j+(2*i)]=='G':
          cnt+=1
print(num-cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = 0
r = [0 for i in range(n)]
g = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
    if s[i] == "R":
        r[i] += 1
    elif s[i] == "G":
        g[i] += 1
    else:
        b[i] += 1
    if i > 0:
        r[i] += r[i-1]
        g[i] += g[i-1]
        b[i] += b[i-1]
ju = [0,0,0]
ju2 = ["R","G","B"]
for i in range(n-2):
    for j in range(3):
        if s[i] == ju2[j]:
            ju[j] += 1
    for j in range(i+1,n-1):
        #print(ans)
        for k in range(3):
            if s[j] == ju2[k]:
                ju[k] += 1
                key = k
        if ju[key] == 1:
            if ju[0] == 0:
                ans += r[n-1] - r[j]
                if j + (j-i) < n:
                    if r[j+(j-i)] > r[j+(j-i)-1]:
                        ans -= 1
            
            elif ju[1] == 0:
                ans += g[n-1] - g[j]
                if j + (j-i) < n:
                    if g[j+(j-i)] > g[j+(j-i)-1]:
                        ans -= 1
            else:
                ans += b[n-1] - b[j]
                if j + (j-i) < n:
                    if b[j+(j-i)] > b[j+(j-i)-1]:
                        ans -= 1
        ju[key] -= 1
    ju = [0,0,0]
print(ans)

Submission status: Accepted

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

d = {
    'R': [0],
    'G': [0],
    'B': [0]
}

for i in reversed(range(n)):
    for c in 'RGB':
        if s[i] == c:
            d[c].append(d[c][-1]+1)
        else:
            d[c].append(d[c][-1])
ans = 0

for c1, c2, c3 in itertools.permutations(['R', 'G', 'B'], 3):
    for i in range(n):
        if s[i] == c1:
            for j in range(i+1, n-1):
                if s[j] == c2:
                    ans += d[c3][-j-1]
                    l = 2 * j -i
                    if l < n and s[l] == c3:
                        ans -= 1
    
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    R = set()
    G = set()
    B = set()
    for i in range(N):
        if S[i] == 'R':
            R.add(i)
        elif S[i] == 'G':
            G.add(i)
        else:
            B.add(i)
    
    d = 0
    for r in R:
        for g in G:
            x = min(r, g)
            y = max(r, g)
            if (2 * y - x) in B:
                d += 1
            if (2 * x - y) >= 0 and (2 * x - y) in B:
                d += 1
            if (y + x) / 2 == int((y + x) / 2) and (y + x) / 2 in B:
                d += 1
    print(len(R) * len(G) * len(B) - d)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = list(input())
l = [0]*3
for i in s:
	if i == "R":
		l[0]+=1
	elif i == "G":
		l[1]+=1
	else:
		l[2]+=1
su = l[0]*l[1]*l[2]
for i in range(1,n+1):
	for j in range(i+2,n+1,2):
		kk = (i+j)//2
		if s[i-1]!=s[j-1] and s[i-1]!=s[kk-1] and s[j-1]!=s[kk-1]:
			su-=1
print(su)

Submission status: Accepted

Solution code:
def main():
    input()
    r, g, b = set(), set(), set()
    for i, s in enumerate(input()):
        if s == "R":
            r.add(i + 1)
        if s == "G":
            g.add(i + 1)
        if s == "B":
            b.add(i + 1)
    c = len(r) * len(g) * len(b)
    for i in r:
        for j in g:
            k = abs(i - j)
            if i + k in b:
                c -= 1
            if i - k in b:
                c -= 1
            if j + k in b:
                c -= 1
            if j - k in b:
                c -= 1
            if (i + j) / 2 in b:
                c -= 1
    print(c)


if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    S = input()

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

    except_cnt = 0
    for w in range(1, N//2 + 1):
        for i in range(N):
            j = i + w
            k = j + w
            if i in R and j in G and k in B:
                except_cnt += 1
            if i in R and j in B and k in G:
                except_cnt += 1
            if i in B and j in R and k in G:
                except_cnt += 1
            if i in B and j in G and k in R:
                except_cnt += 1
            if i in G and j in B and k in R:
                except_cnt += 1
            if i in G and j in R and k in B:
                except_cnt += 1

    print(len(R) * len(G) * len(B) - except_cnt)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    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)

    n_R = len(R)
    n_G = len(G)
    n_B = len(B)
    ans = n_R * n_G * n_B
    if ans == 0:
        print(ans)
        exit()

    for r in R:
        for g in G:
            rg = sorted([r, g])
            dist = rg[1] - rg[0]
            if 0 <= rg[0] - dist:
                if S[rg[0] - dist] == "B":
                    ans -= 1
            if rg[1] + dist <= N - 1:
                if S[rg[1] + dist] == "B":
                    ans -= 1
            if dist % 2 == 0:
                if S[rg[0] + (dist // 2)] == "B":
                    ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


count = 0
r_count = []
for i in range(N):
    if S[i] == "R":
        count += 1
    r_count.append(count)

count = 0
g_count = []
for i in range(N):
    if S[i] == "G":
        count += 1
    g_count.append(count)

count = 0
b_count = []
for i in range(N):
    if S[i] == "B":
        count += 1
    b_count.append(count)

ans = 0
for i in range(N):
    if S[i] == "R":
        ans += g_count[i] * (b_count[-1] - b_count[i]) + b_count[i] * (
            g_count[-1] - g_count[i]
        )
    elif S[i] == "G":
        ans += r_count[i] * (b_count[-1] - b_count[i]) + b_count[i] * (
            r_count[-1] - r_count[i]
        )
    else:
        ans += r_count[i] * (g_count[-1] - g_count[i]) + g_count[i] * (
            r_count[-1] - r_count[i]
        )

rgbs = set(("RGB", "RBG", "BRG", "BGR", "GRB", "GBR"))

for i in range(N):
    for j in range(i + 2, N, 2):
        s = S[i : j + 1 : (j - i) // 2]
        if s in rgbs:
            ans -= 1

print(ans)

Submission status: Accepted

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

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

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

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
	elif S[i]=="G":
		g+=1
	else:
		b+=1
w=0
for i in range(N-2):
	for j in range(1,int((N-i+1)/2)):
		if S[i]!=S[i+j] and S[i+j]!=S[i+j+j] and S[i]!=S[i+j+j]:
			w+=1
print((r*g*b)-w)

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import permutations

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

pos = defaultdict(lambda: [])
for i, s in enumerate(S) :
  pos[s].append(i)

ret = 0
for a, b, c in (permutations('RGB')) :
  for center in pos[b] :
    cnt = [0] * (N + 1)
    left, right = 0, 0
    for l in pos[a] :
      if l >= center :
        break
      cnt[center-l] += 1
      left += 1
    for r in pos[c][::-1] :
      if r <= center :
        break
      cnt[r-center] += 1
      right += 1
    ret += left * right - cnt.count(2)
     

  
print(ret)

Submission status: Accepted

Solution code:
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

from itertools import accumulate

R = [0] + list(accumulate(R))
G = [0] +list(accumulate(G))
B = [0] + list(accumulate(B))

ans = 0
for i in range(N):
    for j in range(i + 1, N - 1):
        if S[i] == S[j]: continue
        tmp = set(["R","G","B"])
        tmp.remove(S[i])
        tmp.remove(S[j])
        Sk = tmp.pop()
        no = j + (j - i)
        if Sk == "R":
            ans += R[min(N, no)] - R[j] + R[N] - R[min(N, no + 1)]
        elif Sk == "G":
            ans += G[min(N, no)] - G[j] + G[N] - G[min(N, no + 1)]
        else:
            ans += B[min(N, no)] - B[j] + B[N] - B[min(N, no + 1)]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, b, g = 0, 0, 0
for c in s:
  if c == 'R':
    r += 1
  elif c == 'G':
    b += 1
  elif c == 'B':
    g += 1
  else:
    raise ValueError
count = r * b * g
for i in range(n-2):
  s_i = s[i]
  for j in range(i+1, n-1):
    k = j + j - i
    if k < n and s_i != s[j] and s_i != s[k] and s[j] != s[k]:
      count -= 1
print(count)

Submission status: Accepted

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

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

for i, s in enumerate(S):
    if s == 'R':
        r.append(i)
    elif s == 'G':
        g.append(i)
    else:
        b.append(i)
        
b = set(b)

ans = 0        

for ri in r:
    for gi in g:
        add = len(b)
        if ri - abs(gi-ri) in b:
            add -= 1
        if ri + abs(gi-ri) in b:
            add -= 1
        if gi - abs(gi-ri) in b:
            add -= 1
        if gi + abs(gi-ri) in b:
            add -= 1
        if abs(gi-ri) % 2 == 0:
            if min(ri,gi) + abs(gi-ri)//2 in b:
                add -= 1
        
        ans += max(0, add)
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
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)
ans = len(r) * len(g) * len(b)
for i in range(1, N//2 +1):
  j = 0
  while(j + 2*i < N):
    A = [S[j], S[j+i], S[j+i+i]]
    if ('R' in A) and ('G' in A) and ('B' in A):
      ans -= 1
    j += 1
print(ans)

Submission status: Accepted

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

RD={}
GD={}
BD={}
RL=[]
GL=[]
BL=[]
for i in range(N):
    if S[i]=="R":
        RD[i]=1
        RL.append(i)
    elif S[i]=="G":
        GD[i]=1
        GL.append(i)
    else:
        BD[i]=1
        BL.append(i)

#print(RD,GD,BD)
#print(RL,GL,BL)
import bisect
#print(bisect.bisect_left(RL,i))
ans=0
for i in range(1,N-1):
    if S[i]=="R":
        cnt=0
        cnt+=bisect.bisect_left(GL,i)*(len(BL)- bisect.bisect_left(BL,i))
        cnt+=bisect.bisect_left(BL,i)*(len(GL)- bisect.bisect_left(GL,i))
        hiku=0
        for j in range(i):
            if len(GL)==j or GL[j]>i:
                break
            if 2*i-GL[j] in BD:
                hiku+=1
        for j in range(i):
            if len(BL)==j or BL[j]>i:
                break
            if 2*i-BL[j] in GD:
                hiku+=1
        ans+=(cnt-hiku)
    elif S[i]=="G":
        cnt=0
        cnt+=bisect.bisect_left(RL,i)*(len(BL)-bisect.bisect_left(BL,i))
        #print(bisect.bisect_left(RL,i),bisect.bisect_left(BL,i))
        cnt+=bisect.bisect_left(BL,i)*(len(RL)-bisect.bisect_left(RL,i))
        #print(cnt)
        hiku=0
        for j in range(i):
            if len(RL)==j or RL[j]>i:
                break
            if 2*i-RL[j] in BD:
                hiku+=1
        for j in range(i):
            if len(BL)==j or BL[j]>i:
                break
            if 2*i-BL[j] in RD:
                hiku+=1
        ans+=(cnt-hiku)
    else:
        cnt=0
        cnt+=bisect.bisect_left(RL,i)*(len(GL)- bisect.bisect_left(GL,i))
        cnt+=bisect.bisect_left(GL,i)*(len(RL)- bisect.bisect_left(RL,i))
        hiku=0
        for j in range(i):
            if len(RL)==j or RL[j]>i:
                break
            if 2*i-RL[j] in GD:
                hiku+=1
        for j in range(i):
            if len(GL)==j or GL[j]>i:
                break
            if 2*i-GL[j] in RD:
                hiku+=1
        ans+=(cnt-hiku)
    #print(ans)
print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = input()
rui_rl = [0] + list(itertools.accumulate([1 if x == 'R' else 0 for x in s]))
rui_gl = [0] + list(itertools.accumulate([1 if x == 'G' else 0 for x in s]))
rui_bl = [0] + list(itertools.accumulate([1 if x == 'B' else 0 for x in s]))
rui = {'R': rui_rl, 'G': rui_gl, 'B': rui_bl}
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue

        if 'R' not in [s[i], s[j]]:
            target = 'R'
        elif 'G' not in [s[i], s[j]]:
            target = 'G'
        else:
            target = 'B'

        dif_ji = j-i

        ans += rui[target][-1] - rui[target][j+1]
        if j+dif_ji <= n-1:
            if s[j+dif_ji] == target:
                ans -= 1
print(ans)

Submission status: Accepted

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

ans=0
for i in range(n-2):
    if (s[i]=="R"):
        numG=s[i+1:].count("G")
        numB=s[i+1:].count("B")
        ans+=numG*numB
        for j in range(i+1,(n+1+i)//2):
            #print(j,i,s[j],s[2*j-i])
            if (s[j]!=s[j+(j-i)] and s[j]!="R" and s[2*j-i]!="R"):
                ans-=1
                
    elif (s[i]=="G"):
        numG=s[i+1:].count("R")
        numB=s[i+1:].count("B")
        ans+=numG*numB
        for j in range(i+1,(n+1+i)//2):
            if (s[j]!=s[j+(j-i)] and s[j]!="G" and s[2*j-i]!="G"):
                ans-=1
        
    else:
        numG=s[i+1:].count("R")
        numB=s[i+1:].count("G")
        ans+=numG*numB
        for j in range(i+1,(n+1+i)//2):
            if (s[j]!=s[j+(j-i)] and s[j]!="B" and s[2*j-i]!="B"):
                ans-=1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
R = []
G = []
B = []
s =input()
ans = 0
for i in range(N):
    if s[i] == "R":
        R.append(i)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)
sB = set(B)
ans = len(R) * len(G) * len(B)
for r in R:
    for g in G:
        i = min(r,g)
        j = max(r,g)
        sa = j - i
        if i - sa in sB:
            ans -= 1
        if j + sa in sB:
            ans -= 1
        if (i + j) / 2 in sB:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import copy
n = int(input())
s = input()
total = 0
rgb = {'R', 'G', 'B'}
rgb_count = {key:s.count(key) for key in rgb}

for i in range(n):
    a = s[i]
    rgb_count[a] -= 1
    rgb_copy = copy.copy(rgb_count)
    for j in range(i+1, n):
        rgb_copy[s[j]] -= 1
        if s[j] != a:
            b = s[j]
            remain = rgb - {a, b}
            c = [i for i in remain][0]
            total += rgb_copy[c]
            if 2*j-i < n and s[2*j-i] == c:
                total -= 1

print(total)