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:
import bisect
n=int(input())
s=input()
r=[]
g=[]
b=[]
for i in range(n):
  if s[i]=='R':
    r.append(i)
  elif s[i]=='G':
    g.append(i)
  else:
    b.append(i)
ans=len(r)*len(g)*len(b)
r.append(n*4)
g.append(n*4)
b.append(n*4)
for i in g:
  for j in b:
    a=i+j
    if a%2==0 and r[bisect.bisect_left(r, a//2)]==a//2:
      ans-=1
for i in r:
  for j in b:
    a=i+j
    if a%2==0 and g[bisect.bisect_left(g, a//2)]==a//2:
      ans-=1
for i in g:
  for j in r:
    a=i+j
    if a%2==0 and b[bisect.bisect_left(b, a//2)]==a//2:
      ans-=1
print(ans+3)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=input()
c=collections.Counter(s)
ans=c["R"]*c["G"]*c["B"]
for i in range(1,(n-1)//2+1):
  for k in range(n-i*2):
    if s[k]!=s[k+i] and s[k]!=s[k+2*i] and s[k+i]!=s[k+2*i]:
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    r = [0]
    g = [0]
    b = [0]
    for i in S:
        if i == 'R':
            r.append(r[-1]+1)
            g.append(g[-1])
            b.append(b[-1])
        if i == 'G':
            r.append(r[-1])
            g.append(g[-1]+1)
            b.append(b[-1])
        if i == 'B':
            r.append(r[-1])
            g.append(g[-1])
            b.append(b[-1]+1)
    rr = r[-1]
    gg = g[-1]
    bb = b[-1]
    ans = 0
    for i, v in enumerate(S[:-2], start=1):
        if v == 'R':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'G':
                    ans += bb - b[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'B':
                        ans -= 1
                if w == 'B':
                    ans += gg - g[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'G':
                        ans -= 1
        if v == 'G':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'R':
                    ans += bb - b[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'B':
                        ans -= 1
                if w == 'B':
                    ans += rr - r[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'R':
                        ans -= 1
        if v == 'B':
            for j, w in enumerate(S[i:-1], start=i+1):
                if w == 'R':
                    ans += gg - g[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'G':
                        ans -= 1
                if w == 'G':
                    ans += rr - r[j]
                    if 2*j - i-1 < N and S[2*j - i-1] == 'R':
                        ans -= 1
    return ans
print(main())

Submission status: Accepted

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

i,j = 0,1
while 2*j < N:
    while i+2*j < N:
        # print(i,i+j,i+2*j)
        if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i+2*j] != S[i]:
            c -= 1
        i += 1
    i = 0
    j += 1
print(c)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    S = input()
    C = Counter([s for s in S]).items()
    ans = 1
    if len (C) < 3:
        ans = 0
    for c in C:
        ans *= c[1]
    for i in range(n):
        mid = i + 1
        last = i + 2
        while last < n:
            if S[i] != S[mid] and S[mid] != S[last] and S[last] != S[i]:
                ans -= 1
            mid += 1
            last += 2
    print(ans)
main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r_cnt = S.count('R')
g_cnt = S.count('G')
b_cnt = N - (r_cnt + g_cnt)

ans = r_cnt * g_cnt * b_cnt

for i in range(1,(N//2)+1):
	for j in range(N):
		if j + 2*i >= N:
			break
		tmp = [S[j],S[j+i],S[j+2*i]]
		tmp = list(set(tmp))
		if len(tmp) == 3:
			ans -= 1

print(ans)

Submission status: Accepted

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

r_kazu = len(r)
g_kazu = len(g)
b_kazu = len(b)

A = r_kazu * g_kazu * b_kazu

#print(A)

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

                    A -= 1


print(A)

Submission status: Accepted

Solution code:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

Rs = []
Gs = []
Bs = []
for index, s in enu(S):
    if s == 'R':
        Rs.append(index+1)
    elif s == 'G':
        Gs.append(index+1)
    elif s == 'B':
        Bs.append(index+1)
#print(Rs, Gs, Bs)

cnt = len(Rs) * len(Gs) * len(Bs)
sBs = set(Bs)

for rr in Rs:
    for gg in Gs:
        r, g = sorted([rr, gg])

        pt1 = g + (g-r)
        pt2 = -1
        if (g-r)%2==0:
            pt2 = r + (g-r)//2
        pt3 = r - (g-r)

        chks = [val for val in [pt1, pt2, pt3] if val > 0]
        for chk in chks:
            if chk in sBs:
                cnt -= 1

print(cnt)

Submission status: Accepted

Solution code:
import bisect
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)
        elif S[i]=='B':
            B.append(i)
    ans=0
    for i in range(N):
        for j in range(i):
            if(S[i]==S[j]):
                continue
            s=set([S[i],S[j]])
            if('B' not in s):
                lon=len(B)-bisect.bisect_left(B,i)
                mi=bisect.bisect_left(B,i+i-j)
                if(mi<len(B) and B[mi]==i*2-j):
                    lon-=1
            elif('R' not in s):
                lon=len(R)-bisect.bisect_left(R,i)
                mi=bisect.bisect_left(R,i+i-j)
                if(mi<len(R) and R[mi]==i*2-j):
                    lon-=1
            elif('G' not in s):
                lon=len(G)-bisect.bisect_left(G,i)
                mi=bisect.bisect_left(G,i+i-j)
                if(mi<len(G) and G[mi]==i*2-j):
                    lon-=1
            ans+=lon
        
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import collections

n=int(input())
s=input()
m=(n-1)//2
x=0

l=collections.Counter(s)
ans=l['R']*l['G']*l['B']

for i in range(1, m+1):
    for j in range(n-i*2):
        a=j
        b=a+i
        c=b+i
        if s[a] != s[b] != s[c] != s[a]:
            x += 1

print(ans-x)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = 0
g = 0
b = 0
if (n==3):
    print(0)
    exit()


for i in range(n):
    if (s[i]=="R"):
        r += 1
    elif (s[i]=="G"):
        g += 1
    else:
        b += 1
da = r*g*b
count = 0
if (n%2==1):
    for i in range (0,n//2):
        for j in range(0,n-2*(i+1)):
            if (s[j]!=s[j+i+1] and s[j+i+1]!=s[j+2*(i+1)] and s[j+2*(i+1)]!=s[j]):
                count += 1
else:
    for i in range (0,n//2-1):
        for j in range(0,n-2*(i+1)):
            if (s[j]!=s[j+i+1] and s[j+i+1]!=s[j+2*(i+1)] and s[j+2*(i+1)]!=s[j]):
                count += 1
                      
print(da -count)

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)
    elif s[i] == "B":
        b.append(i)

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

j = 1
while j * 2 <= n:
    for k in range(n - 2 * j):
        if set([s[k], s[k + j] , s[k + 2 * j]]) == set(["R", "G", "B"]):
            ans -= 1
    j += 1

print(ans)

Submission status: Accepted

Solution code:
x = int(input())
s = list(input())
di = {'R':0, 'G':0, 'B':0}
for i in s:
    di[i] += 1
c = 0 #重複を数える
p = (len(s) - 3) / 2
for i in range(len(s)-2):
    j = 1
    while(j <= p + 1and i+(2*j)<=len(s)-1):
        ss = s[i] + s[i+j] + s[i+(2*j)]
        j += 1
        if ss == 'GBR'or ss ==  'GRB' or ss ==  'RBG' or ss == 'RGB' or  ss == 'BGR' or ss == 'BRG':
                c += 1
print(di['R'] * di['G'] * di['B'] - c)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

R = S.count("R")
G = S.count("G")
B = S.count("B")
ignore = 0
for i in range(N):
    a = S[i]
    for j in range(1,N):
        if i + j + j >= N:
            break
        b = S[i+j]
        c = S[i+j+j]
        if a != b and b != c and c != a:
            ignore += 1
print(R * G * B - ignore)

Submission status: Accepted

Solution code:
# coding: utf-8


def main():
    N = int(input())
    S = input()
    S = [-1 if x == "R" else (0 if x == "G" else 1) for x in S]

    n_right_RGB = [None] * N

    n_r = 0
    n_g = 0
    n_b = 0
    for i in range(N):
        if S[i] == -1:
            n_r += 1
        elif S[i] == 0:
            n_g += 1
        else:
            n_b += 1
    
    for i in range(N):
        if S[i] == -1:
            n_r -= 1
        elif S[i] == 0:
            n_g -= 1
        else:
            n_b -= 1

        n_right_RGB[i] = [n_r, n_g, n_b]
    
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == -1 and S[j] == 0:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == -1 and S[j] == 1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 0 and S[j] == 1:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
            elif S[i] == 0 and S[j] == -1:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == 1 and S[j] == -1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 1 and S[j] == 0:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
    
    return ans


print(main())

Submission status: Accepted

Solution code:
N = int(input())
S = (input())
sum_count = 0
RGB_counter = [0,0,0]
total_pattern = 0


for i in range(3):
    if S[N-i-1] == "R":
        RGB_counter[0] += 1
    elif S[N-i-1] == "G":
        RGB_counter[1] += 1
    else:
        RGB_counter[2] += 1



for i in range(N-3):
    if S[N-i-4] == "R":
        RGB_counter[0] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[1] * RGB_counter[2]
 
            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "B" and S[N-i-4+2*(j+1)] == "G") or (S[N-i-4+(j+1)] == "G" and S[N-i-4+2*(j+1)] == "B"):
                    total_pattern -= 1
    elif S[N-i-4] == "G":
        RGB_counter[1] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[0] * RGB_counter[2]

            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "R" and S[N-i-4+2*(j+1)] == "B") or (S[N-i-4+(j+1)] == "B" and S[N-i-4+2*(j+1)] == "R"):
                    total_pattern -= 1
    else:
        RGB_counter[2] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[0] * RGB_counter[1]

            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "R" and S[N-i-4+2*(j+1)] == "G") or (S[N-i-4+(j+1)] == "G" and S[N-i-4+2*(j+1)] == "R"):
                    total_pattern -= 1  
    sum_count += total_pattern

print(sum_count)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
RGB=[[] for i in range(3)]
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)
L=[len(RGB[0]),len(RGB[1]),len(RGB[2])]
m=L.index(max(L))
A=[]
for i in range(3):
    if i==m:
        continue
    else:
        A.append(i)
C=RGB[A[0]]
D=RGB[A[1]]
E=RGB[m]

ans=0
for i in range(L[A[0]]):
    for j in range(L[A[1]]):
        r=C[i]
        g=D[j]
        d=abs(r-g)
        cnt=0
        if d%2==0:
            if (r+g)//2 in E:
                cnt+=1
        u=max(r,g)+d
        l=min(r,g)-d
        if 0<=u<=n-1:
            if u in E:
                cnt+=1
        if 0<=l<=n-1:
            if l in E:
                cnt+=1
        ans+=L[m]-cnt
print(ans)

Submission status: Accepted

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

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

r = [0]*n
g = [0]*n
b = [0]*n

rc,gc,bc = 0,0,0

for i in range(n-1,-1,-1):
    if s[i] == "R":
        rc += 1
    elif s[i] == "G":
        gc += 1
    else:
        bc += 1

    r[i] = rc
    g[i] = gc
    b[i] = bc

if n in [1,2]:
    print(0)
else:
    res = 0
    for i in range(1,n-1):
        for j in range(i):
            if s[i] == s[j]:
                continue
            if s[i] != "G" and s[j] != "G":
                res += g[i+1]
                if i+i-j < n and s[i+i-j] == "G":
                    res -= 1
            if s[i] != "B" and s[j] != "B":
                res += b[i+1]
                if i+i-j < n and s[i+i-j] == "B":
                    res -= 1
            if s[i] != "R" and s[j] != "R":
                res += r[i+1]
                if i+i-j < n and s[i+i-j] == "R":
                    res -= 1
            
    print(res)

Submission status: Accepted

Solution code:
# coding: utf-8
import itertools
import math
import sys
#input→sys.stdin.readline半分の時間に
input=sys.stdin.readline
N = int(input())
S = list(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

for i in range(N-1):
    R[i+1] += R[i]
    G[i+1] += G[i]
    B[i+1] += B[i]
ans = 0
#print(R,G,B)
for i in range(N-2):
    for j in range(i+1,N-1):
        if len(set([S[i],S[j]])) == 2:
            s = list(set(['R','G','B']) ^ set([S[i],S[j]]))[0]
            if s == 'R':
                ans += R[N-1] - R[j]
                if 2*j-i<=N-1 and S[2*j-i] == 'R':
                    ans -= 1
            if s == 'G':
                ans += G[N-1] - G[j]
                if 2*j-i <= N-1 and S[j+j-i] == 'G':
                    ans -= 1
            if s == 'B':
                ans += B[N-1] - B[j]
                if 2*j-i <= N-1 and S[j+j-i] == 'B':
                    ans -= 1
print(ans)

Submission status: Accepted

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

R = [i for i in range(N) if S[i] == "R"]
G = [i for i in range(N) if S[i] == "G"]
B = [i for i in range(N) if S[i] == "B"]

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

print(ans)

Submission status: Accepted

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    n = int(input())
    s = input()
    r = [0] * n
    g = [0] * n
    b = [0] * n
    if s[0] == 'R':
        r[0] = 1
    elif s[0] == 'G':
        g[0] = 1
    else:
        b[0] = 1
    for i, v in enumerate(s[1:]):
        if v == 'R':
            r[i+1] = 1
        elif v == 'G':
            g[i+1] = 1
        else:
            b[i+1] = 1
        r[i+1] += r[i]
        g[i+1] += g[i]
        b[i+1] += b[i]
    ans = 0
    for j, v in enumerate(s[:-1]):
        if j == 0:
            continue
        if v == 'R':
            ans += g[j-1] * (b[-1] - b[j]) + b[j-1] * (g[-1] - g[j])
        elif v == 'G':
            ans += r[j-1] * (b[-1] - b[j]) + b[j-1] * (r[-1] - r[j])
        else:
            ans += r[j-1] * (g[-1] - g[j]) + g[j-1] * (r[-1] - r[j])
        for d in range(1, j + 1):
            i = j - d
            k = j + d
            if i < 0 or k >= n:
                break
            if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                ans -= 1
    print(ans)

main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
x=dict()
for i in range(N):
    s=S[i]
    if s in x:
        x[s]+=1
    else:
        x[s]=1
count=0
for i in range(1,N+1):
    for k in range(i+1,N+1):
        tmp=i+k
        if tmp%2==0:
            j=tmp//2
            if S[i-1]!=S[j-1] and S[i-1]!=S[k-1] and S[j-1]!=S[k-1]:
                count+=1
ans=1
cnt=0
for idx in x:
    cnt+=1
    ans=ans*x[idx]
if cnt!=3:
    print(0)
    exit()
ans=ans-count
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

dup=0
RGB=set(('R','G','B'))

for width in range(N//2+10):
    for i in range(N):
        try:
            if set((S[i],S[i+width],S[i+width*2]))==RGB:
                dup+=1
        except IndexError:
            break

c=Counter(S)
retval=c['R']*c['G']*c['B']-dup
print(retval)

Submission status: Accepted

Solution code:
import math
import fractions
import collections
import itertools
from collections import deque
N=int(input())
S=input()
leftRGB=[[0,0,0] for _ in range(N)]
rightRGB=[[0,0,0] for _ in range(N)]
cnt=0
buf=0
ch=""
R=0
G=0
B=0
for i in range(N):
    if S[i]=="R":
        R=R+1
    elif S[i]=="G":
        G=G+1
    else:
        B=B+1
    leftRGB[i][0]=R
    leftRGB[i][1] = G
    leftRGB[i][2] = B
R=0
G=0
B=0
for i in range(N):
    if S[N-1-i]=="R":
        R=R+1
    elif S[N-1-i]=="G":
        G=G+1
    else:
        B=B+1
    rightRGB[N-1-i][0]=R
    rightRGB[N-1-i][1] = G
    rightRGB[N-1-i][2] = B
#print(leftRGB)
#print(rightRGB)
for i in range(N):
    if (i!=0)and(i!=N-1):
        ch=S[i]
        if S[i]=="R":
            cnt=cnt+(leftRGB[i-1][1]*rightRGB[i+1][2])+(leftRGB[i-1][2]*rightRGB[i+1][1])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="G")and(S[i+j+1]=="B"):
                    cnt=cnt-1
                if (S[i-j-1]=="B")and(S[i+j+1]=="G"):
                    cnt=cnt-1
        elif S[i]=="G":
            cnt = cnt + (leftRGB[i - 1][0] * rightRGB[i + 1][2])+(leftRGB[i - 1][2] * rightRGB[i + 1][0])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="R")and(S[i+j+1]=="B"):
                    cnt=cnt-1
                if (S[i-j-1]=="B")and(S[i+j+1]=="R"):
                    cnt=cnt-1
        else:
            cnt = cnt + (leftRGB[i - 1][0] * rightRGB[i + 1][1])+(leftRGB[i - 1][1] * rightRGB[i + 1][0])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="R")and(S[i+j+1]=="G"):
                    cnt=cnt-1
                if (S[i-j-1]=="G")and(S[i+j+1]=="R"):
                    cnt=cnt-1
        #print(leftRGB[i - 1],rightRGB[i + 1],S[i],cnt)
        #input()
print(cnt)

Submission status: Accepted

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

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

rest = 0
for gap in range(1, n//2 +1):
    for i in range(n-2):
        try:
            arr = sorted([s[i],s[i+gap],s[i+gap*2]])
            if arr == ["B","G","R"] : rest += 1
        except: continue

ans = r*g*b - rest
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(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
    elif s[i]=='B':
        B[i]=1

ans=0
for i in range(n):
    if R[i]==1:
        lG=sum(G[:i])
        lB=sum(B[:i])
        rG=sum(G[i:])
        rB=sum(B[i:])
        ans+=(lG+rG)*(lB+rB)
        for j in range(1,n//2+1):
            if R[i]==1:
                if 0<=i-j<n and 0<=i+j<n:
                    if G[i-j]==1 and B[i+j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i+j]==1:
                        ans-=1
                if 0<=i-2*j<n:
                    if G[i-j]==1 and B[i-2*j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i-2*j]==1:
                        ans-=1
                if 0<=i+2*j<n:
                    if G[i+j]==1 and B[i+2*j]==1:
                        ans-=1
                    if B[i+j]==1 and G[i+2*j]==1:
                        ans-=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
chkR = 0
chkB = 0
chkG = 0
if n == 1 or n==2:
  print(0)
  exit()
  
for i in range(n):
  if s[i] =='R':
    chkR+=1
  if s[i] =='B':
    chkB+=1
  if s[i] =='G':
    chkG+=1
    
ans = chkR*chkB*chkG

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
ans = 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(int((N+1)/2)):
    for j in range(i,N-i):
        if S[j-i] != S[j] and S[j] != S[j+i] and S[j+i] != S[j-i]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r = S.count("R")
g = S.count("G")
b = S.count("B")
ans = r*g*b
for i in range(1, (N-1)//2+1):
    for j in range(N-i*2):
        if not (S[j] == S[j+i] or S[j] == S[j+i*2] or S[j+i] == S[j+i*2]):
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# import itertools
# from operator import attrgetter, itemgetter
# import math

# from numba import jit

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    if len(set(s)) != 3:
        print(0)
        sys.exit()

    from collections import Counter

    cntr = Counter()
    ans = 0

    for i, ch in enumerate(s):
        cntr[ch] += 1
        if ch == "R":
            ans += cntr["G"] * cntr["B"]
        if ch == "G":
            ans += cntr["R"] * cntr["B"]
        if ch == "B":
            ans += cntr["R"] * cntr["G"]

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

rcount = 0
gcount = 0
bcount = 0

for s in ss:
  if s == "R":
    rcount += 1
  elif s == "G":
    gcount += 1
  else:
    bcount += 1

sumcount = rcount * gcount * bcount
# print(sumcount)

dist = 1
while dist <= (n - 1) / 2:
  i = 0
  while i + 2 * dist < n:
    if ss[i] != ss[i + dist] and ss[i + dist] != ss[i + 2 * dist] and ss[i + 2 * dist] != ss[i]:
      sumcount -= 1
      # print(ss[i], ss[i+dist], ss[i+dist*2])
    i += 1
  dist += 1

print(sumcount)

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

out = 0
t = 1
while(n >= t*2+1):
    for i in range(n-2*t):

        #print(i,i+t,i+2*t)
        #print(s[i],s[i+t],s[i+2*t])

        if s[i] != s[i+t]:
            if s[i+t] != s[i+2*t]:
                if s[i] != s[i+2*t]:
                    ans -= 1
    t+=1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin 
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
answer = 0
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
answer += r*g*b
for i in range(n):
    for j in range(i):
        k = 2*j - i
        if k >= 0:
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                answer -= 1
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(n-2):
  for d in range(n):
    j=i+d
    k=i+2*d
    if k>=n:
      break
    if not (s[i]==s[j] or s[j]==s[k] or s[k]==s[i]):
      ans+=1
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans+=r*(r-1)//2*g+r*(r-1)//2*b+g*(g-1)//2*r+g*(g-1)//2*b+b*(b-1)//2*r+b*(b-1)//2*g+r*(r-1)*(r-2)//6+g*(g-1)*(g-2)//6+b*(b-1)*(b-2)//6
print(n*(n-1)*(n-2)//6-ans)

Submission status: Accepted

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

allpattern = R*G*B
counter = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = j-i
        
        if k+j <= N-1:
            if S[i] != S[j] and S[j] != S[j+k] and S[j+k] != S[i]:
                counter += 1
            

print(allpattern-counter)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
X = [0] * N

Sum1 = 0
Sum2 = 0
Sum3 = 0

for i in range(N):
    if S[i] == "R":
        X[i] = 1
        Sum1 += 1
    elif S[i] == "G":
        X[i] = 2
        Sum2 += 1
    elif S[i] == "B":
        X[i] = 4
        Sum3 += 1

Sum = Sum1 * Sum2 * Sum3

for i in range(N):
    for j in range(1, N):
        if i - j < 0 or N <= i + j:
            break
        else:
            if (X[i] + X[i - j] + X[i + j]) == 7:
                Sum -= 1

print(Sum)

Submission status: Accepted

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

cnt = [[0 for i in range(3)] for i in range(n)]
colors = 'RGB'

for i in range(1, n):
    for j in range(3):
        cnt[i][j] = cnt[i - 1][j]
        if j == colors.find(s[i - 1]):
            cnt[i][j] += 1

ans = 0
for i in range(2, n):
    color = colors.find(s[i])
    ans += cnt[i][(color + 1) % 3] * cnt[i][(color + 2) % 3]

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

print(ans)

Submission status: Accepted

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

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

culmR = []
culmG = []
culmB = []
cR = 0
cG = 0
cB = 0
for i in range(n):
    if s[i] == "R":
        cR += 1
    elif s[i] == "G":
        cG += 1
    else:
        cB += 1
    culmR.append(cR)
    culmG.append(cG)
    culmB.append(cB)
#print(culmR, culmG, culmB)

ans = 0
for i in range(n):
    for j in range(i+1, n):
        #print(i,j)
        if i == j:
            pass
        else:
            count = 0
            #異なる2色を選択したら
            if s[i] != s[j]:
                #差を計算
                diff = j-i
                if "R" not in [s[i], s[j]]:
                    required = "R"
                    #print("R未決定")
                    #累積和の最後とj番目の差
                    count = culmR[-1] - culmR[j]
                    #ただし、diff番目がRの時は除く
                    if (j+diff < n):
                        if s[j+diff] == "R":
                            count -= 1
                    #print("count: ", count)
                elif "G" not in [s[i], s[j]]:
                    required = "G"
                    #print("G未決定")
                    #累積和の最後とj番目の差
                    count = culmG[-1] - culmG[j]
                    #ただし、diff番目がRの時は除く
                    if (j+diff < n):
                        if s[j+diff] == "G":
                            count -= 1
                    #print("count: ", count)
                else:
                    required = "B"
                    #print("B未決定")
                    #累積和の最後とj番目の差
                    count = culmB[-1] - culmB[j]
                    #ただし、diff番目がRの時は除く
                    if (j+diff < n):
                        if s[j+diff] == "B":
                            count -= 1
                    #print("count: ", count)
                ans += count
            #同色ならスキップ
            else:
                pass
print(ans)

Submission status: Accepted

Solution code:
# D
def main():
  #N = 4000
  #S = ["RGB"[random.randint(0, 2)] for _ in range(N)]

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

  import collections

  c = collections.Counter(S)

  total = c["B"]*c["G"]*c["R"]

  for d in range(1, N//2+1):
    for i in range(N-d-d):
      if len({S[i], S[i+d], S[i+d+d]})==3:
        total -= 1
  
  print(total)

main()

Submission status: Accepted

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

r = []
g = []
b = [0] * (n)

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

b_sum = sum(b)
#print(b_sum)
#print(len(r),len(g))
ans = 0

for i in range(len(r)):
    for j in range(len(g)):
        tmp = abs(r[i] -g[j])
        ans += b_sum
        dek = tmp + max(r[i],g[j])
        if dek <= n-1:
            if b[dek] == 1:
                ans -= 1
                
        chi = min(r[i],g[j]) - tmp
        if chi >= 0:
            #print(b[chi-1],chi)
            if b[chi] == 1:
                #print(chi-1,b[chi-1])
                ans -= 1
                
        if tmp % 2 == 0:
            #print(r[i],g[j],int(min(r[i],g[j])-1+tmp/2))
            if b[min(r[i],g[j])+tmp//2] == 1:
                ans -= 1
print(ans)

Submission status: Accepted

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

rlist= []
glist = []
bset = set()
for i,s in enumerate(S):
    if s == "R":
        rlist.append(i)
    elif s=="G":
        glist.append(i)
    else:
        bset.add(i)
blist = [0 for i in range(n)]
cnt = 0
for i,s in enumerate(S[::-1]):
    if s == "B":
        cnt += 1
    blist[n-1-i] = cnt

res = 0
for r in rlist:
    for g in glist:
        res += len(bset)
        l, s = max(r,g), min(r,g)
        possible =  [s-(l-s), l+(l-s)]
        if (l-s) % 2 == 0: possible.append(s+(l-s)//2)
        for x in possible:
            if x in bset:
                res -= 1
print(res)