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: Time Limit Exceeded

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list,target):

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = int((left + right)/2)
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return result
	else:
		return result


if __name__=="__main__":
    n=int(input())
    s=list(input())
    rlist=[]
    glist=[]
    blist=[]
    ans=0
    if len(s)<=3:
        print(0)
        exit()
    for i in range(n):
        if s[i]=="R":
            rlist.append(i)
        elif s[i]=="G":
            glist.append(i)
        else:
            blist.append(i)
    if len(rlist)==0 or len(glist)==0 or len(blist)==0:
        print(0)
        exit()
    ans+=len(rlist)*len(glist)*len(blist)
    for i in glist:
        for j in blist:
            if i>j :
                if binary_search(rlist,2*i-j)!=-1:
                    ans-=1
                if binary_search(rlist,2*j-i)!=-1:
                    ans-=1
                if (i+j)/2 in rlist:
                    ans-=1
            else:
                if binary_search(rlist,2*i-j)!=-1:
                    ans-=1
                if binary_search(rlist,2*j-i)!=-1:
                    ans-=1
                if (i+j)/2 in rlist:
                    ans-=1

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
ans = 0
for a in range(0,N-2):
    for  b in range(a+1,N-1,1):
        if s[a]!=s[b]:
            RGBlist = ["R","G","B"]
            RGBlist.remove(s[a])
            RGBlist.remove(s[b])
            ans_list = s[(b+1):]
            if b-a <= len(s[(b+1):]):
                ans_list.pop(b-a-1)
            ans += ans_list.count(RGBlist[0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
c = 0
for i in range(N):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    else:
        B.append(i)

arr = []

for r in R:
    for g in G:
        for b in B:
            arr = sorted([r,g,b])
            if(arr[1]-arr[0]) != (arr[2]-arr[1]): 
                c = c + 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    s = list(s)
    ans = 0

    for i in range(0, n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            for k in range(j+1, n):
                if j-i == k-j:
                    continue
                if s[i] == s[k]:
                    continue
                if s[j] == s[k]:
                    continue
                ans += 1

    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:

from bisect import bisect_left,bisect
 
n=int(input())
s=input()
#n=39
#s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

#n=7
#s="RBRBGRB"

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)

lenr=len(r)
leng=len(g)
lenb=len(b)

#print(r)
#print(g)
#print(b)

isum=0
for ir in range(len(r)):
    for ig in range(len(g)):
        imin=max(r[ir],g[ig])
        bi=bisect(b,imin)
#        print(ir,ig,imin,bi)
        if abs(r[ir]-g[ig])+imin in b: 
            isum=isum+lenb-bi-1
        else:
            isum=isum+lenb-bi
for ir in range(len(r)):
    for ib in range(len(b)):
        imin=max(r[ir],b[ib])
        gi=bisect(g,imin)
        if abs(r[ir]-b[ib])+imin in g: 
            isum=isum+leng-gi-1
        else:
            isum=isum+leng-gi
for ig in range(len(g)):
    for ib in range(len(b)):
        imin=max(g[ig],b[ib])
        ri=bisect(r,imin)
        if abs(g[ig]-b[ib])+imin in r: 
            isum=isum+lenr-ri-1
        else:
            isum=isum+lenr-ri
                
print(isum)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets
import itertools 

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

R = [i for i, x in enumerate(s) if x=='R']
G = [i for i, x in enumerate(s) if x=='G']
B = [i for i, x in enumerate(s) if x=='B']

ans = 0
for r, g, b in itertools.product(R, G, B):
    mi = min(r, g, b)
    ma = max(r, g, b)
    if (r+g+b)==mi+ma+(mi+ma)/2:
        continue
    else:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

out = 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and j - i != k - j:
                #print(s[i], s[j], s[k])
                out = out + 1

print(out)

Submission status: Time Limit Exceeded

Solution code:
def solve(n, s):
    rlist, glist, blist = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
    rcount, gcount, bcount = 0, 0, 0
    for i in range(n-1):
        if s[n-1-i] == 'R':
            rcount += 1
        if s[n-1-i] == 'G':
            gcount += 1
        if s[n-1-i] == 'B':
            bcount += 1
        rlist[n-2-i], glist[n-2-i], blist[n-2-i] = rcount, gcount, bcount
        
    ans = 0

    for i in range(len(s)-2):
        for j in range(i+1,len(s)-1):
            if s[i] != s[j]:
                if s[i] == 'R':
                    if s[j] == 'G':
                        ans += blist[j] - ( 1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    elif s[j] == 'B':
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
                elif s[i] == 'G':
                    if s[j] == 'R':
                        ans += blist[j] - (1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    elif s[j] == 'B':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                elif s[i] == 'B':
                    if s[j] == 'G':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                    elif s[j] == 'R':
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
    
    return ans

def main():
    n = int(input())
    s = input()
    print(solve(n, s))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
if N > 3:
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            for k in range(j + 1, N):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k] and j - i != k - j:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for i in range(0,n-2):
	for j in range(i+1,n-1):
		for k in range(j+1,n):
			if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k] and (j-i)!=(k-j):
				ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

flag = 0
count = 0

for i in range(N):
    for j in range(N):
        for k in range(N):

            I = i+1
            J = j+1
            K = k+1 

            if(I<=J and J<=K):
                
                if(S[i] != S[j]):
                    if(S[j] != S[k]):
                       if(S[i] != S[k]):
                           if((J-I) != (K-J)):
                         
                              count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
i = int(input())
l = str(input())
ll = len(l)

count = 0

m=0
mm = 0
mmm = 0
for k in l:
    m+=1
    for kk in l:
        mm +=1
        for kkk in l:
            mmm += 1
            if k != kk and kk != kkk and k != kkk and m <= mm and mm <= mmm and (mm-m) != (mmm - mm):
                count+=1
        mmm=0
    mm=0
print()

print(count)

Submission status: Time Limit Exceeded

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

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

result = 0

R_counter = 0
G_counter = 0
B_counter = 0


for i in range(N):
    ai = S[i]
    if ai == 'R':
        if G_counter > G or B_counter > B:
            break
        else:
            R_counter += 1
    elif ai == 'G':
        if R_counter > R or B_counter > B:
            break
        else:
            G_counter += 1
    else:
        if G_counter > G or R_counter > R:
            break
        else:
            B_counter += 1
    for j in range(i+1, N):
        aj = S[j]
        if ai == 'R' and aj == 'G':
            if B_counter > B:
                break
        elif ai =='R' and aj == 'B':
            if G_counter > G:
                break
        elif ai == 'G' and aj == 'R':
            if B_counter > B:
                break
        elif ai == 'G' and aj == 'B':
            if R_counter > R:
                break
        elif ai == 'B' and aj == 'R':
            if G_counter > G:
                break
        else:
            if R_counter > R:
                break
        if ai == aj:
            continue
        for k in range(j+1, N):
            ak = S[k]
            if ai != ak and aj != ak:
                if j-i != k-j:
                    result += 1
            else:
                continue

print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
k=input()
r,B,g=[],[],[]
for i in range(n):
    l=k[i]
    if l=="R":
        r+=[i]
    if l=="B":
        B+=[i]
    if l=="G":
        g+=[i]
k=0
for a in r:
    for b in B:
        for c in g:
            p=[a,b,c]
            p.sort()
            q,r,s=p
            if s!=2*r-q:
                k+=1
print(k)

Submission status: Time Limit Exceeded

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

ans = N * (N - 1) * (N - 2) // 6
for i in range(N):
  I = S[i]
  for j in range(i+1, N):
    J = S[j]
    if I == J:
      ans -= N - 2
    else:
      if 2 * j - i < N:
        K = S[2 * j - i]
        if K != I and K != J:
          ans -= 1
          
ans += r * (r - 1) * (r - 2) // 3
ans += g * (g - 1) * (g - 2) // 3
ans += b * (b - 1) * (b - 2) // 3

print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans = 0


def solve():

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

    def dfs(a, b):
        global ans

        if b == -1:
            for i in range(a, N-1):
                if S[a] != S[i]:
                    dfs(a, i)
        else:
            for i in range(b, N):
                if S[b] != S[i] and S[a] != S[i]:
                    if i - b != b - a:
                        ans += 1

    for i in range(0, N-2):
        dfs(i, -1)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
def main():
    word_len = int(input())
    word = str(input())
    R_list = []
    G_list = []
    B_list = []
    for i in range(word_len):
        if word[i] == "R":
            R_list.append(i)
        if word[i] == "G":
            G_list.append(i)
        if word[i] == "B":
            B_list.append(i)
    counter = 0
    for r in R_list:
        for g in G_list:
            for b in B_list:
                v =  [r,g,b]
                v.sort()
                if (v[1]-v[0]) != (v[2]-v[1]):
                    counter += 1
    print(counter)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
#print(S[1])
cnt=0
cnt2=0
for k in range(1,N+1):
  for j in range(1,k):
    for i in range(1,j):
      if (j-i)!=(k-j):
        if S[i-1]!=S[j-1] and S[i-1]!=S[k-1] and S[j-1]!=S[k-1]:
          cnt += 1
          #print(i,j,k,j-i,k-j, S[i-1],S[j-1],S[k-1])
print(cnt)

Submission status: Time Limit Exceeded

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

rgb = {'R', 'G', 'B'}

res = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            tmp = rgb
            tmp = tmp - {S[i], S[j]}
            ok = True
            k = j
            while ok:
                k = S.find(list(tmp)[0], k + 1)
                if k == -1:
                    ok = False
                elif k - j != j - i:
                    res += 1
                else:
                    pass
print(res)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
# 整数の入力
n = int(input())
# 文字列の入力
s = input()
# 数え上げ
count = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            if s[i] != 'R' and s[j] != 'R':
                C = 'R'
            elif s[i] != 'G' and s[j] != 'G':
                C = 'G'
            else:
                C = 'B'
            count += s.count(C, j+1, 2*j - i)
            count += s.count(C, 2*j - i + 1)
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0
w = {'R', 'G', 'B'}

for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
        if {s[i], s[j], s[k]} == w and j-i != k-j:
          cnt += 1
      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
s = input()
r_cnt = 0
g_cnt = 0
b_cnt = 0
for i in s:
    if i == 'R':
        r_cnt += 1
    elif i == 'G':
        g_cnt += 1
    elif i == 'B':
        b_cnt += 1
        
total = r_cnt*g_cnt*b_cnt

if n < 4:
    print(0)

else:
    for i in range(1,math.floor(len(s)-3/2)):
        for j in range(n-2*i):
            tmp = s[j]+s[j+i]+s[j+2*i]
            if (tmp == 'RGB') or (tmp == 'RBG') or (tmp == 'GBR') or (tmp == 'GRB') or (tmp == 'BRG') or (tmp == 'BGR'):
                total -= 1

    print(total)

Submission status: Time Limit Exceeded

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

    c = 0

    skip_index_tuple = []

    for i in range(N):
        for j in range(i, N):
            if [i,j] in skip_index_tuple:
                continue

            if S[i]==S[j]:
                continue
            for k in range(j, N):
                if S[j] == S[k]:
                    skip_index_tuple.append([j,k])
                if S[j] == S[k] or S[i] == S[k] or j-i == k-j:
                    continue
                else:
                    c = c+1
        
    print(c)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L = []
Ans = 0
for i in range(N):
  s = S[i]
  a = 0
  b = 0
  for j in range(i+1,N):
    t = S[j]
    if s != t:
      K = ["R","G","B"]
      K.remove(s)
      if t == K[0]:
        a +=1
      else:
        b +=1
  Ans += a*b

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
rgb = input()
ans = 0
for i in range(n):
  for j in range(i, n):
    if rgb[i] == rgb[j]:
      continue
    else:
      for k in range(j, n):
        if rgb[i] != rgb[j] and rgb[i] != rgb[k] and rgb[j] != rgb[k] and j - i != k - j:
          ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

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

rs = {}
gs = {}
bs = {}

for i, e in enumerate(s):
  if e == 'R':
    rs[i] = ''
  if e == 'G':
    gs[i] = ''
  if e == 'B':
    bs[i] = ''
   
      
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  for i in rs.keys():
    for j in gs.keys():
      bs_tmp = copy.deepcopy(bs)
      mi = i if i < j else j
      ma = j if i < j else i
      p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
      for e in p:
        if e%1 != 0 or e < 0 or e > len(s):
          continue
        if e in bs_tmp:
          del bs_tmp[e]
          saidai -= 1
  print(int(saidai))

Submission status: Time Limit Exceeded

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)
count = 0
for i in r:
	for j in g:
		u = abs(i-j)
		if (max(i,j)+u) in b:
			count += 1
		if (min(i,j)-u) in b:
			count += 1
		if u%2 == 0:
			if (i+j)//2 in b:
				count += 1
print(len(r)*len(g)*len(b)-count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = [] 
G = []
B = []
i = 1
while i < N+1:
  a = S[i-1]
  if a == 'R':
    R.append(i)
  elif a == 'G':
    G.append(i)
  else:
    B.append(i)
  i += 1
  

c = 0
d = len(R)*len(G)*len(B)


if d == 0:
    print(0)
else:
    for t in range(len(R)):
        for u in range(len(G)):
            k = (R[t] + G[u])/2
            l = 2*R[t]-G[u]
            m = 2*G[u]-R[t]
            if k in B:
                c += 1
            if l in B:
                c += 1
            if m in B:
                c += 1
                
    print(d-c)

Submission status: Time Limit Exceeded

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

r_list = [i for i, x in enumerate(s) if x == 'R']
g_list = [i for i, x in enumerate(s) if x == 'G']
b_list = [i for i, x in enumerate(s) if x == 'B']


result = 0
result_list = []
for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            result_list.append([r_index, g_index, b_index])

minus_count = 0
for comb in result_list:
    comb = sorted(comb)
    if comb[2] - comb[1] == comb[1] - comb[0]:
        minus_count +=1
print(len(result_list)- minus_count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
r=s.count("R")
b=s.count("B")
g=s.count("G")
ans=0
herasu=0
for i in range(n):
  if s[i]=="R":
    ab=s[:i].count("B")
    ag=s[:i].count("G")
    bb=b-ab
    bg=g-ag
    l=ab*bg+ag*bb
    ans+=l
  elif s[i]=="B":
    ar=s[:i].count("R")
    ag=s[:i].count("G")
    br=r-ar
    bg=g-ag
    l=ar*bg+ag*br
    ans+=l
  else:
    ab=s[:i].count("B")
    ar=s[:i].count("R")
    bb=b-ab
    br=r-ar
    l=ab*br+ar*bb
    ans+=l
for i in range(1,n):
  for e in range(i):
    if s[i]!=s[e]:
      if i*2-e<=n-1:
        if s[e]!=s[i*2-e] and s[i]!=s[i*2-e]:
          herasu+=1
print(ans-herasu)

Submission status: Time Limit Exceeded

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

r_index = []
g_index = []
b_index = []

for i in range(N):
    if S[i] == 'R':
        r_index.append(i)

    if S[i] == 'G':
        g_index.append(i)

    if S[i] == 'B':
        b_index.append(i)

res = 0


def cnt(x, y, z):
    res = 0
    for xi in x:
        for yi in y:
            for zi in z:
                if yi - xi != zi - yi and xi < yi < zi:
                    # print(xi, yi, zi)
                    res += 1
    return res


ans = []

ans.append(cnt(r_index, g_index, b_index))
ans.append(cnt(r_index, b_index, g_index))
ans.append(cnt(b_index, r_index, g_index))
ans.append(cnt(b_index, g_index, r_index))
ans.append(cnt(g_index, b_index, r_index))
ans.append(cnt(g_index, r_index, b_index))

print(sum(ans))

Submission status: Time Limit Exceeded

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

positions = {}
positions['R'] = [i for i in range(n) if s[i]=='R']
positions['G'] = [i for i in range(n) if s[i]=='G']
positions['B'] = [i for i in range(n) if s[i]=='B']
positions['RG'] = sorted(positions['R']+positions['G'])
positions['RB'] = sorted(positions['R']+positions['B'])
positions['GB'] = sorted(positions['G']+positions['B'])

for p1 in range(n-3):
    if s[p1] == 'R':
        pos2 = [p for p in positions['GB'] if p > p1]
    elif s[p1] == 'G':
        pos2 = [p for p in positions['RB'] if p > p1]
    else:
        pos2 = [p for p in positions['RG'] if p > p1]

    for p2 in pos2:
        if s[p1] == 'R' and s[p2] == 'G':
            pos3 = [p for p in positions['B'] if p > p2]
        elif s[p1] == 'R' and s[p2] == 'B':
            pos3 = [p for p in positions['G'] if p > p2]
        elif s[p1] == 'G' and s[p2] == 'B':
            pos3 = [p for p in positions['R'] if p > p2]
        elif s[p1] == 'G' and s[p2] == 'R':
            pos3 = [p for p in positions['B'] if p > p2]
        elif s[p1] == 'B' and s[p2] == 'R':
            pos3 = [p for p in positions['G'] if p > p2]
        elif s[p1] == 'B' and s[p2] == 'G':
            pos3 = [p for p in positions['R'] if p > p2]
        
        for p3 in pos3:             
            if (p1-p2 != p2-p3):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

    for i in range(N):
        for j in range(i+1, N):
            for k in range(j+1, N):
                if j - i != k - j:
                    if S[i] != S[j]:
                        if S[j] != S[k]:
                            if S[i] != S[k]:
                                res += 1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = 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)

dif = 0
for r in R:
    for g in G:
        for b in B:
            if abs(g - r) == abs(g - b):
                dif += 1
            elif abs(b - r) == abs(b - g):
                dif += 1
            elif abs(r - g) == abs(r - b):
                dif += 1
             

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
 
def solve(N: int, S: str):
    r = []
    g = []
    b = []
 
    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        elif x =="B":b.append(i)
 
    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
 
 
    def rgb(r, g, b):
        sb = set(b)
        ans = 0
        for r_ in r:
            # rg
            for g_ in g[g>r_]:
                # rgb
                if (g_ + (g_ - r_)) in sb:
                    ans -= 1
                # brg
                if (r_ - (g_ - r_)) in sb:
                    ans -= 1
                # rbg
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b)

            # gr
            for g_ in g[g<r_]:
                # grb
                if (r_ + (r_ - g_)) in sb:
                    ans -= 1
                # bgr
                if (g_ - (r_ - g_)) in sb:
                    ans -= 1
                # gbr
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b)

        return ans
 
    k = rgb(r,g,b)
    print(k)
 
 
    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: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = []
