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:
from collections import Counter

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

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

if ans == 0:
    print(0)
    exit()

for n_skip in range(1, N):
    for i in range(N):
        try:
            if set(["R", "G", "B"]) == set([S[i], S[i+n_skip], S[i + 2*n_skip]]):
                ans -= 1
        except IndexError:
            break

print(ans)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    S=input()
    ans=S.count('R')*S.count('G')*S.count('B')
    for i in range(N):
        for d in range(N):
            j = i+d
            k = j+d
            if k >= N:
                break
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1
    print(ans)
main()

Submission status: Accepted

Solution code:
N=int(input())
S=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
res=R*G*B
cnt=0
for i in range(N):
    lr=0
    while(i-lr>=0 and i+lr<N):
        s=set([S[i],S[i-lr],S[i+lr]])
        if(len(s)==3):
            res-=1
        lr+=1
print(res)

Submission status: Accepted

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

ans = S.count("R")*S.count("B")*S.count("G")
for i in range(N):
    for d in range(N):
        j = i+d
        k = j+d
        if k < N:
            if S[i] != S[j] and S[k] != S[j] and S[k] != S[i]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect
 
# from operator import mul
 
sys.setrecursionlimit(100001)
 
 
# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)
 
# ===FUNCTION===
 
def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]
 
    return outputDataList
 
 
def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)
 
    return outputDataList
 
 
def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)
 
    return outputDataList
 
 
# ===CODE===
 
n = int(input())
s = list(input())
 
ruisekiwa = []
tmp = [0 for _ in range(3)]
 
for si in s:
    if si == "R":
        tmp[0] += 1
    elif si == "G":
        tmp[1] += 1
    else:
        tmp[2] += 1
    tmpra = copy.deepcopy(tmp)
    ruisekiwa.append(tmpra)
 
ans = 0
for j in range(1, n - 1):
    sj = 0
    if s[j] == "R":
        sj = 0
    elif s[j] == "G":
        sj = 1
    else:
        sj = 2
 
    tmpa = ruisekiwa[j - 1]
    tmpb = [0 for _ in range(3)]
    for tmp in range(3):
        tmpb[tmp]=ruisekiwa[n - 1][tmp] - ruisekiwa[j][tmp]
 
    for a in range(3):
        for b in range(3):
            if a == b:
                continue
            if a == sj or b == sj:
                continue
            ans += tmpa[a] * tmpb[b]
 
    hikukazu = 0
    for e in range(1, 4000):
        if not(-1< j-e < j+e < n):
            break
        if s[j-e] != s[j] and s[j] != s[j+e] and s[j-e] != s[j+e]:
            hikukazu += 1
 
    ans -= hikukazu
 
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
count=0
A=[[0,0,0]for i in range(n)]
for i in range(0,n):
    if s[n-i-1]=="R":
        A[n-i-2][0]=A[n-i-1][0]+1
        A[n-i-2][1]=A[n-i-1][1]
        A[n-i-2][2]=A[n-i-1][2]
    if s[n-i-1]=="G":
        A[n-i-2][0]=A[n-i-1][0]
        A[n-i-2][1]=A[n-i-1][1]+1
        A[n-i-2][2]=A[n-i-1][2]
    if s[n-i-1]=="B":
        A[n-i-2][0]=A[n-i-1][0]
        A[n-i-2][1]=A[n-i-1][1]
        A[n-i-2][2]=A[n-i-1][2]+1
for i in range(0,n-3):
    s1=s[i]
    for j in range(i+1,n-1):
        s2=s[j]
        if s1!=s2:
            if s1=="R" and s2=="G":
                s3="B"
            elif s1=="G" and s2=="R":
                s3="B"
            elif s1=="R" and s2=="B":
                s3="G"
            elif s1=="B" and s2=="R":
                s3="G"
            elif s1=="B" and s2=="G":
                s3="R"
            elif s1=="G" and s2=="B":
                s3="R"
            if s3=="R":
                s4=0
            if s3=="G":
                s4=1
            if s3=="B":
                s4=2
            count=count+A[j][s4]
            if j+j-i+1<=n: 
                if s[j+j-i]==s3:
                    count=count-1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
