Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
all = r * g * b

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

Submission status: Accepted

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

r_num = s.count("R")
g_num = s.count("G")
b_num = s.count("B")

ans = r_num * b_num * g_num

for i in range(n):
    for j in range(i+1, n):
        k = 2 * j - i
        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_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[i] != s[k] and s[j]!=s[k]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations_with_replacement

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

rgb = 'RGB'

nr = S.count(rgb[0])
ng = S.count(rgb[1])
nb = N - nr - ng
nlst = np.array([nr, ng, nb])

nmax = np.argmax(nlst)
nmin = np.argmin(nlst)
if nmax == nmin:nmin = 2

minlst, midlst = [], []
maxlst = [0]*N

for i, s in enumerate(S):
    if s==rgb[nmax]:maxlst[i] = 1
    elif s==rgb[nmin]:minlst.append(i)
    else: midlst.append(i)
minlst.sort()
midlst.sort()
      
ans = 0

for midl in midlst:
    for minl in minlst:
        mm = abs(midl - minl)
        mmin = min(minl, midl) - mm
        mmax = max(minl, midl) + mm
        if mmin >= 0:
            if maxlst[mmin]: ans += 1
        if mmax <= N-1:
            if maxlst[mmax]: ans += 1
        if mm % 2 == 0 and maxlst[(minl + midl) // 2]: ans += 1
print(nr * ng * nb - 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):
  for j in range(n-2*i):
    if len(set([s[j],s[j+i],s[j+2*i]]))==3:
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = S.count("R") * S.count("B") * S.count("G")
for i in range(len(S)):
    for j in range(i + 1, len(S)):
        k = j * 2 - i
        if k >= len(S):
            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:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = c["R"]*c["G"]*c["B"]
    less = 0
    for d in range(1, N+1):
        for i in range(N):
            # 差dで三つが異なるなら条件を満たさないので取り除く
            if i+d < N and i+2*d < N and \
                    S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                less += 1
    print(ans - less)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = 1 if len(c.keys()) > 2 else 0
    for v in c.values():
        ans *= v
    less = 0
    for d in range(1, N+1):
        for i in range(N):
            # 差dで三つが異なるなら条件を満たさないので取り除く
            if i+d < N and i+2*d < N and \
                    S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                less += 1
    print(ans - less)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

r, g, b = 0, 0, 0
for c in S:
  if c == "R": r += 1
  if c == "G": g += 1
  if c == "B": b += 1

total = r*g*b

for i in range(N):
  for j in range(i+1, N):
    if S[i] == S[j]: continue

    k = 2*j - i
    if k >= N: continue
    if S[i] == S[k] or S[j] == S[k]: continue
    total -= 1

print(total)

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

Submission status: Accepted

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

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

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

print(r*g*b - cnt)

Submission status: Accepted

Solution code:
import numpy as np
def Main(N, S):
    num = [0]*3
    for c in S:
        if c == 'R':
            num[0] += 1
        elif c == 'G':
            num[1] += 1
        else:
            num[2] += 1
    ans = np.prod(num)
    sub = 0
    i = 0
    j = 1
    while(i-2 < N):
        while(i+2*j < N):
            a = i
            b = i+j
            c = i+2*j
            if S[a] != S[b] and S[a] != S[c] and S[b] != S[c]:
                sub += 1
            j += 1
        i += 1
        j = 1
    return ans - sub
        
    

def main():
    N = int(input())
    S = str(input())
    print(Main(N,S))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(r*g*b - cnt)

Submission status: Accepted

Solution code:
# D - RGB Triplets
	
N = int(input())
S = input()
	
r, g, b = 0, 0, 0
	
for s in S:
	if s == 'R':
		r += 1
	elif s == 'G':
		g += 1
	else:
		b += 1
	
ans = r * g * b
	
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		k = j * 2 - i
			
		if k < N:
			if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
				if j -i == k -j:
					ans -= 1
	
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout
from collections import defaultdict
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()

def brute(S):
	ans = 0
	for i in range(len(S)):
		for j in range(i+1, len(S)):
			for k in range(j+1, len(S)):
				if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
					if k - j != j - i:
						ans += 1
	return ans

def main():
	n = int(rl())
	s = rll()[0]
	cnt = [[0, 0, 0] for _ in range(len(s))]
	d = {"R": 0, "G": 1, "B": 2}
	for i in range(len(s)):
		if i == 0:
			for c in d.keys():
				if s[i] == c:
					cnt[0][d[c]] += 1
					break
		else:
			for j in range(3):
				cnt[i][j] = cnt[i-1][j] 
			for c in d.keys():
				if s[i] == c:
					cnt[i][d[c]] += 1
					break
	ans = 0
	for k in range(3, len(s)):
		color = s[k]
		for j in range(1, k):
			second_color = s[j]
			if second_color == color: continue
			diff = k - j
			third_color = [x for x in ["R", "G", "B"] if x != color and x != second_color][0]
			choices = cnt[j-1][d[third_color]]
			crit_i = (2*j) - k
			if crit_i >= 0 and crit_i < j:
				choices -= 1 if s[crit_i] == third_color else 0
			ans += choices
	stdout.write(str(ans));stdout.write("\n")
	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import bisect

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

	tb_pos = {'BG': None, 'BR': None, 'GR': None}
	for k in tb_pos:
		tb_pos[k] = [0] * (N + 1)
	
	rs = 0
	for i in range(N):
		for j in range(i + 1, N):
			if S[i] == S[j]:
				continue
			d = j - i
			c1, c2 = S[i], S[j]
			if c1 > c2:
				c1, c2 = c2, c1
			word = c1 + c2
			if j + d < N and S[j + d] not in word:
				rs -= 1
			tb_pos[word][j + 1] += 1 

	for i in range(1, N + 1):
		for k in tb_pos:
			tb_pos[k][i] += tb_pos[k][i - 1]
				
	for j in range(2, N):
		word = None
		if S[j] == 'B':
			word = 'GR'
		elif S[j] == 'G':
			word = 'BR'
		else:
			word = 'BG'
		
		rs += tb_pos[word][j + 1]

	print(rs)
	return 0 

main()

Submission status: Accepted

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

to_int = {"R":0, "G":1, "B":2}
cnt = [0] * 3
for char in s:
    cnt[to_int[char]] += 1

ans = 1
for num in cnt:
    ans *= num

for i in range(n):
    for length in range(1, n):
        if i - length < 0 or i + length >= n:
            break
        if len(set([to_int[s[i - j * length]] for j in (-1, 0, 1)])) == 3:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

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

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            continue
        for x, y, z in permutations(lst):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    elif c == "G":
        g += 1
    elif c == "B":
        b += 1

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

print(r*g*b-sub)

Submission status: Accepted

Solution code:
from collections import Counter

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

c=Counter(s)
ans1=c['B']*c['R']*c['G']

lim= n//2 if n%2==1 else n//2-1

for i in range(1,lim+1):
  for j in range(i,n-i):
    right=s[j-i]
    middle=s[j]
    left=s[j+i]
    if right!=middle and middle!=left and left!=right:
      ans1-=1
print(ans1)

Submission status: Accepted

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

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

cnt = 0

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

print(r * b * g - cnt)

Submission status: Accepted

Solution code:


N = int(input())
S = input()
S = S[::-1]

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

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


print(cnt)

Submission status: Accepted

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

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

a = r * g * b

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

print(a)

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=j+(j-i)
    if N-1<k:
      break
 
    if S[j]!=S[i] and S[j]!=S[k] and S[k]!=S[i]:
        ans-=1
        
print(ans)

Submission status: Accepted

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

    R_count = 0
    G_count = 0
    B_count = 0

    for s in S:
        if s == "R":
            R_count += 1
        elif s == "G":
            G_count += 1
        else:
            B_count += 1

    # 第一条件だけを考えたときの組み合わせの総数
    answer = R_count * G_count * B_count

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            # i, j が決まると、第二条件を満たさない k が決まる
            k = 2 * j - i

            # k が文字列の長さを超えたときは、第二条件を満たすことができない
            if k >= N:
                continue

            # 第二条件を満たす i, j, k が、第一条件を満たす場合、
            # 第一条件だけ考えたときの組み合わせの総数から差し引く
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                answer -= 1
            
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

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

r = len(list(filter(lambda x: x == "R", S)))
g = len(list(filter(lambda x: x == "G", S)))
b = len(list(filter(lambda x: x == "B", S)))

# print("r:{}, g:{}, b:{}".format(r, g, b))

ans = r * g * b
RGB = list(itertools.permutations(["R", "G", "B"], 3))

# print(RGB)

for i in range(1, N):
    for j in range(N-i*2):
        if (S[j], S[j+i], S[j+2*i]) in RGB:
            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):
    k=j+(j-i)
    if N-1<k:
      break

    if S[j]!=S[i] 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):
    k=j+(j-i)
    if k<=N-1:
      if S[j]!=S[i] and S[j]!=S[k] and S[k]!=S[i]:
          ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(str, input()))