g = []
b = []
for i,j in enumerate(s):
    if j == "R":
        r.append(i)
    elif j == "G":
        g.append(i)
    else:
        b.append(i)
ans = 0
for i in r:
    for j in g:
        sa = abs(i-j)
        if sa%2==0:
            p = (i+j)//2
            if p in b:
                ans-=1
        ans += len(b)
        if i<j:
            if (i-sa) in b:
                ans -= 1
            if (j+sa) in b:
                ans -= 1
        if i>j:
            if (j-sa) in b:
                ans -= 1
            if (i+sa) in b:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if len(set([S[i], S[j]])) == 2:
            sk = list({"R", "G", "B"} - set([S[i], S[j]]))[0]
            if 2*j - i < N and S[2*j - i] == sk:
                ans += S[j+1:].count(sk) - 1
            else:
                ans += S[j+1:].count(sk)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
s = input()
R,G,B = [],[],[]
for i in range(n):
    if s[i]=='R':
        R.append(i)
    elif s[i]=='G':
        G.append(i)
    else:
        B.append(i)
if len(R)==0 or len(G)==0 or len(B)==0:
    print(0)
    exit()
cnt = 0
for r in R:
    for g in G:
        a,b = min(r,g), max(r,g)
        if 2*a-b in B:
            cnt += 1
        if 2*b-a in B:
            cnt += 1
        if (b-a)%2==0 and a+(b-a)//2 in B:
            cnt += 1
