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=str(input())
t=len(s)
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        dist = abs(ri-gi)
        if ri > gi:
            bi = dist + ri
            if bi in b:
                d -= 1
            bi = gi - dist
            if bi in b:
                d -= 1
        else:
            bi = dist + gi
            if bi in b:
                d -= 1
            bi = ri - dist
            if bi in b:
                d -= 1

        if (ri + gi) % 2 == 0:
            bi = (ri+gi)/2
            if bi in b:
                d -= 1
print(d)

Submission status: Accepted

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

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

Submission status: Accepted

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

X=set()
Y=set()
Z=set()
for i in range(N):
    if S[i]=="R":
        X.add(i)
    elif S[i]=="G":
        Y.add(i)
    else:
        Z.add(i)

E=0
for a in X:
    for b in Y:
        E+=(2*a-b in Z)
        E+=(2*b-a in Z)
        E+=(((a+b)%2==0) and ((a+b)//2 in Z))
                
print(len(X)*len(Y)*len(Z)-E)

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)

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

for i in R:
    for j in G:
        k = int(2 * j - i)
        if(0 <= k < n):
            if(s[k] == 'B'):
                samu -= 1
        k = int(2 * i - j)
        if(0 <= k < n):
            if(s[k] == 'B'):
                samu -= 1
        if(i % 2 == j % 2):
            k = int((i + j) / 2)
            if(s[k] == 'B'):
                samu -= 1

print(samu)

Submission status: Accepted

Solution code:
from itertools import product
from collections import deque
n = int(input())
s = deque(input())
R = []
G = []
B = []
i = 0
while s:
    x = s.popleft()
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i*2)
    i += 1
ans = len(R)*len(G)*len(B)
# print(len(R))
# print(len(G))
# print(len(B))
# print(R)
# print(G)
# print(B)
B = set(B)
S = product(R, G)
for i, j in S:
    if i + j in B:
        ans -= 1
    if 4 * i - 2*j in B:
        ans -= 1
    if 4 * j - 2 * i in B:
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:

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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi, ri) + df
            ll = min(gi, ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri) / 2)

            if rr in bs:
                cnt += 1
            if mm in bs:
                cnt += 1
            if ll in bs:
                cnt += 1

    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Submission status: Accepted

Solution code:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
t=len(s)
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        rg=gi-ri
        if rg+gi in b:
            d-=1
        if  ri-rg in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    result = 1
    if len(list(c.values())) < 3:
        print(0)
        return
    for v in c.values():
        result *= v
    piyo = (N-3) // 2
    for p in range(piyo+1):
        for i in range(N-1 - 2*p -2 + 1):
            a, b, c = S[i] , S[i+p+1] , S[i + (p+1)*2]
            if a != b and a != c and b != c:
                result -= 1
    print(result)
main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
from collections import Counter
c=Counter(s)
t=c['R']*c['G']*c['B']
for i in range(n):
  for j in range(i+1, min(n, (n+1+i)>>1)):
    k = 2*j - i
    if k < n and s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      t-=1
print(t)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    s = str(input())
    cnt = 0
    r = s.count('R')
    g = s.count('G')
    b = s.count('B')

    for i in range(n):
        for j in range(i,n):
            if i == j:
                continue
            k = 2 * j - i
            if k > len(s) - 1:
                continue
            if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
                continue

            cnt +=1

    all = r * g * b
    print(all - cnt) 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, g, b = set(), set(), 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(b)*len(g)
for i in r:
    for j in g:
        chk = j - i
        if chk + j in b:
            ans -= 1
        if i - chk in b:
            ans -= 1
        if (i + j)/2  in b:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import bisect


def count(text, s, rgb_bag):
    ans = 0
    for i in rgb_bag[text[0]]:
        jst = bisect.bisect_left(rgb_bag[text[1]], i)
        if jst == len(rgb_bag[text[1]]):
            continue
        for j in rgb_bag[text[1]][jst:]:
            k = bisect.bisect_left(rgb_bag[text[2]], j)
            ans += len(rgb_bag[text[2]]) - k
            ng_idx = j + (j - i)
            if ng_idx >= len(s):
                continue
            if s[ng_idx] == text[2]:
                ans -= 1
    return ans