counter = Counter(S)
ans = counter['R'] * counter['G'] * counter['B']
for l in range(1, N):
    for i in range(N):
        if i + 2*l >= N: break
        a, b, c = S[i], S[i + l], S[i + 2*l]
        if a != b and a != c and b != c: ans -= 1
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
rs = []
gs = []
bs = []
for i,c in enumerate(S):
    if c=='R':
        rs.append(i)
    elif c=='G':
        gs.append(i)
    else:
        bs.append(i)
R,G,B = len(rs),len(gs),len(bs)
rt = set(rs)
gt = set(gs)
bt = set(bs)

ans = 0
ri = gi = bi = 0
for i in range(N):
    if ri < R and i==rs[ri]:
        ans += gi*(B-bi) + bi*(G-gi)
        for g in gs:
            if i+(i-g) in bt:
                ans -= 1
        ri += 1
    elif gi < G and i==gs[gi]:
        ans += ri*(B-bi) + bi*(R-ri)
        for r in rs:
            if i+(i-r) in bt:
                ans -= 1
        gi += 1
    else:
        ans += ri*(G-gi) + gi*(R-ri)
        for r in rs:
            if i+(i-r) in gt:
                ans -= 1
        bi += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input().strip()
r, g, b = map(S.count, 'RGB')
t = 0
for i in range(n-2):
    for w in range(1, (n-i+1) // 2):
        t += len({S[i], S[i+w], S[i+2*w]}) == 3
print(r*g*b - t)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r = S.count("R")
g = S.count("G")
b = S.count("B")
out =r*g*b

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = list(input())

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
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)
    else:
        B.add(i)

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

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
result = 0
for i in range(1,N-1):
    a, b = ("G","B") if S[i] == "R" else ("R","B") if S[i] == "G" else ("R","G")
    mae = S[:i]
    ushiro = S[i+1:]
    result += mae.count(a) * ushiro.count(b)
    result += mae.count(b) * ushiro.count(a)
    result -= sum([(a == mae[-1-j] and b == ushiro[j]) or (b == mae[-1-j] and a == ushiro[j]) for j in range(min(len(mae),len(ushiro)))])
print(result)

Submission status: Accepted

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

R_cnt = s.count("R")
G_cnt = s.count("G")
B_cnt = s.count("B")

ans = R_cnt * G_cnt * B_cnt

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

print(ans)

Submission status: Accepted

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

R,G,B = 0,0,0

for i in range(0,N):
    char = S[i]
    if char == "R":
        R += 1
    elif char == "G":
        G += 1
    elif char == "B":
        B += 1

count = R * G * B

N2 = int(N/2 + 0.5)
for i in range(0,N):
    stri = S[i]
    for j in range(0, N2):
        j2 = 2 * j
        if i+j2 >= N:
            break
        strj = S[i+j]
        strk = S[i+j2]
        if stri != strj and strj != strk and stri != strk:
            count -= 1
print(count)

Submission status: Accepted

Solution code:
#problem D
input_d41 = int(input())
input_d42 = input()

N = int(input_d41)
S = input_d42

R = S.count('R')
G = S.count('G')
B = N - (R + G)

