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 sys
def input(): return sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    sub_sum = {key : [0]*N for key in "RGB"}
    
    for i, key in enumerate(S):
        sub_sum[key][i] = 1
    
    for i in range(1, N):
        for key in "RGB":
            sub_sum[key][i] += sub_sum[key][i-1]

    ans = 0
    for i in range(1, N):
        now = S[i]
        right_nums = {key:sub_sum[key][N-1]-sub_sum[key][i-1] for key in "RGB"}
        for j in range(i):
            left = S[j]
            right = [c for c in "RGB" if  c not in [now, left]][0]
            right_num = right_nums[right]
            if now != left:
                if 2*i - j < N and right_num:
                    if S[2*i - j] == right:
                        right_num -= 1
                ans += right_num
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

n = int(input())
s = input()
r = []
b = []
g = []
for i,c in enumerate(s):
    if c == 'R':
        r.append(i)
    elif c == 'B':
        b.append(i)
    else:
        g.append(i)
g_leng = len(g)
r_leng = len(r)
b_leng = len(b)
ans = g_leng*r_leng*b_leng
subans = 0
for j in b:
    for i in r:
        g_ind = bisect.bisect_left(g, 2*j-i)
        if g_ind != g_leng and g[g_ind] == 2*j-i:
            subans+=1
        g_ind = bisect.bisect_left(g, 2*i-j)
        if g_ind != g_leng and g[g_ind] == 2*i-j:
            subans+=1
        g_ind = bisect.bisect_left(g, (i + j) // 2)
        if g_ind != g_leng and g[g_ind] == (i + j) / 2:
            subans += 1
print(ans-subans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
S = list(input())
R, G, B = [], [], []
for i in range(n):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)
r = len(R)
g = len(G)
b = len(B)
cnt = 0
Rset, Gset, Bset = set(R), set(G), set(B)
# RGB
for i in range(r):
    g0 = bisect.bisect_right(G, R[i])
    for j in range(g0, g):
        b0 = bisect.bisect_right(B, G[j])
        cnt += b - b0
        x = 2*G[j]-R[i]
        if x in Bset:
            cnt -= 1
# RBG
for i in range(r):
    b0 = bisect.bisect_right(B, R[i])
    for j in range(b0, b):
        g0 = bisect.bisect_right(G, B[j])
        cnt += g - g0
        x = 2*B[j]-R[i]
        if x in Gset:
            cnt -= 1
# GBR
for i in range(g):
    b0 = bisect.bisect_right(B, G[i])
    for j in range(b0, b):
        r0 = bisect.bisect_right(R, B[j])
        cnt += r - r0
        x = 2*B[j]-G[i]
        if x in Rset:
            cnt -= 1
# GRB
for i in range(g):
    r0 = bisect.bisect_right(R, G[i])
    for j in range(r0, r):
        b0 = bisect.bisect_right(B, R[j])
        cnt += b - b0
        x = 2*R[j]-G[i]
        if x in Bset:
            cnt -= 1
# BRG
for i in range(b):
    r0 = bisect.bisect_right(R, B[i])
    for j in range(r0, r):
        g0 = bisect.bisect_right(G, R[j])
        cnt += g - g0
        x = 2*R[j]-B[i]
        if x in Gset:
            cnt -= 1
# BGR
for i in range(b):
    g0 = bisect.bisect_right(G, B[i])
    for j in range(g0, g):
        r0 = bisect.bisect_right(R, G[j])
        cnt += r - r0
        x = 2*G[j]-B[i]
        if x in Rset:
            cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
def main():
    import math
    import sys
    from collections import defaultdict

    n = int(input())
    s = str(input())
    dic1 = defaultdict(list)

    for i in range(len(s)):
        dic1[s[i]] += [i]

    c = 0
    co = len(dic1["B"])

    for a in dic1["R"]:
        for b in dic1["G"]:
            f = 0
            t = abs(a-b)
            ma = max(a,b) + t
            mi = min(a,b) - t
            if abs(a + b) % 2 == 0:
                if s[abs(a + b) // 2] == "B":
                    f += 1
            if ma <= len(s) - 1:
                if s[ma] =="B":
                    f += 1
            if mi >= 0:
                if s[mi] == "B":
                    f += 1
            c += (co - f)

    print(c)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

if __name__ == '__main__':
    N = int(input())
    S = input()

    cntR_from_r = [0 for _ in range(N)]
    cntG_from_r = [0 for _ in range(N)]
    cntB_from_r = [0 for _ in range(N)]

    for i in range(N - 1, 0, -1):
        if S[i] == 'R':
            cntR_from_r[i - 1] = cntR_from_r[i] + 1
            cntG_from_r[i - 1] = cntG_from_r[i]
            cntB_from_r[i - 1] = cntB_from_r[i]
        elif S[i] == 'G':
            cntR_from_r[i - 1] = cntR_from_r[i]
            cntG_from_r[i - 1] = cntG_from_r[i] + 1
            cntB_from_r[i - 1] = cntB_from_r[i]
        else:
            cntR_from_r[i - 1] = cntR_from_r[i]
            cntG_from_r[i - 1] = cntG_from_r[i]
            cntB_from_r[i - 1] = cntB_from_r[i] + 1

    ans = 0
    for i in range(N):
        if S[i] == 'R':
            val1 = cntG_from_r[i] * cntB_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j

                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'G' and S[j2] == 'B') or (S[j1] == 'B' and S[j2] == 'G'):
                        val1 -= 1
            ans += val1
        elif S[i] == 'G':
            val1 = cntR_from_r[i] * cntB_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j
                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'R' and S[j2] == 'B') or (S[j1] == 'B' and S[j2] == 'R'):
                        val1 -= 1
            ans += val1
        else:
            val1 = cntG_from_r[i] * cntR_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j
                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'R' and S[j2] == 'G') or (S[j1] == 'G' and S[j2] == 'R'):
                        val1 -= 1
            ans += val1
    print(ans)