# print(S)
# sum_number = int((N*(N-1)*(N-2))/6)
# del_number = N - 2

# print(sum_number)
# print(del_number)

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


# for i in S:
#     if i == "R":
#         R += 1
#     elif i == "G":
#         G += 1
#     elif i == "B":
#         B += 1

# print(R)
# print(G)
# print(B)

ans = R * G * B

for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        # print(i)
        # print(j)
        # print(k)
        if k > N-1:
            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 = input()
r = 0
g = 0 
for i in s:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  
c = r*g*(n-r-g)
for j in range(1, n-1):
  for i in range(1, min(j, n - j -1) + 1):
    if (s[j] != s[j + i] ) and (s[j] != s[j - i]) and (s[j - i] != s[j + i]):
      c -= 1
print(c)

Submission status: Accepted

Solution code:
from itertools import product

x1 = int(input())
x2 = input()

R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = {i for i, x in enumerate(x2) if x == "B"}

R = len(R_i)
G = len(G_i)
B = len(B_i)

xs = map(sorted, product(R_i, G_i))

s = 0
for a in xs:
    k = a[1]*2 - a[0]  # 2j-i
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  # 2j-k
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2  # (i+k)/2
    if j in B_i:
        s +=1
print(R*G*B - s)

Submission status: Accepted