def main():
    n = int(input())
    s = input()
    rgb_bag = {'R': [], 'G': [], 'B': []}
    for i, v in enumerate(s):
        rgb_bag[v].append(i)
    ans = 0

    for r in 'RGB':
        for g in 'RGB':
            for b in 'RGB':
                if r == g or g == b or b == r:
                    continue
                ans += count(r+g+b, s, rgb_bag)
    print(ans)


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

print(r*g*b-cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if 2*j - i >= n:
            break
        
        if string[i] != string[j] and string[i] != string[2*j-i] and string[j] != string[2*j-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-2):
    for j in range(i+1,n-1):
        if 2*j-i >= n:
            break
        elif S[i] != S[j] and S[j]!=S[2*j-i] and S[i] != S[2*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 x in range(N-2):
    for y in range(x+1, N-1):
        z = 2 * y - x
        if z >= N:
            break
        if S[x] != S[y] and S[y] != S[z] and S[x] != S[z]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
S = list(input())
C = Counter(S)
equ = 0
for i in range(1, (n - 1) // 2 + 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 + 2 * i] != S[j]:
            equ += 1
print(C['R'] * C['G'] * C['B'] - equ)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    a = [0, 0, 0]
    for i in range(N):
        if S[i] == 'R':
            a[0] += 1
        elif S[i] == 'G':
            a[1] += 1
        else:
            a[2] += 1
    count = a[0] * a[1] * a[2]
    for i in range(N - 1):
        for j in range(1, N):
            if i - j < 0 or N <= i + j:
                break
            if S[i - j] != S[i] and S[i] != S[i + j] and S[i - j] != S[i + j]:
                count -= 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
n = I()
s = list(S())

r = s.count('R')
g = s.count('G')
b = s.count('B')
 
ans = r*g*b
for i in range(n-1):
    for j in range(1,n):
        if i-j<0 or n<=i+j:
            break
        else:
            if s[i-j]!=s[i] and s[i]!=s[i+j] and s[i-j]!=s[i+j]:
                ans-=1
 
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
color = [[], [], []]
for i in range(N):
    if S[i] == 'R':
        color[0].append(i)
    elif S[i] == 'G':
        color[1].append(i)
    elif S[i] == 'B':
        color[2].append(i)
rn = len(color[0])
gn = len(color[1])
bn = len(color[2])
mi = 0
for i in range(N):
    p = 0
    while i + 2*p <= N-1:
        if S[i] != S[i+p] and S[i+p] != S[i+2*p] and S[i] != S[i+2*p]:
           mi += 1
        p += 1
print(rn*gn*bn-mi)

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 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]):
            cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
from itertools import combinations
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i, j in combinations([int(i) for i in range(N)], 2):
        if (i+j)%2 == 0:
            m = (i+j)//2
            if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i in range(N):
        for j in range(i+1, N):
            if (i+j)%2 == 0:
                m = (i+j)//2
                if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                    cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

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-2):
    pl = 1
    while i + pl * 2 < n:
        if s[i] != s[i+pl] and s[i+pl] != s[i+pl*2] and s[i] != s[i+pl*2]:
            cnt -= 1
        pl += 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N-1):
    for j in range(i+1,N):
        k = j + (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:
_input = input
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
#データ読み込み

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

RGB_num=[0]*3

for c in S:
	if c=='R':
		RGB_num[0] += 1
	elif c=='G':
		RGB_num[1] += 1
	else:
		RGB_num[2] += 1

#print(RGB_num)
joken2=0

for i in range(N-2):
	#print("i",i,S[i+1])
	for j in range(min(i+1,N-i-2)):
		if S[i+1] != S[i-j] and S[i+1] != S[i+j+2] and S[i-j] != S[i+j+2]:
			joken2 += 1
			#print(i,j,S[i-j]+S[i+1]+S[i+j+2],"j2")
		#else:
			#print(i,j,S[i-j]+S[i+1]+S[i+j+2])

joken1=RGB_num[0]*RGB_num[1]*RGB_num[2]

print(joken1-joken2)

input = _input

Submission status: Accepted

Solution code:
import sys
import math
import time
import itertools
from collections import defaultdict
import collections
from functools import reduce

def ep(*params):
    print(*params,file=sys.stderr)

start = time.time()

r = 0
g = 0
b = 0

ans = 0
total = 0
N = int(input())
inp = input().rstrip()
for i in range(N):
    c = inp[i]
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    elif c == 'B':
        b += 1
    else:
        assert False
total = r*g*b
ep(r,g,b,total)
ep(time.time() - start)
ans = total
for i in range(0,N-2):
    for j in range(i+1, (N+i+1)//2):
        k = j+j-i
        if inp[i] != inp[j] and inp[j] != inp[k] and inp[k] != inp[i]:
            ans -= 1
ep(time.time() - start)
ep(total, ans)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(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 and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
dict={}
dict["R"]=[]
dict["G"]=[]
dict["B"]=[]
for i in range(n):
  if s[i]=="R":
    dict["R"].append(i)
  elif s[i]=="G":
    dict["G"].append(i)
  elif s[i]=="B":
    dict["B"].append(i)
  else:
    raise Exception
res=len(dict["R"])*len(dict["G"])*len(dict["B"])
for r in dict["R"]:
  for g in dict["G"]:
      if (r+g)%2==0:
        if s[(r+g)//2]=="B":
          res-=1
      try:
        if s[2*r-g]=="B" and 2*r-g>=0:
          	res-=1
      except:pass
      try:
        if s[2*g-r]=="B" and 2*g-r>=0:
          res-=1
      except:pass      
      
print(res)

Submission status: Accepted

Solution code:
n = int(input())
colors = list(input())
index = [[],[],[]]
ans = 0

for i in range(n):
    if colors[i] == 'R':
        index[0].append(i)
    elif colors[i] == 'G':
        index[1].append(i)
    else:
        index[2].append(i)

ans = len(index[0]) * len(index[1]) * len(index[2])

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

print(ans)

Submission status: Accepted

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

r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == "R":
        r += 1
        s[i] = 1
    elif s[i] == "G":
        g += 1
        s[i] = 2
    else:
        b += 1
        s[i] = 4

cnt = r*g*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] + s[k] == 7:
                cnt -= 1

print(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 = j + 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())
SS = input()

S = [0] * (2 * N + 1)
for i in range(N):
    if SS[i] == "R":
        S[i] = 1

    if SS[i] == "G":
        S[i] = 2

    if SS[i] == "B":
        S[i] = 4

cnt = [[0] * 3 for i in range(N)]
if S[0] == 1:
    cnt[0][0] = 1
elif S[0] == 2:
    cnt[0][1] = 1
else:
    cnt[0][2] = 1


for i in range(1, N):
    cnt[i][0] = cnt[i - 1][0]
    cnt[i][1] = cnt[i - 1][1]
    cnt[i][2] = cnt[i - 1][2]
    if S[i] == 1:
        cnt[i][0] += 1

    if S[i] == 2:
        cnt[i][1] += 1

    if S[i] == 4:
        cnt[i][2] += 1


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

            if to == 3:
                ans += cnt[N - 1][2] - cnt[j][2]
                if S[j + j - i] == 4:
                    ans -= 1

            if to == 5:
                ans += cnt[N - 1][1] - cnt[j][1]
                if S[j + j - i] == 2:
                    ans -= 1

            if to == 6:
                ans += cnt[N - 1][0] - cnt[j][0]
                if S[j + j - i] == 1:
                    ans -= 1


print(ans)

Submission status: Accepted

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

cnt = 0

for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
  else:
    B.append(i)

B = set(B)
b = len(B)  

for i in R:
  for j in G:
    check = 0
    m = min(i, j)
    M = max(i, j)
    r = M - m
    left = m - r
    right = M + r
    if r % 2 == 0:
      center = m + r//2
      if center in B:
        check += 1
    if left in B:
      check += 1
    if right in B:
      check += 1
    cnt += b - check

print(cnt)

Submission status: Accepted

Solution code:

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

cnt = 0

for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
  else:
    B.append(i)

B = set(B)
b = len(B)  

for i in R:
  for j in G:
    check = 0
    m = min(i, j)
    M = max(i, j)
    r = M - m
    left = m - r
    right = M + r
    if r % 2 == 0:
      center = m + r//2
      if center in B:
        check += 1
    if left in B:
      check += 1
    if right in B:
      check += 1
    cnt += b - check

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sum = s.count("R") * s.count("G") * s.count("B")
for i in range(1, n-1):
  for j in range(i+1, n):
    k = 2*j-i-1
    if(k >= n):
      break
    if(len({s[i-1], s[j-1], s[k]}) == 3):
        sum -= 1
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
from collections import deque
C =[deque([]),deque([]),deque([])]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = len(C[0])*len(C[1])*len(C[2])

for i in C[0]:
    for j in C[1]:
        if max(i,j) + abs(j-i)<N and S[max(i,j) + abs(j-i)] == "B":
            count -= 1
        if min(i,j) - abs(j-i)>=0 and S[min(i,j) - abs(j-i)] == "B":
            count -= 1
        if (i + j)%2 == 0 and S[(i + j)//2] == "B":
            count -= 1


print(count)

Submission status: Accepted

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

ans = 0
r,g,b = 0,0,0
for i in range(N):
  if(S[i] == 'R'): r += 1
  elif(S[i] == 'G'): g += 1  
  elif(S[i] == 'B'): b += 1 
  
  # i     j    j+(j-i) : R  G  B  (or)  R  B  G (or) ... である場合 ans -= 1   
  for j in range(i+1,N-1):
     i1,i2,i3 = i,j,j+(j-i)
     if(i3 <= N-1):
       tmp = [S[i1],S[i2],S[i3]]
       tmp.sort()
       tmp = tmp[0]+tmp[1]+tmp[2]
       if(tmp == 'BGR'):
         ans -= 1

ans += r*g*b
print(ans)

Submission status: Accepted

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

r, g, b = set(), set(), set()
for i in range(len(s)):
    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_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in [j + diff_ji, i - diff_ji, (j + i) / 2]:
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)

Submission status: Accepted

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

## 組み合わせの総数
ans = S.count('R') * S.count('B') * S.count('G')

## 除外
## j - i = k - j 
## k = 2*j -i

for i in range(0, N-2):
  for j in range(i+1, N-1):
    
    if 2*j-i >= N:
      break
    elif S[i] != S[j] 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=0,0,0
for ss in s:
  if ss=="R":r+=1
  elif ss=="G":g+=1
  elif ss=="B":b+=1

ans=r*g*b
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j+(j-i)
    if k>=n:break
    if not(s[i]==s[j] or s[j]==s[k] or s[i]==s[k]):
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
R = []
G = []
B = []
cnt = 0

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

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

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

print(ans - cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
R=0
G=1
B=2
chk=[0]*N
lst=[[0 for e in range(3)] for f in range(N)]
if S[N-1]=="R":
 chk[N-1]=0
 lst[N-1][0]+=1
elif S[N-1]=="G":
 chk[N-1]=1
 lst[N-1][1]+=1
else:
 chk[N-1]=2
 lst[N-1][2]+=1
if N>=2:
 for i in range(1,N):
  lst[N-1-i][0]+=lst[N-i][0]
  lst[N-1-i][1]+=lst[N-i][1]
  lst[N-1-i][2]+=lst[N-i][2]
  if S[N-1-i]=="R":
   chk[N-1-i]=0
   lst[N-1-i][0]+=1
  elif S[N-1-i]=="G":
   chk[N-1-i]=1
   lst[N-1-i][1]+=1
  else:
   chk[N-1-i]=2
   lst[N-1-i][2]+=1
ans=0
if N<=2:
 print(0)
else:
 for i in range(N-2):
  for j in range(i+1,N-1):
   if chk[i]!=chk[j]:
    ans+=lst[j+1][3-chk[i]-chk[j]]
    if 2*j-i<=N-1:
     if chk[2*j-i]==3-chk[i]-chk[j]:
      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*b*g
for i in range(n):
  for j in range(i,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()

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

if R==0 or G==0 or B==0:
    print(0)
    exit()

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

print(ans)

Submission status: Accepted

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

Lis=[[] for _ in range(3)]
RGB="RGB"

for i in range(N):
    idx=RGB.index(S[i])
    Lis[idx].append(i+1)

if len(Lis[0])==0 or len(Lis[1])==0 or len(Lis[2])==0:
    print(0)
    exit()

Ng=len(Lis[2])
ans=0
for r in Lis[0]:
    for g in Lis[1]:
        n1=2*r-g
        n2=2*g-r
        n3=(r+g)//2
        Su=Ng
        if N>=n1>0:
            if S[n1-1]=="B":
                Su-=1
        if N>=n2>0:
            if S[n2-1]=="B":
                Su-=1
        if (g-r)%2==0:
            if S[n3-1]=="B":
                Su-=1
        ans+=Su
print(ans)

Submission status: Accepted

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

        S_list = list(S)

        r = 0; g = 0; b = 0

        for s in S_list:
                if s=="R": r += 1
                elif s=="G": g += 1
                else: b += 1

        sub = 0

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

        ans = r*g*b - sub

        print("{}".format(ans))


if __name__=="__main__":
        main()

Submission status: Accepted

Solution code:
def solve():
  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):
      k = 2*j-i
      if k < n and s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
        ans -= 1
  print(ans)
  return 0
 
if __name__ == "__main__":
  solve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = []
G = []
B = [False] * N
for i, c in enumerate(S):
    if c == "R":
        R.append(i)
    elif c == "G":
        G.append(i)
    else:
        B[i] = True
answer = 0
for r in R:
    for g in G:
        hikuyatsu = 0
        u = max(r,g)
        b = min(r,g)
        if b - (u - b) >= 0 and B[b - (u - b)] == True:
            hikuyatsu += 1
        if (u + b) % 2 == 0 and B[(u+b)//2] == True:
            hikuyatsu += 1
        if u + (u - b) < N and B[u + (u - b)] == True:
            hikuyatsu += 1
        answer += (N - len(R) - len(G) - hikuyatsu)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
s=list(input())
sr=set()
sg=set()
sb=set()
for ii in range(n):
    if s[ii]=="R":
        sr.add(ii)
    if s[ii]=="G":
        sg.add(ii)
    if s[ii]=="B":
        sb.add(ii)
l=[len(sr),len(sg),len(sb)]
l.sort()

if l[0]==len(sr):
  s1=sr
  if l[1]==len(sg):
    s2=sg
    s3=sb
  else:
    s2=sb
    s3=sg
elif l[0]==len(sg):
  s1=sg
  if l[1]==len(sr):
    s2=sr
    s3=sb
  else:
    s2=sb
    s3=sr
else:
  s1=sb
  if l[1]==len(sr):
    s2=sr
    s3=sg
  else:
    s2=sg
    s3=sr
  
r=0
for ii in s1:
  for jj in s2:
    r+=len(s3)
    dt=abs(ii-jj)
    if ii+dt in s3:
      r-=1
    if ii-dt in s3:
      r-=1
    if jj+dt in s3:
      r-=1
    if jj-dt in s3:
      r-=1
    if (ii+jj)%2==0:
      if (ii+jj)//2 in s3:
        r-=1
      
print(r)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
p=s.count('R')*s.count('G')*s.count('B')
for j in range(1,n-1):
    for i in range(1,min(j,n-j-1)+1):
        x,y,z=s[j-i],s[j],s[j+i]
        if x!=y and y!=z and z!=x:
            p-=1
print(p)