print(len(R)*len(G)*len(B)-cnt)

Submission status: Time Limit Exceeded

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

c = 0

chars = list(s)

for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if (j-i!=k-j):
                if (chars[i] != chars[j] and chars[i] != chars[k] and chars[j] != chars[k]):
#                    print("{}:{}:{}".format(i,j,k))
                    c+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a = [str(c) for c in s]
ans=0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if (k-j)==(j-i):
                continue
            elif a[i]!=a[j] and a[j]!=a[k] and a[k]!=a[i]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
    return [id for id, _x in enumerate(l) if _x == x]

#R_index = my_index_multi(s,'R')
#G_index = my_index_multi(s,'G')
#B_index = my_index_multi(s,'B')

R_index = []
G_index = []
B_index = []

for i in range(n):
    if s[i] == 'R':
        R_index.append(i)
    if s[i] == 'G':
        G_index.append(i)
    if s[i] == 'B':
        B_index.append(i)

#for R_index[i] in range(len(R_index)):
#    R_index[R_index[i]] += 1
#for G_index[j] in range(len(G_index)):
#    G_index[G_index[j]] += 1
#for B_index[k] in range(len(B_index)):
#    B_index[B_index[k]] += 1

count =  len(R_index)*len(G_index)*len(B_index)
for i in range(len(R_index)):
    for j in range(len(G_index)):
        if (R_index[i]+G_index[j])//2 in B_index and (R_index[i]+G_index[j])%2==0:
            count = count -1
    for k in range(len(B_index)):
        if (R_index[i]+B_index[k])//2 in G_index and (R_index[i]+B_index[k])%2==0:
            count = count -1
for j in range(len(G_index)):
    for k in range(len(B_index)):
        if (G_index[j]+B_index[k])//2 in R_index and (G_index[j]+B_index[k])%2==0:
            count = count -1

#        for B_index[k] in range(len(B_index)):
#            li = [R_index[R_index[i]],G_index[G_index[j]],B_index[B_index[k]]]
#            li.sort()
#            if (li[1]*2 != li[0]+li[2]):
#                count = count +1

#print(len(R_index))
#print(len(G_index))
#print(len(B_index))
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
import math

n = int(input())
S = input()

ans = 0

dis = []

l = len(S)

for i in range(l-2):
    for j in range(i+1,l-1):
        if S[i] == S[j]:
            continue
        for k in range(j+1,l):
            if S[i] == S[k]:
                continue
            if S[j] == S[k]:
                continue
            if j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a,b,c,count=0,0,0,0
l=["R","G","B"]
for i in range(N):
	a=S[i]
	for h in range(i+1,N):
		b=S[h]
		if b==a:
			continue
		else:
			l.remove(a)
			l.remove(b)
			count+=S.count(l[0],h+1,N)
			if 2*h-i<N:
				if S[2*h-i]==l[0]:
					count-=1
			l=["R","G","B"]
			

print(count)