Solution code:
from itertools import product

x1 = int(input())
x2 = input()

R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = {i for i, x in enumerate(x2) if x == "B"}

R = len(R_i)
G = len(G_i)
B = len(B_i)

xs = map(sorted, product(R_i, G_i))

s = 0
for a in xs:
    k = a[1]*2 - a[0]  # 2j-i
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  # 2j-k
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2
    if j.is_integer():
        j = int(j)
        if j in B_i:
            s +=1
print(R*G*B - s)

Submission status: Accepted

Solution code:
import sys
A=[a.rstrip() for a in sys.stdin.readlines()]
N=int(A[0])
S="".join(["A",A[1]])

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

rgb = {'R': 0, 'G': 0, 'B': 0}
for i in s:
    rgb[i] += 1

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

print(rgb['R']*rgb['G']*rgb['B'] - c)

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):
    for j in range(i+1, n):
        k = 2*j-i
        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()

num_R = len([c for c in S if c == "R"])
num_G = len([c for c in S if c == "G"])
num_B = len([c for c in S if c == "B"])

total = num_R * num_G * num_B

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

print(total - count)

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):
    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:
import bisect
N=int(input())
S=list(input())
ans=0
R=[]
G=[]
B=[]
for i in range(N):
    if S[i]=="R":
        R.append(i+1)
    elif S[i]=="G":
        G.append(i+1)
    else:
        B.append(i+1)
ans=len(B)*len(R)*len(G)
B.append(10000)
for r in R:
    for g in G:
        left=min(r,g)-abs(r-g)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for i in (left,right,middle):
            if i>0:
                a=bisect.bisect_left(B,i)
                if B[a]==i:
                    ans=ans-1
print(ans)

Submission status: Accepted

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

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

n_r, n_g, n_b = 0, 0, 0
for i in range(N):
    if S[i] == 'R':
        n_r += 1
    elif S[i] == 'G':
        n_g += 1
    else:
        n_b += 1
# print(n_r, n_g, n_b)

n_ignore = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k >= N:
            continue
        cond_first = i < j < k
        cond_second = S[i] != S[j] and S[i] != S[k] and S[j] != S[k]
        if cond_first and cond_second:
            n_ignore += 1

res = n_r * n_g * n_b - n_ignore
print(res)

Submission status: Accepted

Solution code:
# AtCoder
N = int(input())
S = input()
count = 0
r = g = b = 0
for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1

_all = r*g*b
sub = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        k = 2*j-i
        if k >= N or S[k] == S[i] or S[k] == S[j]:
            continue
        sub += 1
