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:
num = int(input())
s = list(input())

red_index = [i for i in range(1,len(s)+1) if s[i-1] == 'R']
gre_index = [i for i in range(1,len(s)+1) if s[i-1] == 'G']
blu_index = [i for i in range(1,len(s)+1) if s[i-1] == 'B']
ans = len(red_index) * len(gre_index) * len(blu_index)

delete = 0
for i in range(1,(num // 2) + 1):
  for st in range(num):
    if st+2*i <= num -1:
      check = [s[st], s[st + i], s[st + 2*i]]
      if len(set(check)) == 3:
        delete += 1
print(ans-delete)

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline())
S = list(input())
R = set()
G = set()
B = set()
for i, s in enumerate(S, 1):
    if s == 'R':
        R.add(i)
    elif s == 'G':
        G.add(i)
    else:
        B.add(i)

b = len(B)
ans = 0
for r in R:
    for g in G:
        M = max(r, g)
        m = min(r, g)
        if 2 * m - M in B:
            ans -= 1
        if 2 * M - m in B:
            ans -= 1
        if (M + m) / 2 in B:
            ans -= 1
        ans += b

print(ans)

Submission status: Accepted

Solution code:
# D - RGB Triplets

n = int(input())
s = input()
assert len(s) == n

# post['R'][i] 位置i以降の'R'の個数
post = {a: [0] * (n + 1) for a in 'RGB'}
for i in range(n - 1, -1, -1):
    for a in 'RGB':
        post[a][i] = post[a][i + 1]
    post[s[i]][i] = post[s[i]][i + 1] + 1

def remain(a, b):
    return (list(set('RGB') - {a, b}))[0]

total = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        r = remain(s[i], s[j])
        total += post[r][j + 1]
        if j - i + j < n and s[j - i + j] == r:
            total -= 1
        
print(total)

Submission status: Accepted

Solution code:
def CountComp(N,S):
    Ncomp = 0
    for i in range(N):
        for j in range(1,(N-i+1)//2):
            # print(i,i+j,i+2*j)
            if S[i]==S[i+j] or S[i]==S[i+2*j] or S[i+j]==S[i+2*j]:
                pass
            else:
                Ncomp+=1
    return Ncomp

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

# print(S.count('R'))
NR = S.count('R')
NG = S.count('G')
NB = S.count('B')
# print(NR,NG,NB,NR*NG*NB)
# print(CountComp(N,S))
Nmatch = NR*NG*NB-CountComp(N,S)
print(Nmatch)

Submission status: Accepted

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

rlis=[]
glis=[]
blis=[]
for i in range(len(s)):
    if s[i]=="R":
        rlis.append(i)
    if s[i]=="G":
        glis.append(i)
    if s[i]=="B":
        blis.append(i)
import bisect
ans=0

lis1=rlis
lis2=glis
lis3=blis

for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp


lis1=glis
lis2=blis
lis3=rlis
for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

lis1=blis
lis2=rlis
lis3=glis
for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

print(ans)

Submission status: Accepted

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

r = K.count("R")
g = K.count("G")
b = K.count("B")
total = r*g*b
n_remove = 0
for space in range(0, N//2 +1):
    for i in range(N- 2*(space+1)):
        try:
            pattern_remove = [K[i], K[i+space+1], K[i+(space*2)+ 2]]
            if set(pattern_remove) == set(["R", "B", "G"]):
                #print(set(pattern_remove))
                n_remove += 1
        except:
            continue
                
print(total - n_remove)

Submission status: Accepted

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

import collections
C = collections.Counter(S)

if len(C) < 3:
    print(0)
    exit()

count = 0

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

ans = 1

for k, v in C.items():
    ans *= v

print(ans - count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def solv(S):
    m = [[], [], []]
    for i, s in enumerate(S):
        if s == "R":
            m[0].append(i)
        elif s == "G":
            m[1].append(i)
        else:
            m[2].append(i)

    if len(m[0]) > len(m[1]):
        m[0], m[1] = m[1], m[0]
    if len(m[0]) > len(m[2]):
        m[0], m[2] = m[2], m[0]
    if len(m[1]) > len(m[2]):
        m[1], m[2] = m[2], m[1]

    C = set(m[2])
    ans = len(m[0]) * len(m[1]) * len(m[2])

    for a0 in m[1]:
        for b0 in m[0]:
            if a0 < b0:
                a, b = a0, b0
            else:
                a, b = b0, a0

            c1 = a - (b-a)
            c2 = b + (b-a)

            if c1 in C:
                ans -= 1
            if c2 in C:
                ans -= 1

            if (a+b) % 2 == 0:
                c3 = (a+b) // 2
                if c3 in C:
                    ans -= 1
    return ans


if __name__ == "__main__":

    _ = input()
    S = input()
    ans = solv(S)

    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r = []
g = []
b = []
R,G,B = 0,0,0
for s in S:
    if s =='R':
        R += 1
    if s == 'G':
        G += 1
    if s == 'B':
        B += 1
    r.append(R)
    g.append(G)
    b.append(B)
ans = 0
for i in range(len(S)):
    for j in range(i+1,len(S)):
        T = ["R","G","B"]
        if S[i] != S[j]:
            T.remove(S[i])
            T.remove(S[j])
            t = T[0]
            if 0<=2*j-i<len(S) and S[2*j-i]==t:
                ans -=1
            if t =="R":
                # ans += r[i]
                ans += (r[-1]-r[j])
            if t =="G":
                # ans += g[i]
                ans += (g[-1]-g[j])
            if t =="B":
                # ans += b[i]
                ans += (b[-1]-b[j])
print(ans)

Submission status: Accepted

Solution code:
import collections

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

s_l = [s.count('R'), s.count('G'), s.count('B')]
for k in s_l:
    ans *= k


cnt = 0
for i in range(1, n-1):
    for j in range(1, min(i, n - i - 1) + 1):
        if s[i] != s[i - j] and s[i] != s[i + j] and s[i - j] != s[i + j]:
            cnt += 1

print(ans - cnt)

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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and s[a] == 'B':
            ans -= 1
        if 0 <= c and c <= n-1 and s[c] == 'B':
            ans -= 1
        if (i + j) % 2 == 0 and s[int((i + j) / 2)] == 'B':
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

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

Submission status: Accepted

Solution code:
input()
s = input()

n_total = {'R':0,'G':0,'B':0}
n_0 = {'R':0,'G':0,'B':0}

for i,mid in enumerate(s):
    n_total[mid] += 1
n_1 = n_total.copy()

cnt = 0
for i,mid in enumerate(s):
    n_0[mid] += 1
    n_1[mid] -= 1
    if mid == 'R':
        cnt += n_0['G'] * n_1['B'] + n_0['B'] * n_1['G']
    if mid == 'G':
        cnt += n_0['R'] * n_1['B'] + n_0['B'] * n_1['R'] 
    if mid == 'B':
        cnt += n_0['G'] * n_1['R'] + n_0['R'] * n_1['G'] 
    for j in range(1,min(i, len(s) - 1 - i)+1):
        if (mid != s[i-j]) & (mid != s[i+j]) & (s[i+j] != s[i-j]):
            cnt -=1
print(cnt)

Submission status: Accepted

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

for i in range(n - 2):
  j = i + 1
  while((i + 1) + (j - i) * 2 <= n):
    if s[i] != s[j] and s[j] != s[i + (j - i) * 2] and s[i] != s[i + (j - i) * 2]:
      answer -= 1
    j += 1
 
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
memo = {key:[] for key in ("R","G","B")}
ans = 0
for i in range(N):
    memo[S[i]].append(i)
    for space in range(1,N+1):
        j = space + i
        k = space + j
        if(k<N):
            if(S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]):
                ans -= 1

ans += len(memo["R"])*len(memo["G"])*len(memo["B"])
print(ans)

Submission status: Accepted

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

R.sort()
G.sort()
B.sort()

from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
 
ans = len(R)*len(G)*len(B)
if ans > 0:
    for r in R:
        for g in G:
            for p in [r*2-g,g*2-r,(r+g)/2]:
                if p<=B[-1] and p>=B[0]:
                    ind = bisect_left(B,p)
                    if p == B[ind]:
                        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

d = [[0] * N for _ in range(3)]
for i in range(3):
    for j in range(N):
        d[i][j] = int(S[j] == "RGB"[i])
        
cumsum = [[0] * (N + 1) for _ in range(3)]
for i in range(3):
    for j in range(N):
        cumsum[i][j + 1] = cumsum[i][j] + d[i][j]

cnt = [len(v) for v in d]
ans = 0
for i in range(1, N - 1):
    for j in range(i + 1, N):
        
        if S[i - 1] == S[j - 1]:
            continue

        # Find `k` string and index
        s = (set("RGB") - set([S[i - 1], S[j - 1]])).pop()
        idx = "RGB".find(s)
        ans += cumsum[idx][-1] - cumsum[idx][j]
        
        k = 2 * j - i
        if k <= N and d[idx][k - 1]:
            ans -= 1
        
print(ans)

Submission status: Accepted

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

r = len([i for i in s if i == "R"])
g = len([i for i in s if i == "G"])
b = len(s)- r-g

res = r *g * b
count = 0
between = 1
isLoop = True
while isLoop:
    for start in range(between):
        tg = s[start::between]
        if start == 0 and len(tg) < 3:
            isLoop = False
            break
        res -= tg.count("RGB") \
        + tg.count("RBG") \
        + tg.count("GRB") \
        + tg.count("GBR") \
        + tg.count("BRG") \
        + tg.count("BGR") 
    between += 1

print(res)

Submission status: Accepted

Solution code:
N=int(input())

S=input()
LEN=len(S)
END='EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE'
S += END

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


cnt=0
for j in range(1, int((LEN-1)/2)+1):
    for i in range(LEN-j*2):
        if S[i+j+j]=='E':
            break
        if (S[i] != S[i+j]) and (S[i+j] != S[i+j+j]) and (S[i] != S[i+j+j]):
            cnt+=1

print(R*G*B-cnt)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    R, G, B = set(), set(), set()
    for i, s in enumerate(S):
        if s == 'R':
            R.add(i)
        elif s == 'G':
            G.add(i)
        else:
            B.add(i)
    lr, lg, lb = len(R), len(G), len(B)
    #if lr < lg:
    #    lr, lg = lg, lr
    #    R, G = G, R
    #if lr < lb:
    #    lr, lb = lb, lr
    #    R, B = B, R

    a = 0

    for g in G:
        for b in B:
            b0, g0 = b, g
            if b0 < g0:
                b0, g0 = g0, b0
            r = lr
            gb = b0 - g0
            if (g0 - gb) in R:
                r -= 1
            if (b0 + gb) in R:
                r -= 1
            if gb % 2 == 0 and (g0 + gb // 2) in R:
                r -= 1
            a += r

    return a

print(main())

Submission status: Accepted

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

r = [0] * n
g = [0] * n
b = [0] * n
ri = gi = bi = 0
for i, c in enumerate(s):
  if c == "R":
    ri += 1
  elif c == "G":
    gi += 1
  if c == "B":
    bi += 1
  r[i] = ri
  g[i] = gi
  b[i] = bi

 
ans = r[-1] * g[-1] * b[-1]

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

print(ans)

Submission status: Accepted

Solution code:
# D
N=int(input())
S=input()
order=list('RGB')
cnt=[[0,0,0] for _ in range(N)]
for i in range(N):
    cnt[i][order.index(S[i])]+=1
    if i!=0:
        for j in range(3):
            cnt[i][j]+=cnt[i-1][j]
ans=0
for j in range(1,N):
    for k in range(j+1,N):
        # print(j,k)
        if S[j]==S[k]:
            continue
        else:
            s=list(set(order)-set([S[j],S[k]]))[0]
            ans+=cnt[j-1][order.index(s)]
            # print('db',ans)
            i=2*j-k
            if i>=0 and S[i]==s:
                # print('!')
                ans-=1
print(ans)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
s = ins()
r = g = b = rg = rb = gr = gb = bg = br = 0
rgb = rbg = gbr = grb = brg = bgr = 0
rgb0 = rbg0 = gbr0 = grb0 = brg0 = bgr0 = 0

for i in range(n):
    c = s[i]
    if c=='R':
        gbr += gb
        bgr += bg
        gr += g
        br += b
        r += 1
        for j in range(1,i//2+1):
            if s[i-j]=='G' and s[i-2*j]=='B':
                bgr0 += 1
            if s[i-j]=='B' and s[i-2*j]=='G':
                gbr0 += 1
    elif c=='G':
        rbg += rb
        brg += br
        bg += b
        rg += r
        g += 1
        for j in range(1,i//2+1):
            if s[i-j]=='R' and s[i-2*j]=='B':
                brg0 += 1
            if s[i-j]=='B' and s[i-2*j]=='R':
                rbg0 += 1
    else:  # c=='B'
        rgb += rg
        grb += gr
        gb += g
        rb += r
        b += 1
        for j in range(1,i//2+1):
            if s[i-j]=='R' and s[i-2*j]=='G':
                grb0 += 1
            if s[i-j]=='G' and s[i-2*j]=='R':
                rgb0 += 1
print(rgb+rbg+bgr+brg+grb+gbr-(rgb0+rbg0+bgr0+brg0+grb0+gbr0))

Submission status: Accepted

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

total = 0

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)


R_len = len(R)
G_len = len(G)
B_len = len(B)

counter1 = 0
counter2 = 0
if max(R_len, G_len, B_len) == B_len:
    for i in range(R_len):
        for j in range(G_len):
            illegal = 0
            r = R[i]
            g = G[j]

            distance = abs(r - g)
            
            if r < g:
                check1 = r - distance
                check2 = g + distance
            else:
                check1 = g - distance
                check2 = r + distance

            if check1 >= 0 and S[check1] == "B":
                illegal += 1
            if check2 < N and S[check2] == "B":
                illegal += 1
            if distance % 2 == 0 and S[(r+g)//2] == "B":
                illegal += 1
   
            total += (B_len - illegal)
elif max(R_len, G_len, B_len) == G_len:
    for i in range(R_len):
        for j in range(B_len):
            illegal = 0
            r = R[i]
            b = B[j]

            distance = abs(r - b)
            
            if r < b:
                check1 = r - distance
                check2 = b + distance
            else:
                check1 = b - distance
                check2 = r + distance

            if check1 >= 0 and S[check1] == "G":
                illegal += 1
            if check2 < N and S[check2] == "G":
                illegal += 1
            if distance % 2 == 0 and S[(r+b)//2] == "G":
                illegal += 1
                
            total += (G_len - illegal)
else:
    for i in range(B_len):
        for j in range(G_len):
            illegal = 0
            b = B[i]
            g = G[j]

            distance = abs(b - g)
            
            if b < g:
                check1 = b - distance
                check2 = g + distance
            else:
                check1 = g - distance
                check2 = b + distance

            if check1 >= 0 and S[check1] == "R":
                illegal += 1
            if check2 < N and S[check2] == "R":
                illegal += 1
            if distance % 2 == 0 and S[(b+g)//2] == "R":
                illegal += 1
                
            total += (R_len - illegal)

print(total)

Submission status: Accepted

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

Rs = []
Bs = []
Gs = []

for i in range(len(S)):
    if S[i] == 'R':
        Rs.append(i)
    if S[i] == 'G':
        Gs.append(i)
    if S[i] == 'B':
        Bs.append(i)

#print(Rs)
#print(Gs)
#print(Bs)

sum = 0

for r in Rs:
    for g in Gs:
        sum += len(Bs)

        if r > g:
            l = r-g
            if r+l < len(S) and S[r + l] == 'B':
                sum -= 1
            if g-l > 0 and S[g - l] == 'B':
                sum -= 1

        else:
            l = g-r
            if len(S) > g + l and S[g + l] == 'B':
                sum -= 1
            if r - l > 0 and S[r - l] == 'B':
                sum -= 1

        if (r + g) % 2 == 0 and S[int((r + g) / 2)] == 'B':
            sum -= 1
            #print(r, end = " ")
            #print(g)

print(sum)

Submission status: Accepted

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

ans = 0

R = []
G = []
B = [0 for _ in range(N+1)]
B_cnt = 0

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

    if S[i-1] == "G":
        G.append(i)

    if S[i-1] == "B":
        B[i] = 1
        B_cnt += 1

for rr in R:
    for g in G:
        l = min(rr, g)
        r = max(rr, g)
        d = r - l
        ans += B_cnt
        if d + r <= N and B[d+r] == 1:
            ans -= 1
        if d % 2 == 0 and B[l + d//2] == 1:
            ans -= 1
        if B[l - d] == 1 and 1 <= l - d:
            ans -= 1


print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
c=s.count
r,g,b=c('R'),c('G'),c('B')
t=0
for i in range(n-2):
  for w in range(1,(n-i+1)//2):
    if s[i]+s[i+w]+s[i+w*2] in ('RGB','RBG','GRB','GBR','BRG','BGR'):
        t+=1
print(r*g*b-t)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
Rd=set();Gd=set();Bd=set()
for i in range(0,N):
    if S[i]=='R':
        Rd.add(i)
    elif S[i]=='G':
        Gd.add(i)
    elif S[i]=='B':
        Bd.add(i)
RN=len(Rd);GN=len(Gd);BN=len(Bd)
s=0
for r in Rd:
    for g in Gd:
        if g>r:
            if 2*r-g in Bd:
                s+=1
                #print(r,g)
            if (r+g)%2==0 and (r+g)//2 in Bd:
                s+=1
                #print(r,g)
            if 2*g-r in Bd:
                s+=1
                #print(r,g)
        else:
            if 2*r-g in Bd:
                s+=1
                #print(r,g)
            if 2*g-r in Bd:
                s+=1
                #print(r,g)
            if (r+g)%2==0 and (r+g)//2 in Bd:
                s+=1
                #print(r, g)
print(RN*GN*BN-s)

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 > g:
            if (r + (r - g)) in color[2]:
                x -= 1
            if (g - (r - g)) in color[2]:
                x -= 1
        else:
            if (g + (g - r)) in color[2]:
                x -= 1
            if (r - (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:
N = int(input())
l = list(input())
from collections import deque
r = deque()
g = []
b = deque()
for i in range(N):
    if l[i] == "R":
        r.append(i)
    elif l[i] == "G":
        g.append(i)
    else:
        b.append(i)
from collections import defaultdict
d = defaultdict(int)
for i in range(len(b)):
    d[b[i]] = 1
ans = 0
na = len(b)
while r:
    rr = r.pop()
    for i in range(len(g)):
        gg = g[i]
        ans += na - d[(rr+gg) / 2] - d[min(rr,gg)-abs(gg-rr)] - d[max(rr,gg)+abs(gg-rr)]
print(ans)

Submission status: Accepted

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

def resolve():
    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)

    ans = 0
    for i in R:
        index_G = bs.bisect_left(G, i)
        index_B = bs.bisect_left(B, i)
        # print(i, index_G, index_B)
        ans += index_G*(len(B)-index_B)
        # print('ansGRB', i, ans)
        for j in range(index_G):
            if 2*i-G[j]<N and S[2*i-G[j]]=='B':
                ans -= 1
        ans += index_B*(len(G)-index_G)
        for j in range(index_B):
            if 2*i-B[j]<N and S[2*i-B[j]]=='G':
                ans -= 1
    # print('ansR', ans)
    for i in G:
        index_R = bs.bisect_left(R, i)
        index_B = bs.bisect_left(B, i)
        # print(i, index_R, index_B)
        ans += index_R*(len(B)-index_B)
        for j in range(index_R):
            if 2*i-R[j]<N and S[2*i-R[j]]=='B':
                ans -= 1
        # print('ansRGB', ans)
        ans += index_B*(len(R)-index_R)
        for j in range(index_B):
            if 2*i-B[j]<N and S[2*i-B[j]]=='R':
                ans -= 1
        # print('ansBGR', ans)
    # print('ansG', ans)
    for i in B:
        index_G = bs.bisect_left(G, i)
        index_R = bs.bisect_left(R, i)
        # print(i, index_G, index_B)
        ans += index_G*(len(R)-index_R)
        for j in range(index_G):
            if 2*i-G[j]<N and S[2*i-G[j]]=='R':
                ans -= 1
        ans += index_R*(len(G)-index_G)
        for j in range(index_R):
            if 2*i-R[j]<N and S[2*i-R[j]]=='G':
                ans -= 1
    # print('ansB', ans)

    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = list(input())
a = collections.Counter(s)
a = list(a.items())
ans = 0
# ar = []
# for i in range(1,2000):
#     c_ar = []
#     for j in range(1,n+1,i):
#         c_ar.append(j)
#     if len(c_ar) >= 3:
#         ar.append(c_ar)
# print(ar)
# print(len(ar))
# for i in range(len(ar)):
#     print(ar[i])
#     print(len(ar[i]))
#     for j in range(len(ar[i])-2):
#         print(s[ar[i][j]-1],s[ar[i][j+1]-1],s[ar[i][j+2]-1])
#         print(ar[i][j]-1, ar[i][j+1]-1, ar[i][j+2]-1)
#         if s[ar[i][j]-1] != s[ar[i][j+1]-1] and s[ar[i][j+1]-1] != s[ar[i][j+2]-1] and s[ar[i][j+2]-1] != s[ar[i][j]-1]:
#             cnt += 1
#             print("Y")
#     print(cnt)
# print(a[0][1],a[1][1],a[2][1])
# print(a[0][1]*a[1][1]*a[2][1]-cnt)
if n == 1 or n == 2 or n == 3:
    print(0)
    exit()
if len(a) != 3:
    print(0)
    exit()
for i in range(1,n//2+3):
    cnt = 0
    #print(i)
    for j in range(n):
        if cnt + 2*i > n-1:
            break
        x,y,z = s[cnt], s[cnt+i], s[cnt+2*i]
        #print(cnt, cnt+i, cnt+i+i)
        if x != y and y != z and z != x:
            ans += 1
        cnt += 1
print(a[0][1]*a[1][1]*a[2][1]-ans)

Submission status: Accepted

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

# from math import comb
def comb(n,r):
    if r==2:
        return n*(n-1)//2
    else:
        return n*(n-1)*(n-2)//6
n=n0()
s=s0()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=0
for i in range(1,(n-1)//2+1):
    for j in range(0,n-2*i):
        ans+=1
        if s[j]==s[j+i]==s[j+2*i]:
            ans-=1
        elif s[j]==s[j+i] or s[j]==s[j+2*i] or s[j+i]==s[j+2*i]:
            ans-=1
            
ans+=comb(r,3)+comb(g,3)+comb(b,3)+comb(r,2)*(g+b)+comb(g,2)*(r+b)+comb(b,2)*(r+g)
print(comb(n,3)-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)

cnt=len(R)*len(G)*len(B)
for i in R:
  for j in G:
    if (i+j)%2==0 and S[(i+j)//2]=='B':
      cnt-=1
for i in R:
  for j in B:
    if (i+j)%2==0 and S[(i+j)//2]=='G':
      cnt-=1
for i in B:
  for j in G:
    if (i+j)%2==0 and S[(i+j)//2]=='R':
      cnt-=1      
print(cnt)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    rcnt, gcnt, bcnt = 0, 0, 0
    for i, s in enumerate(S):
        if s=='R':
            rcnt += 1
        if s=='G':
            gcnt += 1
        if s=='B':
            bcnt += 1
    
    base = rcnt*gcnt*bcnt
    interval = 1
    totalminus = 0
    limit = 1 + (N-3)//2
    while interval <= limit:
        for i in range(interval, N-interval):
            a = S[i-interval]
            b = S[i]
            c = S[i+interval]
            if a!=b and b!=c and c!=a:
                totalminus += 1
        interval += 1
    print(base-totalminus)
main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
 
num_rgb = {'R':0, 'G':0, 'B':0}
num_list = []
for i in range(n):
    num_rgb[s[i]] += 1
    num_list.append(num_rgb.copy())
 
total = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        target = ''
        if not (s[i] == 'R' or s[j] == 'R'):
            target = 'R'
        elif not (s[i] == 'G' or s[j] == 'G'):
            target = 'G'
        elif not (s[i] == 'B' or s[j] == 'B'):
            target = 'B'
        num = num_list[n-1][target]-num_list[j][target]
        if 2*j-i < n and s[j+j-i] == target:
            num -= 1
        total += num
print(total)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = input()

R = [0 for _ in range(N + 5)]
G = [0 for _ in range(N + 5)]
B = [0 for _ in range(N + 5)]

for i in range(N):
    if(i != 0):
        R[i] = R[i - 1]
        G[i] = G[i - 1]
        B[i] = B[i - 1]
    if (A[i] == 'R'):
        R[i] += 1
    elif (A[i] == 'G'):
        G[i] += 1
    elif (A[i] == 'B'):
        B[i] += 1
        
ans = 0
for i in range(1, N):
    if A[i] == 'R':
       ans += G[i - 1] * (B[N - 1] - B[i]) 
       ans += B[i - 1] * (G[N - 1] - G[i]) 
    if A[i] == 'G':
       ans += R[i - 1] * (B[N - 1] - B[i]) 
       ans += B[i - 1] * (R[N - 1] - R[i]) 
    if A[i] == 'B':
       ans += G[i - 1] * (R[N - 1] - R[i]) 
       ans += R[i - 1] * (G[N - 1] - G[i])
    # print(i, ans, end = "")
    for j in range(1, min([N - i - 1, i]) + 1):
        if A[i - j] != A[i] and A[i] != A[i + j] and A[i - j] != A[i + j]:
            ans -= 1
    #print(ans)

print(ans)

Submission status: Accepted

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

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

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

print(r * g * b - count)

Submission status: Accepted

Solution code:
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        num[i+1][j] = num[i][j]
        if d[S[i]] == j:
            num[i+1][j] += 1
num = tuple(num[1:])
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)