ans = R*G*B
answ = 0

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R)*len(G)*len(B)
for i in range(N):
    for j in range(i+1,(N+i+1)//2):
        if S[i] != S[j] and S[j] != S[2*j-i] and S[i] != S[2*j-i]:
            s -= 1
print(s)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = S.count('R')*S.count('G')*S.count('B')
for i in range(N):
    for d in range(N):
        j = i+d
        k = j+d
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

rsum = 0
gsum = 0
bsum = 0

for s in S_list:
	if s == "R":
		rsum += 1
	elif s == "G":
		gsum += 1
	elif s == "B":
		bsum += 1

sum = rsum * gsum * bsum

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

print(sum)

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = list(input())

    t = Counter(s)

    ans = t['R']*t['G']*t['B']

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    R = C.get("R", 0)
    G = C.get("G", 0)
    B = C.get("B", 0)
    ans = R * G * B
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid + mid - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

colors = ['R', 'B', 'G']

cnt = 1
for c in colors:
    cnt *= s.count(c)

for i in range(n):
    w = 1
    while True:
        if i + 2*w >= n: break
        if s[i] != s[i+w] and s[i+w] != s[i+2*w] and s[i+2*w] != s[i]:
            cnt -= 1
        w += 1
print(cnt)

Submission status: Accepted

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

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
setB = set(B)

for r in R:
    for g in G:
        if g - abs(r - g) in setB:
            rem += 1
        if r - abs(g - r) in setB:
            rem += 1
        if (g + r) / 2 in setB:
            rem += 1
        if g + abs(r - g) in setB:
            rem += 1
        if r + abs(g - r) in setB:
            rem += 1


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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
total_triplets = sum(int(val=="R") for val in s)*sum(int(val=="G") for val in s)*sum(int(val=="B") for val in s)
num_missing = 0
for centre_point in range(1, n-1):
  for halfwidth in range(1, 1+min(centre_point, n-centre_point-1)):
    if s[centre_point-halfwidth] != s[centre_point]:
      if s[centre_point] != s[centre_point+halfwidth]:
        if s[centre_point-halfwidth] != s[centre_point+halfwidth]: num_missing += 1
print(total_triplets-num_missing)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
s = input()
c_dic = defaultdict(list)
for idx,i in   enumerate(s):
    c_dic[i].append(idx)
ans = len(c_dic['R'])*len(c_dic['G'])*len(c_dic['B'])
for i in range(0,n-1):
    for j in range(i+1,n):
        k = 2*j - i
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
count=0
char="RGB"
A=[[0,0,0] for i in range(n)]
if s[0]=="R":
    A[0][0]+=1
elif s[0]=="G":
    A[0][1]+=1
else:
    A[0][2]+=1

for i in range(1,n):
    x=A[i-1][0]
    y=A[i-1][1]
    z=A[i-1][2]
    if s[i]=="R":
        x+=1
    elif s[i]=="G":
        y+=1
    else:
        z+=1
    A[i][0]=x
    A[i][1]=y
    A[i][2]=z
#print(A)
count=0
for i in range(n):
    for k in range(i+2,n):
        if s[i]!=s[k]:
            c=3-char.index(s[i])-char.index(s[k])
            x=A[k][c]-A[i][c]
            count+=x
            if (i+k)%2==0:
                j=(i+k)//2
                if char.index(s[j])==c:
                    count-=1
print(count)

Submission status: Accepted

Solution code:
n = int(input())

s = input()
cnt = s.count('R') * s.count('G') * s.count('B')

for i in range(n):
    for j in range(n):
        if 0 <= i - j <= i and 0 <= i + j <= n-1:
            x1, x2, x3 = s[i - j], s[i], s[i + j]
            if x1 != x2 and x1 != x3 and x2 != x3:
                cnt -= 1
print(cnt)

Submission status: Accepted

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

cnt = s.count("R") * s.count("G") * s.count("B")

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

Submission status: Accepted

Solution code:
import math
N = int(input())
S = input()
outexp = 0

def comb(n, r):
  comb = math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
  return comb

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

if R >= 3:
  Rnum = comb(R, 2) * (N - R) + comb(R, 3)
if R == 2:
  Rnum = N - R
if R <= 1:
  Rnum = 0

if B >= 3:
  Bnum = comb(B, 2) * (N - B) + comb(B, 3)
if B == 2:
  Bnum = N - B
if B <= 1:
  Bnum = 0

if G >= 3:
  Gnum = comb(G, 2) * (N - G) + comb(G, 3)
if G == 2:
  Gnum = N - G
if G <= 1:
  Gnum = 0

for i in range(N - 2):
  m = 1
  while 2 * m + i <= N - 1:
    if not S[i] == S[i + m]:
      if not S[i + m] == S[i + 2 * m]:
        if not S[i] == S[i + 2 * m]:
          outexp += 1
    m += 1

sum = comb(N, 3) - Rnum - Bnum - Gnum - outexp

print(int(sum))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
RGB_Counter = Counter(S)
ans = RGB_Counter['R'] * RGB_Counter['G'] * RGB_Counter['B']

dum = []
d = 0
while 3 + d*2 <= N:
    for i in range(N - 2*(d+1)):
        a = S[i]
        b = S[i+d+1]
        c = S[i+2*(d+1)]
        if a != b and b != c and c != a:
            ans -= 1
    d += 1

print(ans)

Submission status: Accepted

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

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

for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        # 三個目が範囲内 かつ 三文字違う かつ 距離が同じはダメ
        if k < n and (s[i] != s[j] and s[j] != s[k]
                      and s[i] != s[k]) and j - i == k - j:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

ans = rcnt*gcnt*bcnt

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")  # 第１条件の全事象
for i in range(n-2):
    for j in range(i+1,n-1):
        k=j+j-i
        if k<n and s[k]!=s[i] and s[k]!=s[j] and s[i]!=s[j]: # 第1条件 かつ not第２条件
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
s = list(input())
baf = s.count('B')
raf = s.count('R')
gaf = s.count('G')
bbf = 0
rbf = 0
gbf = 0
ans = 0
for i,item in enumerate(s):
    if item == 'B':
        baf -= 1
        bbf += 1
        ans += rbf*gaf
        ans += raf*gbf
    elif item == 'R':
        raf -= 1
        rbf += 1
        ans += bbf*gaf
        ans += baf*gbf
    else:
        gaf -= 1
        gbf += 1
        ans += rbf*baf
        ans += raf*bbf
cnt = 0
for i in range(n):
    num = min(i,n-1-i)
    for j in range(1,num+1):
        if s[i-j] != s[i+j] and s[i-j] != s[i] and s[i+j] != s[i]:
            cnt += 1
print(ans-cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=S.count("R")*S.count("G")*S.count("B")
for i in range(N):
    for j in range(i+1,N):
        k=2*j-i
        if k<N and S[k]!=S[i] and S[k]!=S[j] and S[j]!=S[i]:
            ans-=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
if n <= 3:
    sum = 0
else:
    sum = 0
    r = []
    g = []
    b = []
    if s[n-1] == "R":
        r.append(1)
        g.append(0)
        b.append(0)
    if s[n-1] == "G":
        r.append(0)
        g.append(1)
        b.append(0)
    if s[n-1] == "B":
        r.append(0)
        g.append(0)
        b.append(1)
    for i in range(1,n):
        if s[n-i-1] == "R":
            r.append(r[i-1]+1)
            g.append(g[i-1])
            b.append(b[i-1])
        if s[n-i-1] == "G":
            r.append(r[i-1])
            g.append(g[i-1]+1)
            b.append(b[i-1])
        if s[n-i-1] == "B":
            r.append(r[i-1])
            g.append(g[i-1])
            b.append(b[i-1]+1)
    for i in range(n-3):
        if s[i] == "R":
            j = i + 1
            while j <= n-2:
                if s[j] == "G":
                    sum += b[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += g[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                j += 1
        if s[i] == "G":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += b[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += r[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
        if s[i] == "B":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += g[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                if s[j] == "G":
                    sum += r[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
print(sum)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
c=s.count
a=c("R")*c("G")*c("B")
for i in range(n-2):
    for j in range((n-i+1)//2):
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i+2*j]!=s[i]:
            a-=1
print(a)

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[(i + j) // 2] == 'B':
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
 
n = int(input())
s = input()
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(n) for j in range(i + 2, n, 2)))
except ValueError:
  print(0)

Submission status: Accepted

Solution code:
import bisect
 
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])
len_B=len(RGB[2])
for r in RGB[0]:
    for g in RGB[1]:
        dif = abs(r - g)
        up = max(r,g)+dif
        down = min(r,g)-dif
        up_posi=bisect.bisect_left(RGB[2],up)
 
        if up_posi < len_B:
            if RGB[2][up_posi]==up:
                total-=1
        down_posi=bisect.bisect_left(RGB[2],down)
        if down_posi < len_B:
            if RGB[2][down_posi]==down:
                total-=1
        if dif % 2 == 0:
            center = (r+g)/2
            center_posi=bisect.bisect_left(RGB[2], center)
            if center_posi < len_B:
                if RGB[2][center_posi]==center:
                    total-=1
print(total)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

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


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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = S.count("R")
G = S.count("G")
B = S.count("B")
 
ans = R*G*B
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j - i
        if k < N:
            tmp = []
            tmp.append(S[i])
            tmp.append(S[j])
            tmp.append(S[k])
            if len(set(tmp)) == 3:
                ans -= 1
 
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

input()
s = input()
n = len(s)
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(n) for j in range(i + 2, n, 2)))
except ValueError:
  print(0)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
R_cnt = [0] * (N + 1)
B_cnt = [0] * (N + 1)
G_cnt = [0] * (N + 1)
for i in range(N):
    if s[i] == 'R':
        R_cnt[i + 1] = R_cnt[i] + 1
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i]
    elif s[i] == 'B':
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i] + 1
        G_cnt[i + 1] = G_cnt[i]
    else:
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i] + 1
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if s[i] == s[j]:
            continue
            
        if s[i] == 'R' and s[j] == 'B' or s[i] == 'B' and s[j] == 'R':
            ans += G_cnt[N] - G_cnt[j]
        elif s[i] == 'B' and s[j] == 'G' or s[i] == 'G' and s[j] == 'B':
            ans += R_cnt[N] - R_cnt[j]
        elif s[i] == 'R' and s[j] == 'G' or s[i] == 'G' and s[j] == 'R':
            ans += B_cnt[N] - B_cnt[j]
        if j + (j - i) < N and s[i] != s[j] != s[j + (j - i)] != s[i]:
            ans -= 1