print(_all-sub)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n = int(input())
    S = input()
    '''
    r_inds = []
    g_inds = []
    b_inds = []
    for i in range(len(S)):
        if S[i] == "R":
            r_inds.append(i)
        elif S[i] == "G":
            g_inds.append(i)
        else:
            b_inds.append(i)

    total_comb = len(r_inds)*len(g_inds)*len(b_inds)
    ded = 0
    for i in r_inds:
        for j in g_inds:
            s_ind = min(i, j)
            b_ind = max(i, j)
            diff = b_ind - s_ind
            bigger_ind = b_ind + diff
            smaller_ind = s_ind - diff
            if bigger_ind < len(S) and bigger_ind in b_inds:
                ded += 1
            if smaller_ind >= 0 and smaller_ind in b_inds:
                ded += 1
            if diff % 2 == 0:
                mid_diff = diff // 2
                mid_ind = s_ind + mid_diff
                if mid_ind in b_inds:
                    ded += 1
    print(total_comb - ded)
    '''
    color_num = defaultdict(int)
    num_subst = 0

    for i in range(len(S) - 1):
        color_num[S[i]] += 1 
        for j in range(i + 1, len(S)):
            if S[i] != S[j]:
                diff = j - i
                idx = j + diff
                if idx < len(S) and S[idx] != S[i] and S[idx] != S[j]:
                    num_subst += 1
    color_num[S[len(S) - 1]] += 1 
    total_num_comb = color_num["R"] * color_num["G"] * color_num["B"]
    print(total_num_comb - num_subst)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

R = li.count("R")
G = li.count("G")
B = li.count("B")

cnt = 0

for i in range(n-1):
    for j in range(i+1, n):
        if 2*j - i > n-1:
            continue
        if li[i] != li[j]:
            if li[2*j-i] != li[i] and li[2*j-i] != li[j]:
                cnt += 1
print(R*G*B - cnt)

Submission status: Accepted

Solution code:
import sys

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

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

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

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

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

Submission status: Accepted

Solution code:
N = int(input())  #数字
S = list(input()) #スペース抜きの複数行,１文字ずつ

cou = 0

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

print(S.count("R")*S.count("G")*S.count("B")-cou)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_ary = []
g_ary = []
b_ary = []
for i in (s):
    if i == 'R':
        r_ary.append(i)
    elif i == 'G':
        g_ary.append(i)
    elif i == 'B':
        b_ary.append(i)
r = len(r_ary)
g = len(g_ary)
b = len(b_ary)
first_conditions = r*g*b
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            first_conditions -= 1
print(first_conditions)

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    S =     _in[1]   # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 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:
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    cnt -= 1
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    S = input()
    r,g,b = "R","G","B"

    def get_other(x,y):
        if x in [r,g] and y in [r,g]:
            return b
        if x in [r,b] and y in [r,b]:
            return g
        if x in [g,b] and y in [g,b]:
            return r

    rs = [0] * (N+1)
    gs = [0] * (N+1)
    bs = [0] * (N+1)

    for i in range(N):
        rs[i+1] = ((S[i] == r) + 0) + rs[i]
        gs[i+1] = ((S[i] == g) + 0) + gs[i]
        bs[i+1] = ((S[i] == b) + 0) + bs[i]

    s_dic = {r:rs,b:bs,g:gs}

    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            if S[i] != S[j]:
                not_k = 2 * j - i
                other = get_other(S[i],S[j])

                counter = s_dic[other]

                ans += counter[N] - counter[j+1]
                if not_k < len(S) and S[not_k] == other:
                    ans -= 1

    print(ans)



if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 初期入力
N= int(input())
S =input()


#
R_index=[]
G_index=[]
B_index=[]
for i,v in enumerate(S):
    if v =="R":
        R_index.append(i)
    if v =="G":
        G_index.append(i)
    if v =="B":
        B_index.append(i)

# 例　R_index各要素から等距離離れたところに左にG、右にBがあればOK
R_B =[]
B_G =[]
count_ij_jk_onaji =0
for r in R_index:
    for g in G_index:
        if 0<=g-(r-g) and g-(r-g)<N  :
            #if S[g-(r-g)]=="B" and r<g:
            if S[g-(r-g)]=="B" :
                count_ij_jk_onaji +=1
               # print(r,g,g-(r-g),"B")