Submission status: Accepted

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

rc = S.count("R")
gc = S.count("G")

tc = int(rc*gc*(N-rc-gc))

aa = 0
for i in range(N):
  for j in range(1,math.ceil((N-i)/2)+1):
    
    if i+j+j < N:
      if S[i] != S[i+j]:
        if S[i] != S[i+j+j]:
          if S[i+j] != S[i+j+j]:
            aa = int(aa + 1)
            
print(int(tc-aa))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    ss = list(sorted(list(set(s))))
    s_sorted = list(sorted(s))
    if len(ss) < 3:
        print(0)
        return
    m = n-1
    sn = [0]*3
    j = 0
    flag = False
    for i, si in enumerate(ss):
        while s_sorted[j] == si:
            sn[i] += 1
            if j == m:
                flag = True
                break
            j += 1
        if flag:
            break
    ans = sn[0]*sn[1]*sn[2]
    for d in range(1, n//2+1):
        i = 0
        d2 = 2*d
        ma = n-d2
        while i < ma:
            si, sj, sk = s[i], s[i+d], s[i+d2]
            if si != sj and sj != sk and si != sk:
                ans -= 1
            i += 1
    print(ans)
                

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
N = int(input())
S = input()
r = 0
g = 0
b = 0
T = list()
for i in range(N):
    if(S[i] == "R"):
        r += 1
        T.append(1)
    elif(S[i] == "G"):
        g += 1
        T.append(2)
    elif(S[i] == "B"):
        b += 1
        T.append(3)
total = r*g*b
for j in range(1, N-1):
    for n in range(1, min(N-1-j,j)+1):
        if (T[j]*T[j-n]*T[j+n] == 6):
            total -= 1
print(total)

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()
aint = lambda: int(input())

n = aint()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
a = 0
if n<4:
    print(0)
else:
    for i in range(len(s)-2):
        for j in range(int(0.5*(len(s)-i-1))):
            if s[i]!=s[i+j+1] and s[i+j+1]!=s[i+2*j+2] and s[i]!=s[i+2*j+2]:
                a += 1
    print(r*g*b-a)

Submission status: Accepted

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

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

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



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

ans = 0
for i in range(n):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ng = j + (j - i) + 1
        if s[i] == "R" and s[j] == "G":
            l = b
        elif s[i] == "R" and s[j] == "B":
            l = g
            #c = cg
        elif s[i] == "G" and s[j] == "R":
            l = b
            #c = cb
        elif s[i] == "G" and s[j] == "B":
            l = r
            #c = cr
        elif s[i] == "B" and s[j] == "R":
            l = g
            #c = cg
        elif s[i] == "B" and s[j] == "G":
            l = r
            #c = cr
        ans += l[n] - l[j + 1]
        if ng < n + 1:
            ans -= l[ng] - l[ng - 1]
        #print(ans)

print(ans)

Submission status: Accepted

Solution code:


n = 0
s = ""

def format_input(filename = None):
	global n
	global s
	if filename == None:
		n = int(input())
		s = str(input())

	elif filename == '__random__':
		from random import randint as rng
		from random import choices
		n = rng(1, 4 * 10**3)
		s = ''.join(choices('RGB', k = n))
		print(n)
		print(s)

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

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

	for l in r:
		for m in g:
			if (l + m) % 2 == 0:
				if s[(l + m) // 2] == 'B':
					answer -= 1
			if max(l, m) + abs(l - m) < n:
				if s[max(l, m) + abs(l - m)] == 'B':
					answer -= 1
			if min(l, m) - abs(l - m) >= 0:
				if s[min(l, m) - abs(l - m)] == 'B':
					answer -= 1
	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    s = input()
    rlist = []
    glist = []
    blist = []
    for i in range(n):
        if s[i] == "R":
            rlist.append(i)
        elif s[i] == "G":
            glist.append(i)
        else:
            blist.append(i)
    blist = set(blist)
    ans = 0
    for i in range(len(rlist)):

        for j in range(len(glist)):
            r = rlist[i]
            ng = 0
            g = glist[j]
            s = sorted([r, g])
            r = s[0]
            g = s[1]
            if (r-(g-r)) in blist:
                ng += 1
            if (g+(g-r)) in blist:
                ng += 1
            if (r+g) % 2 == 0 and (r+g)//2 in blist:
                ng += 1
            ans += len(blist)-ng
    print(ans)
resolve()

Submission status: Accepted

Solution code:
n=int(input())

ans=0
s=list(str(input()))
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)
count=0
aida=1

while aida*2-1<=n:
    for i in range(n-aida*2):
        #print("DF")
       # print(s[i],i,s[i+aida],i+aida,s[i+aida*2],i+aida*2)
        aa=set()
        aa.add(s[i])
        aa.add(s[i+aida])
        aa.add(s[i+aida*2])
        #print(aa)
        if len(aa)==3:
            count+=1
    aida+=1
#print(count)

print(len(r)*len(b)*len(g)-count)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
S = input()
rgb = {"R":0, "G":0, "B":0}
RGBs = 0
for s in S:
    rgb[s] += 1

for i in range(N):
    tail = (N - 1 - i) // 2
    for d in range(1, tail + 1):
        Si = S[i]
        Sj = S[i + d]
        Sk = S[i + d * 2]
        if Si == Sj or Sj == Sk or Sk == Si:
            continue
        RGBs += 1

ans = rgb["R"] * rgb["G"] * rgb["B"] - RGBs
print(ans)

Submission status: Accepted

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

ans=r*g*b

for j in range(1,N-1):
    for d in range(1,min(j,N-j-1)+1):
        if S[j-d]!= S[j] and S[j]!= S[j+d] and S[j-d]!= S[j+d]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    s = IS()
    cnt = 0
    rgb = {'R': [], 'G': [], 'B': []}
    for i in range(n):
        rgb[s[i]].append(i)
    dd = 0
    for i in range(n-1):
        for k in range(i+2, n):
            if (i+k)%2==0:
                j = (i+k)//2
                if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                    dd += 1
    all = len(rgb['R']) * len(rgb['G']) * len(rgb['B'])
    ans = all - dd
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

bad_list = ['RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR']
RGB = {'R': 0, 'G': 0, 'B': 0}
other_map = {'R': 'GB', 'G': 'RB', 'B': 'RG'}
res = 0
for (i, s) in enumerate(S):
  other = other_map[s]
  res += RGB[other[0]] * RGB[other[1]]
  
  for j in range(((i+1)//2)+1)[1:]:
    if S[i::-j][:3] in bad_list:
      res -= 1

  RGB[s] += 1


print(res)

Submission status: Accepted

Solution code:
from collections import Counter
from math import ceil
n = int(input())
s = input()

if n <= 2:
    ans = 0
else:
    c = list(Counter(s).values())
    if len(c) <= 2:
        ans = 0
    else:
        ans = 1
        for i in c:
            ans *= i
            
        for i in range(ceil(n/2)-1):
            for j in range(n-(i+1)*2):
                if s[j] != s[j+i+1] and s[j] != s[j+(i+1)*2] and s[j+(i+1)*2] != s[j+i+1]:
                    ans -= 1
            
print(ans)

Submission status: Accepted

Solution code:
import collections
import functools
import operator



n=int(input())
s=input()
c=collections.Counter(s)
if len(c)<=2:
    print(0)
    exit()
# l=r*g*b
l = functools.reduce(operator.mul, c.values())
for i in range(0,n-1):
    for j in range(i+1,n):
        k=j-i+j
        if k>=n:
            continue
        if s[i]!=s[j] and s[k]!=s[j] and s[i]!=s[k]:
            l-=1
print(l)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i, item in enumerate(s):
    if item=="R":
        r.append(i)
    elif item=="G":
        g.append(i)
    else:
        b.append(i)
if (not r) or (not g) or (not b):
    print(0)
else:
    count = 0
    for low,mid,high in [(r,g,b), (r,b,g), (g,r,b), (g,b,r), (b,r,g), (b,g,r)]:
        for m in mid:
            ind = len(high) - 1
            for l in low:
                if l >= m:
                    continue
                while (l+high[ind] > m*2) and (ind>0):
                    ind -= 1
                if m < high[ind]:
                    if l+high[ind] == m*2:
                        count += 1
    print(len(r)*len(g)*len(b) - count)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
b=[]
b_num,r_num,g_num=0,0,0
r=[]
g=[]
for i in range(n):
    if s[i]=="R":
        r_num+=1
    elif s[i]=="B":
        b_num+=1
    else:
        g_num+=1
    b.append(b_num)
    g.append(g_num)
    r.append(r_num)
def c(al,j):
    global r,g,b,r_num,g_num,b_num
    if al=="R":
        return r_num-r[j]
    elif al=="B":
        return b_num-b[j]
    else:
        return g_num-g[j]
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if j<=i:
            continue
        if s[i]==s[j]:
            continue
        a=[]
        a.append(s[i])
        a.append(s[j])
        #print(s[i]+s[j])
        sa=j-i
        if n-1<j+sa:
            if not "B" in a:
                ans+=(b_num-b[j])
            elif not "R" in a:
                ans+=(r_num-r[j])
            else:
                ans+=(g_num-g[j])
            continue
        else:
            if s[j+sa] in a: #入ってないやつを返す
                if not "B" in a:
                    ans+=(b_num-b[j])
                elif not "R" in a:
                    ans+=(r_num-r[j])
                else:
                    ans+=(g_num-g[j])
            else:
                ans+=c(s[j+sa],j)-1
print(ans)
#print(g)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
s = input()
rgb = [[],[],[]]
for i in range(n):
  if s[i] == 'R':
    rgb[0].append(i)
  elif s[i] == 'G':
    rgb[1].append(i)
  else:
    rgb[2].append(i)
x = []
for i in range(3):
  x.append((len(rgb[i]),i))
x.sort()
y = []
for i in range(3):
  y.append(rgb[x[i][1]])
a = 0
for y0 in y[0]:
  for y1 in y[1]:
    b = (y0+y1)//2
    if abs(b-y0) == abs(b-y1):
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
    b = min(y0,y1)-abs(y0-y1)
    if b >= 0:
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
    b = max(y0,y1)+abs(y0-y1)
    if b < n:
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
print(len(y[0])*len(y[1])*len(y[2])-a)

Submission status: Accepted

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions


N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()
r = S.count('R')
g = S.count('G')
b = S.count('B')
t = r * g * b
for el in range(N):
    end = N - 1 - el
    tt = min([el, end])
    for el2 in range(1, tt+1):
        if S[el - el2] != S[el] and S[el + el2] != S[el] and S[el - el2] != S[el + el2]:
            t -= 1
print(t)

Submission status: Accepted

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

NR = [-1 for _ in range(N)]
NG = [-1 for _ in range(N)]
NB = [-1 for _ in range(N)]

R_pos = []
G_pos = []
B_pos = []

r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == 'R':
        r += 1
        R_pos.append(i)
    elif S[i] == 'G':
        g += 1
        G_pos.append(i)
    else:
        b += 1
        B_pos.append(i)

    NR[i] = r
    NG[i] = g
    NB[i] = b

ans = 0
for i_ in R_pos:
    for j_ in G_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NB[N - 1] - NB[j]
        if j + (j - i) < N:
            ans -= NB[j + (j - i)] - NB[j + (j - i) - 1]

    for j_ in B_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NG[N - 1] - NG[j]
        if j + (j - i) < N:
            ans -= NG[j + (j - i)] - NG[j + (j - i) - 1]

for i_ in G_pos:
    for j_ in B_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NR[N - 1] - NR[j]
        if j + (j - i) < N:
            ans -= NR[j + (j - i)] - NR[j + (j - i) - 1]

print(ans)

Submission status: Accepted

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

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

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

ans = r*g*b - rgb # 全部の可能性から同じ距離のものを引く
print(ans)

Submission status: Accepted

Solution code:
# map(int, input().split())
# list(map(int, input().split()))
# for _ in range(int(input())):

n = int(input())

def check(i, j, col, occ):
    c = occ[n-1]-occ[j];
    if (j+j-i<=n-1 and rgb[j+(j-i)]==col): 
        c-=1
    return c

rgb = list(input())

red, blue, green = [0]*n,[0]*n,[0]*n 

if (rgb[0]=='R'): 
    red[0]+=1;
elif (rgb[0]=='B'): 
    blue[0]+=1;
elif (rgb[0]=='G'): 
    green[0]+=1;
for i in range(1,n):
    red[i] = red[i-1];
    blue[i] = blue[i-1];
    green[i] = green[i-1];
    if (rgb[i]=='R'): red[i]+=1;
    elif (rgb[i]=='B'): blue[i]+=1;
    elif (rgb[i]=='G'): green[i]+=1;


ans = 0;
for i in range(0,n-2) :
    for j in range(i+1,n-1) :
        if (rgb[j]==rgb[i]): 
            continue
        if ('G' != rgb[i] and 'G' != rgb[j]): 
            ans += check(i,j,'G',green);
        elif ('B' != rgb[i] and 'B' != rgb[j]): 
            ans += check(i,j,'B',blue);
        elif ('R' != rgb[i] and 'R' != rgb[j]):
            ans += check(i,j,'R',red);

print(ans)

Submission status: Accepted

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

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

hiku = 0;
for j in range(2,n) :
    if j-1 < n-j :
        for i in range(1,j) :
            if (not(s[j-i-1]==s[j-1])) and (not(s[j-i-1]==s[j+i-1])) and (not(s[j-1]==s[j+i-1])) :
                hiku += 1;
    else :
        for i in range(1,n-j+1) :
            if (not(s[j-i-1]==s[j-1])) and (not(s[j-i-1]==s[j+i-1])) and (not(s[j-1]==s[j+i-1])) :
                hiku += 1;

print(len(r)*len(g)*len(b)-hiku);

Submission status: Accepted

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


def solve(N: int, S: str):
    r = 0
    g = 0
    b = 0
    sub = 0
    for i in range(N):
        if S[i] == "R":
            r += 1
            for j in range(i + 1, N):
                if S[j] == "G":
                    if j * 2 - i < N and S[j * 2 - i] == "B":
                        sub += 1
                elif S[j] == "B":
                    if j * 2 - i < N and S[j * 2 - i] == "G":
                        sub += 1
        elif S[i] == "G":
            g += 1
            for j in range(i + 1, N):
                if S[j] == "R":
                    if j * 2 - i < N and S[j * 2 - i] == "B":
                        sub += 1
                elif S[j] == "B":
                    if j * 2 - i < N and S[j * 2 - i] == "R":
                        sub += 1
        else:
            b += 1
            for j in range(i + 1, N):
                if S[j] == "R":
                    if j * 2 - i < N and S[j * 2 - i] == "G":
                        sub += 1
                elif S[j] == "G":
                    if j * 2 - i < N and S[j * 2 - i] == "R":
                        sub += 1

    print(r * g * b - sub)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s_list = list(s)
result = s_list.count("R") * s_list.count("G") *s_list.count("B") 
for i in range(1,n):
  left = s_list[:i]
  right = s_list[i+1:]
  for j in range(min(len(left),len(right))):
    if s[i] != s[i-j-1] and s[i] != s[i + j + 1] and s[i-j-1] != s[i + j + 1]:
      result -= 1
    
print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = [0] * n
g = [0] * n
b = [0] * n
rcnt = 0
gcnt = 0
bcnt = 0
for i in range(n):
    r[i] += r[i-1]
    g[i] += g[i-1]
    b[i] += b[i-1]
    if s[i] == 'R':
        r[i] += 1
        rcnt += 1
    if s[i] == 'G':
        g[i] += 1
        gcnt += 1
    if s[i] == 'B':
        b[i] += 1
        bcnt += 1
#print(r,g,b)
ans = 0
for i in range(n-1):
    for j in range(i+1,n-1):
        if s[i] == 'R':
            if s[j] == 'G':
                ans += b[-1] - b[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'B':
                        ans -= 1
            if s[j] == 'B':
                ans += g[-1] - g[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'G':
                        ans -= 1
        if s[i] == 'G':
            if s[j] == 'R':
                ans += b[-1] - b[j]
                if j + (j - i) < n:
                    if s[j + (j-i)] == 'B':
                        ans -= 1
            if s[j] == 'B':
                ans += r[-1] - r[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'R':
                        ans -= 1
        if s[i] == 'B':
            if s[j] == 'R':
                ans += g[-1] - g[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'G':
                        ans -= 1
            if s[j] == 'G':
                ans += r[-1] - r[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'R':
                        ans -= 1
print(ans)

Submission status: Accepted

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

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

res = 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 S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            res -= 1

print(res)

Submission status: Accepted

Solution code:
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] = r[i - 1] + 1
        g[i] = g[i - 1]
        b[i] = b[i - 1]
    elif s[i] == "G":
        r[i] = r[i - 1]
        g[i] = g[i - 1] + 1
        b[i] = b[i - 1]
    else:
        r[i] = r[i - 1]
        g[i] = g[i - 1]
        b[i] = b[i - 1] + 1
ans=0
for k in range(n-1,-1,-1):
    for j in range(k - 1, -1, -1):
        if s[k]==s[j]:continue
        i = {"R", "G", "B"} - {s[k], s[j]}
        if "R" in i:
            ans += r[j]
        elif "G" in i:
            ans += g[j]
        else:
            ans += b[j]
        if 2 * j - k >= 0 and s[2 * j - k] in i:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    R, G, B = [0] * (N + 1), [0] * (N + 1), [0] * (N + 1)
    for i in range(N - 1, -1, -1):
        R[i], G[i], B[i] = R[i + 1], G[i + 1], B[i + 1]
        if S[i] == 'R':
            R[i] += 1
        elif S[i] == 'G':
            G[i] += 1
        else:
            B[i] += 1
    ans = 0
    for i in range(N):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            if j + (j - i) + 1 < N + 1:
                if S[i] + S[j] == 'RG':
                    ans += B[j + 1]
                    if B[j + (j - i) + 1] != B[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'GR':
                    ans += B[j + 1]
                    if B[j + (j - i) + 1] != B[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'RB':
                    ans += G[j + 1]
                    if G[j + (j - i) + 1] != G[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'BR':
                    ans += G[j + 1]
                    if G[j + (j - i) + 1] != G[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'GB':
                    ans += R[j + 1]
                    if R[j + (j - i) + 1] != R[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'BG':
                    ans += R[j + 1]
                    if R[j + (j - i) + 1] != R[j + (j - i)]:
                        ans -= 1
            else:
                if S[i] + S[j] == 'RG':
                    ans += B[j + 1]
                elif S[i] + S[j] == 'GR':
                    ans += B[j + 1]
                elif S[i] + S[j] == 'RB':
                    ans += G[j + 1]
                elif S[i] + S[j] == 'BR':
                    ans += G[j + 1]
                elif S[i] + S[j] == 'GB':
                    ans += R[j + 1]
                elif S[i] + S[j] == 'BG':
                    ans += R[j + 1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
'''


39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB


'''

l=[]
q=[]
w=[]
n=int(input())
s=input().rstrip()
x=list(s)
for i in range(0,n):
    if x[i]=='R':
        l.append(i)
    elif x[i]=='G':
        q.append(i);
    else:
        w.append(i);
total = 0;
for i in range(0,len(l)):
    for j in range(0,len(q)):
        A=abs(l[i]-q[j]);
        H=[]
        H.append(l[i])
        H.append(q[j]);
        H.sort();
        B=H[0]-A;
        C=H[1]+A;
        D=-1;
        if A%2==0:
            D=H[0]+(A//2);
        I=0;
        J=len(w)-1;
        V=0;
        while(I<=J):
            m=I+(J-I)//2;
            if w[m]==B:
                V+=1;
                break;
            elif w[m]>B:
                J=m-1;
            else:
                I=m+1;
        B=C;
        I=0;
        J=len(w)-1
        while(I<=J):
            m=I+(J-I)//2;
            if w[m]==B:
                V+=1;
                break;
            elif w[m]>B:
                J=m-1;
            else:
                I=m+1;
        if D!=-1:
            B=D;
            I=0;
            J=len(w)-1
            while(I<=J):
                m=I+(J-I)//2;
                if w[m]==B:
                    V+=1;
                    break;
                elif w[m]>B:
                    J=m-1;
                else:
                    I=m+1;            
        total+=(len(w)-V);
print(total)

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
    elif s[i] == "G":
        g += 1
    else:
        b += 1

ans = r * g * b
if ans == 0:
    print("0")
else:
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            sub = j - i
            sub2 = (n - 1) - j
            if sub <= sub2:
                if s[i] != s[j] and s[j] != s[j + sub] and s[i] != s[j + sub]:
                    ans -= 1
    print(ans)