print(ans)

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]:
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 RGB[2][bisect.bisect_left(RGB[2],up)]==up:
            total-=1
    if min_B <= down and max_B >= down:
        if RGB[2][bisect.bisect_left(RGB[2],down)]==down:
            total-=1
    if dif % 2 == 0:
        center = (r+g)/2
        center_posi=bisect.bisect_left(RGB[2], center)
        if center_posi < len_B:
            if RGB[2][center_posi]==center:
                total-=1
print(total)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

counter = r_cnt * g_cnt * b_cnt

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

Submission status: Accepted

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

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

for i in range(N):
    for j in range(N):
        if i+2*j < N:
            if len(set([S[i],S[i+j],S[i+2*j]]))==3:
                ans -=1
print(ans)

Submission status: Accepted

Solution code:
# これは, u749194f さんの改造です。
# オリジナルではございません。
# https://atcoder.jp/contests/abc162/submissions/11825963
n = int(input())
s = input()
flag = True
if len(set(s))<=2:
    print(0)
    flag = False
if flag:
    r = [i for i in range(n) if s[i]=='R']
    b = [i for i in range(n) if s[i]=='B']
    g = [i for i in range(n) if s[i]=='G']
    minus = 0
    def cnt_cmb(a, b, c):
        cnt = 0
        set_a = set(a)
        set_b = set(b)
        set_c = set(c)
        for i in set_a:
            for j in set_b:
                if (i+j)/2 in set_c:
                    cnt += 1
        return cnt
    minus += cnt_cmb(r, b, g)
    minus += cnt_cmb(b, g, r)
    minus += cnt_cmb(g, r, b)
    print(len(r)*len(b)*len(g)-minus)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
L = {"R": 0, "G": 0, "B": 0}
R = {"R": s.count("R"), "G": s.count("G"), "B": s.count("B")}
ans = 0
L[s[0]]+=1
R[s[0]]-=1
for i in range(1, n - 1):
	t = s[i]
	if t == "R":
		ans += L["G"] * R["B"] + L["B"] * R["G"]
	elif t == "G":
		ans += L["R"] * R["B"] + L["B"] * R["R"]
	else:
		ans += L["R"] * R["G"] + L["G"] * R["R"]
	for j in range(min(i,n-1-i)):
		ans -= t!=s[i-j-1]!=s[i+j+1]!=t
	L[t]+=1
	R[t]-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = [0, 0, 0]
col1 = []
col2 = []
col3 = []
res = 0

for i in range(n):
    if s[i] == "R":
        col1.append(i)
    elif s[i] == "G":
        col2.append(i)
    else:
        col3.append(i)

res = len(col1) * len(col2) * len(col3)

for i in range((n + 1) // 2):
    for j in range(n - (i + 1) * 2):
        x = s[j]
        y = s[j + i + 1]
        z = s[j + (i + 1) * 2]
        if x == y or y == z or x == z:
            continue
        else:
            res = res - 1

print(res)