for b in B_index:
    for r in R_index:
        if 0<=r-(b-r) and r-(b-r)<N :
            #if S[r-(b-r)] == "G" and b<r:
            if S[r-(b-r)] == "G" :
                count_ij_jk_onaji +=1
                #print(r,b,r-(b-r),"G")
for g in G_index:
    for b in B_index:
        if 0<=b-(g-b) and b-(g-b)<N :
            #if S[b-(g-b)] =="R" and g<b:
            if S[b-(g-b)] =="R":
                count_ij_jk_onaji +=1
                #print(b,g,b-(g-b),"R")

            
#3つとも異なる文字=all
kotae_all = len(R_index) * len(G_index) * len(B_index) 
print(kotae_all -count_ij_jk_onaji)

Submission status: Accepted

Solution code:
def main():
  N=int(input())
  S=[1 if x=='R' else (2 if x=='G' else 3) for x in [*input()]]

  t=S.count(1)*S.count(2)*S.count(3)
  for i in range(N-2):
    j=1
    while i+2*j<N:
      if S[i]*S[i+j]*S[i+2*j]==6:
        t-=1
      j+=1

  print(t)

main()

Submission status: Accepted

Solution code:
N=int(input())
S=[1 if x=='R' else (2 if x=='G' else 3) for x in [*input()]]

t=S.count(1)*S.count(2)*S.count(3)
for i in range(N-2):
  j=1
  while i+2*j<N:
    if S[i]*S[i+j]*S[i+2*j]==6:
      t-=1
    j+=1

print(t)

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["R"]
    g = c["G"]
    b = c["B"]

    all_num = r*g*b
    sub = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]: continue
            k = j + (j - i)
            if k >= N or S[k] == S[i] or S[k] == S[j]: continue
            sub += 1
    print(all_num - sub)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    r = S.count("R")
    g = S.count("G")
    b = N-r-g

    all_num = r*g*b
    sub = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]: continue
            k = j + (j - i)
            if k >= N or S[k] == S[i] or S[k] == S[j]: continue
            sub += 1
    print(all_num - sub)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
##RGBからひとつづつ
##かつ
from collections import Counter

n=int(input())
s = input()
sc= Counter(list(s))
ans= 0
try:
    ans =  sc["R"]*sc["G"]*sc["B"]
except:
    print(0)
    exit()

for j in range(n):
    for i in range(j):
        if 2*j-i<n:
            flag = True
            for c in {"R","G","B"}:
                if c not in {s[i],s[j],s[2*j-i]}:
                    flag = False
            if flag:
                ans-=1
                

print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = str(input())
r = {}
b = {}
g = {}
for i in range(len(s)):
    if s[i] == 'R':
        r[i+1] = s[i]
    elif s[i] == 'B':
        b[i+1] = s[i]
    else:
        g[i+1] = s[i]
ans = len(r) * len(b) * len(g)
for i in r.keys():
    for j in b.keys():
        diff = abs(j-i)
        minb = min(i,j)-diff
        maxb = max(i,j)+diff
        if maxb in g.keys():
            ans -= 1
        if minb in g.keys():
            ans -= 1
        if((i+j)%2==0):
            if((i+j)//2 in g.keys()):
                ans -=1
print(ans)

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    mx = S.count('R') * S.count('G') * S.count('B')
    for i in range(N):
        for j in range(i + 1, N):
            k = j + j - i
            if k < N:
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    mx -= 1

    print(mx)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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-2):
    for j in range(i+1,n-1):
      k = j + j - i
      if k >= n:
        break
      elif s[i] != s[j] and s[i] != s[k] and s[k] != s[j]:
        ans -=1
  print(ans)
main()

Submission status: Accepted

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


"""
脳死で全探索で探すとO(n^3）で時間がかかる。
→1つめの条件で予め絞った後に、そこからさらに絞り込むやり方をやる。（補集合を引くパターン）
j-i=k-jを満たすもの（すなわちk=2j-i）を求めて、あとで引き算すればOK.
≠は＝で考えて引き算のほうがよい。
"""

r_count = S.count('R')
g_count = S.count('G')
b_count = S.count('B')


val = 0

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

print((r_count * g_count * b_count) - val)

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+1,n-1):
        k=2*j-i
        if k<n:
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                ans-=1

print(ans)