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 = input()

RGBs = []
count = 0
RGBs.append({"value": S[0], "left_R": [], "left_G": [], "left_B": []})

for i in range(1, N):
    new = S[i]
    for j in range(i):
        if (S[j] == 'G' and new == 'B') or (S[j] == 'B' and new == 'G'):
            count += len(RGBs[j]["left_R"]) - int(i-j in RGBs[j]["left_R"])
        if (S[j] == 'R' and new == 'B') or (S[j] == 'B' and new == 'R'):
            count += len(RGBs[j]["left_G"]) - int(i-j in RGBs[j]["left_G"])
        if (S[j] == 'R' and new == 'G') or (S[j] == 'G' and new == 'R'):
            count += len(RGBs[j]["left_B"]) - int(i-j in RGBs[j]["left_B"])

    tmp_left_R = list(map(lambda x: x+1, RGBs[i-1]["left_R"]))
    tmp_left_G = list(map(lambda x: x+1, RGBs[i-1]["left_G"]))
    tmp_left_B = list(map(lambda x: x+1, RGBs[i-1]["left_B"]))
    if S[i-1] == 'R':
        tmp_left_R.append(1)
    if S[i-1] == 'G':
        tmp_left_G.append(1)
    if S[i-1] == 'B':
        tmp_left_B.append(1)
    RGBs.append({"value": S[i], "left_R": tmp_left_R, "left_G": tmp_left_G, "left_B": tmp_left_B})

print(count)

Submission status: Time Limit Exceeded

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

A = {'R':[] ,'G':[] ,'B':[]}
sum = 0

for i in range(N):
    A[S[i]].append(i)

for i in A['R']:
    for j in A['B']:
        for k in A['G']:
            l = [i,j,k]
            l.sort()
            if not l[1] - l[0] == l[2] - l[1]:
                sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
if __name__ == "__main__":
	N = int(input())
	S = tuple(input())
	sum = 0
	for c in combinations(range(N),3):
		S_i = S[c[0]]; S_j = S[c[1]]; S_k = S[c[2]]
		if (S_i != S_j) and (S_i != S_k) and (S_j != S_k):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1
	print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

l_r = [i for i, s in enumerate(S) if s=='R']
l_g = [i for i, s in enumerate(S) if s=='G']
l_b = [i for i, s in enumerate(S) if s=='B']

ans = len(l_r)*len(l_g)*len(l_b)

for dis in range(1, N//2+1):
  for i in range(1, N-2*dis+1):
    j = i+dis
    k = j+dis
    if len(set([S[i-1], S[j-1], S[k-1]]))==3:
      ans -= 1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
cnt=0
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)   
if len(r)*len(g)*len(b)==0:
  print(0)
else:
  tmpa=0
  tmpb=0
  tmpc=0
  for i in r:
    for j in g:
      for k in b:
        tmpa=min(i,j,k)
        tmpc=max(i,j,k)
        tmpb=i+j+k-tmpa-tmpc
        if tmpb-tmpa!=tmpc-tmpb:
          cnt=cnt+1
  print(cnt)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy
import sys
import statistics

num=int(input())
txt=input()
count=0
arr=numpy.zeros(len(txt),dtype=">i4")
for i in range(len(txt)):
  if txt[i]=='G':
    arr[i]=1
  elif txt[i]=='B':
    arr[i]=2
if 0 not in arr or 1 not in arr or 2 not in arr:
  print(0)
  sys.exit()
ri=numpy.where(arr==0)[0]
gi=numpy.where(arr==1)[0]
bi=numpy.where(arr==2)[0]
for i in ri:
  for j in gi:
    for k in bi:
      mean=statistics.mean([float(i),float(j),float(k)])
      med=statistics.median([i,j,k])
      diff=abs(mean-med)
      if diff<=1e-5:
        continue
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

N_list = [i for i in range(N)]

ans = 0
for iter in itertools.combinations(N_list, 3):
    cond_1 = (S[iter[0]] != S[iter[1]] and S[iter[1]] != S[iter[2]] and S[iter[2]] != S[iter[0]])
    cond_2 = (iter[0] - iter[1] != iter[1] - iter[2])
    if (cond_1 and cond_2):
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

count=0

for i in range(N):
    a=[l for l in range(N) if S[l]!=S[i] and l>i] 
    for j in a:
        b=[m for m in range(N) if S[m]!=S[i] and S[m]!=S[j] and j-i!=m-j and j<m]
        # print(b)
        count+=len(b)
print(count)

Submission status: Time Limit Exceeded

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

i2 = range(len(S) - 3)
j2 = len(S) - 1
k2 = len(S)
count = 0
for i in i2:
    for j in range(i + 1, j2):
        for k in range(j + 1, k2):
            if j - i == k -j:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                count += 1
                

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=list(input())
sr=[0]*n
sg=[0]*n
sb=[0]*n
for ii in range(n):
    if s[ii]=="R":
        sr[ii]=1
    if s[ii]=="G":
        sg[ii]=1
    if s[ii]=="B":
        sb[ii]=1

r=0
"""
print(sr)
print(sg)
print(sb)
"""
for ii in np.arange(1,n+1):
  for jj in np.arange(ii+1,n+1):
    if s[ii-1]!=s[jj-1] and jj<n:
        kk=2*jj-ii-1
        if (s[ii-1]=="R" and s[jj-1]=="G") or (s[jj-1]=="R" and s[ii-1]=="G"):
            r+=sum(sb[jj+1-1:n])
            if kk<n:
                r-=sb[kk]
        if (s[ii-1]=="R" and s[jj-1]=="B") or (s[jj-1]=="R" and s[ii-1]=="B"):
            r+=sum(sg[jj+1-1:n])
            if kk<n:
                r-=sg[kk]
        if (s[ii-1]=="B" and s[jj-1]=="G") or (s[jj-1]=="B" and s[ii-1]=="G"):
            r+=sum(sr[jj+1-1:n])
            if kk<n:
                r-=sr[kk]
        """
      for kk in np.arange(jj+1,n+1):
        #print(ii,jj,kk)
        if jj-ii!=kk-jj:
          if s[jj-1]!=s[kk-1] and s[ii-1]!=s[kk-1]:
            r+=1
        """
print(r)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
s = input()

count = 0

for i in range(0, n-2):
    for j in range(i, n-1):
        j_i = j - i
        if (s[i] != s[j]):
            for k in range(j, n):
                k_j = k - j
                if (s[i] != s[k]) and (s[j] != s[k]) and (j_i != k_j):
                    count = count + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
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 2*i-j in rlist:
                ans-=1
            if 2*j-i in rlist:
                ans-=1
            if (i+j)/2 in rlist:
                ans-=1
        else:
            if 2*i-j in rlist:
                ans-=1
            if 2*j-i in rlist:
                ans-=1
            if (i+j)/2 in rlist:
                ans-=1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
s=input()
count=0
for a in range(num-2):
  for b in range(a+1,num-1):
    for c in range(b+1,num):
      if (s[a]!=s[b] and s[b]!=s[c] and s[a]!=s[c]) and c-b!=b-a:
        count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
s = input()
c = Counter(s)
cnt = c['R'] * c['G'] * c['B']

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        w = j - i
        if j+w < n and (s[j+w] != s[j] and s[j+w] != s[i]):
            cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

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] = 1
    elif S[i] == 'G':
        G[i] = 1
    else:
        B[i] = 1

sumR = sum(R)
sumG = sum(G)
sumB = sum(B)

cnt = 0
for i in range(N):
    sumR -= R[i]
    sumG -= G[i]
    sumB -= B[i]
    sum_r = sumR
    sum_g = sumG
    sum_b = sumB
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
            if S[i] == 'B':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    print()
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            for k in range(j, N):
                sk = S[k]
                #if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                if si != sj and si != sk and sj != sk:
                    if j - i != k -j:
                        sum += 1
    print(sum)



main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = tuple(input())
    if n <= 3:
        sum = 0
    else:
        sum = 0
        for i in range(n-3):
            if s[i] == "R":
                j = i + 1
                while j <= n-2:
                    if s[j] == "G":
                        sum += s[j:n].count("B")
                        if 2*j-i <= n-1 and s[2*j-i] == "B":
                            sum -= 1
                    if s[j] == "B":
                        sum += s[j:n].count("G")
                        if 2*j-i <= n-1 and s[2*j-i] == "G":
                            sum -= 1
                    j += 1
            if s[i] == "G":
                j = i + 1
                while j <= n-2:
                    if s[j] == "R":
                        sum += s[j:n].count("B")
                        if 2*j-i <= n-1 and s[2*j-i] == "B":
                            sum -= 1
                    if s[j] == "B":
                        sum += s[j:n].count("R")
                        if 2*j-i <= n-1 and s[2*j-i] == "R":
                            sum -= 1
                    j += 1
            if s[i] == "B":
                j = i + 1
                while j <= n-2:
                    if s[j] == "R":
                        sum += s[j:n].count("G")
                        if 2*j-i <= n-1 and s[2*j-i] == "G":
                            sum -= 1
                    if s[j] == "G":
                        sum += s[j:n].count("R")
                        if 2*j-i <= n-1 and s[2*j-i] == "R":
                            sum -= 1
                    j += 1
    print(sum)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
notToAllow = 0


for i in range(0,N):
    if S[i] == "R":
        R = R + 1
    elif S[i] == "G":
        G = G + 1
    else:
        B = B + 1

total = R*G*B

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

print(R*G*B - notToAllow)

Submission status: Time Limit Exceeded

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

# def TLE():
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)
for i in range(N):
    if S[i] == 'R':
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans -= 1
                # ans += sum(x > j for x in B_index) -1
            continue
            # else:
                # ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans -= 1
                # ans += sum(x > j for x in G_index) -1
            continue
            # else:
                # ans += sum(x > j for x in G_index)
    elif S[i] == 'G':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans -= 1
                # ans += sum(x > j for x in B_index) -1
            continue
            # else:
                # ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans -= 1
                # ans += sum(x > j for x in R_index) -1
            continue
            # else:
                # ans += sum(x > j for x in R_index)
    elif S[i] == 'B':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans -= 1
                # ans += sum(x > j for x in G_index) -1
            continue
            # else:
                # ans += sum(x > j for x in G_index) 
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans -= 1
                # ans += sum(x > j for x in R_index) -1
            continue
            # else:
                # ans += sum(x > j for x in R_index)



# ans = 0
def TLE2():
    R_index = [n for n, v in enumerate(S) if v == 'R']
    G_index = [n for n, v in enumerate(S) if v == 'G']
    B_index = [n for n, v in enumerate(S) if v == 'B']
    ans = len(R_index) *len(G_index) *len(B_index)
    for i in range(N):
        for j in range(i+1, N):
            if S[i] == 'R' and S[j] =='G':
                # ans += sum(x > j for x in B_index)
                if j+j-i in B_index:
                    ans -= 1
                continue
            elif S[i] == 'R' and S[j] =='B':
                # ans += sum(x > j for x in G_index)
                if j+j-i in G_index:
                    ans -= 1
                continue
            elif S[i] == 'G' and S[j] =='R':
                # ans += sum(x > j for x in B_index)
                if j+j-i in B_index:
                    ans -= 1
                continue
            elif S[i] == 'G' and S[j] =='B':
                # ans += sum(x > j for x in R_index)
                if j+j-i in R_index:
                    ans -= 1
                continue
            elif S[i] == 'B' and S[j] =='R':
                # ans += sum(x > j for x in G_index)
                if j+j-i in G_index:
                    ans -= 1
                continue
            elif S[i] == 'B' and S[j] =='G':
                # ans += sum(x > j for x in R_index)
                if j+j-i in R_index:
                    ans -= 1
                continue
            # for k in range(j+1, N):
            #     if j-i == k-j:
            #         continue
            #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            #         ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

if __name__ == "__main__":
	N = int(input())
	S = tuple(input())

	sum = 0

	for c in combinations(range(N),3):
		S_i = S[c[0]]; S_j = S[c[1]]; S_k = S[c[2]]
		if (S_i != S_j) and (S_i != S_k) and (S_j != S_k):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

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

ans = 0

for x in range(n):
  for y in range(n):
    if not x < y:
      continue
    for z in range(n):
      if not x < y < z:
        # print("SKIP: ", x, y, z)
        continue

      if "".join( sorted([s[x], s[y], s[z]]) ) != "BGR":
        # print("SKIP: ", x, y, z)
        continue

      if y-x == z-y:
        # print("SKIP: ", x, y, z)
        continue

      ans += 1
      # print(x, y, z)
      # print(s[x], s[y], s[z])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

def split(s):
    r = []
    g = []
    b = []
    for i in range(len(s)):
        if 'R' == s[i]:
            r.append(i)
        elif 'G' == s[i]:
            g.append(i)
        elif 'B' == s[i]:
            b.append(i)
    return { 'R':r, 'G':g, 'B':b }

def f(s):
    for key in 'RGB':
        if key not in (s):
            return key

def biin(a, x):
    i = bisect.bisect_left(a, x)
    return i < len(a) and a[i] == x

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

    rgb = split(s)
    count = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            if s[i] == s[j]:
                continue
            color = f(s[i] + s[j])
            count += len(rgb[color]) - bisect.bisect_left(rgb[color], j+1)
            if biin(rgb[color], (j + (j - i))):
                count -= 1
    print(count)

main()

Submission status: Time Limit Exceeded

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

count = 0

def other_char_num(char1:str, char2:str) -> int:
  return len([n for n in [m for m in S if m!=char1] if n!=char2])

for i in range(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
      elif S[j] == S[k] or S[k] == S[i]:
        continue
      else:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
def main():
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            if s[i] != s[j]:
                a = s[i]
                b = s[j]
                for k in range(j+1, n):
                    if a != s[k] and b != s[k] and j != (k + i)/2:
                        ans +=1

    return ans

if __name__ == '__main__':
  print(main())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    t = s[i]
    if t == 'R':
        r.append(i+1)
    elif t == 'G':
        g.append(i+1)
    else:
        b.append(i+1)

ans = 0
for i in r:
    for j in g:
        for k in b:
            if i < j < k:
                if j-i != k-j:
                    ans += 1
for i in g:
    for j in b:
        for k in r:
            if i < j < k:
                if j-i != k-j:
                    ans += 1
for i in b:
    for j in r:
        for k in g:
            if i < j < k:
                if j-i != k-j:
                    ans += 1
for i in r:
    for j in b:
        for k in g:
            if i < j < k:
                if j-i != k-j:
                    ans += 1
for i in g:
    for j in r:
        for k in b:
            if i < j < k:
                if j-i != k-j:
                    ans += 1
for i in b:
    for j in g:
        for k in r:
            if i < j < k:
                if j-i != k-j:
                    ans += 1

"""
ans = 0
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            if sl[i-1] != sl[j-1] and sl[j-1] != sl[k-1] and sl[k-1] != sl[i-1]:
                if 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(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        else:
            sk = ({"R", "G", "B"} - set([S[i], S[j]])).pop()
            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:
n = int(input())
s = input()
r = []
g = []
b = []
res = 0
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
if max(len(r),len(g),len(b)) == len(b):
  for i in g:
    for j in r:
      res += len(b) - b.count((i + j) / 2) - b.count( 2 * i - j) - b.count(2 * j - i)
else:
  for i in b:
    for j in r:
      res += len(g) - g.count((i + j) / 2) - g.count( 2 * i - j) - g.count(2 * j - i)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = II()
S = input()

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

Submission status: Time Limit Exceeded

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

    answer = 0

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

            for k in range(j + 1, N):
                Sk = S[k]
                if Si == Sk or Sj == Sk:
                    continue
                if j - i == k - j:
                    continue
                answer += 1
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = input()
S = input()
sli = [c for c in S]

nR = sli.count('R')
nG = sli.count('G')
nB = sli.count('B')

dx = 1
ng = 0
for i, c in enumerate(sli):
    rl = []
    dx = 1

    a = int(len(sli) / 2)
    rl = [(i, i+dx, i+dx*2)
          for dx in range(len(sli)) if dx * 2 + i < len(sli)]

    for ind in rl:
        if len(set([sli[ind[0]], sli[ind[1]], sli[ind[2]]])) == 3:
            ng += 1


n = nR * nG * nB - ng
print(str(n))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i, s in enumerate(S):
    if s == 'R':
        R.append(i+1)
    elif s == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
res = [sorted([r, g, b]) for r in R for g in G for b in B]
print(len([x for x in res if x[1] - x[0] != x[2] - x[1]]))

Submission status: Time Limit Exceeded

Solution code:
ans = 0


def solve():

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

    def dfs(len, a, b):
        global ans

        if len == 1:
            for i in range(a, N):
                if S[a] != S[i]:
                    dfs(2, a, i)
        elif len == 2:
            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):
        dfs(1, i, 0)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

ans = 0
for j in range(N):
    original = list("RGB")
    if S[j] in original:
        for i in range(j):
            obj = [a for a in original if a != S[j]]
            if S[i] in obj:
                for k in range(j+1, N):
                    if (j-i) != (k-j):
                        obj_2 = [b for b in obj if b != S[i]]
                        if S[k] == obj_2[0]:
                        	ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from time import time

def solve(N, S):
    answer = 0
    for i in range(N):
        Si = S[i]
        for k in range(i+2, N):
            Sk = S[k]
            if Si == Sk:
                continue
            for j in range(i+1, k):
                if j-i == k-j:
                    continue
                Sj = S[j]
                if Si == Sj or Sj == Sk:
                    continue
                answer += 1
    print(answer)

def main(N, S):
    import sys
    start = time()
    from itertools import combinations
    Cs = [[], [], []]
    for i in range(N):
        c = S[i]
        if c == 'R':
            Cs[0].append(i)
        elif c == 'G':
            Cs[1].append(i)
        elif c == 'B':
            Cs[2].append(i)
    answer = 0
    for C in Cs:
        a = 0
        print(C, len(C))
        for Ci in range(len(C)-1):
            i = C[Ci]
            for Cj in range(Ci+1, len(C)):
                j = C[Cj]
                print("*", i, j)
                print(i)           # i より前
                print(j - i - 1)   # i と j の間
                print(N - 1 - j)   # j より後
                print(Ci)
                print(Cj - Ci-1)
                print(len(C) - 1 - Cj)
                a += i           # i より前
                a += N - 1 - j   # j より後
                a += j - i - 1   # i と j の間
                a -= Ci
                a -= Cj - Ci-1
                a -= len(C) - 1 - Cj
        print(S[C[0]], "expect", sum(S[C[0]] in (i, j, k) and (i == j or j == k or i == k) for i,j,k in combinations(S, 3)))
        print(S[C[0]], "actual", a)
        answer += a
    print("expect", sum(i == j or j == k or i == k for i,j,k in combinations(S, 3)))
    print("actual", answer)

    print(answer, time() - start, file=sys.stderr)
    for i in range(N):
        for d in range(1, N-i):
            if i + 2*d < N and not(S[i] == S[i+d] or S[i] == S[i+2*d]):
                answer += 1
    total = N * (N-1)*(N-2) // 6
    print(total, answer, file=sys.stderr)
    print(total - answer)
    print(time() - start, file=sys.stderr)


N = int(input())
S = input()
solve(N, S)
#main(N, S)

Submission status: Time Limit Exceeded

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

n= int(input())
s = input()
R = []
G = []
B = []
import bisect
def func(l1,l2,l3,l3set):
  res =0
  for i in l1:
    for j in l2:
      if i>j:
        continue
      a = l3[bisect.bisect_left(l3,j):]
      res += len(a)
      if j*2-i in l3set:
        res -=1
  return res

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)
    
    
R_set = set(R)
G_set = set(G)
B_set = set(B)
ans = func(R,G,B,B_set)+func(R,B,G,G_set)+func(G,R,B,B_set)+func(G,B,R,R_set) + func(B,R,G,G_set)+func(B,G,R,R_set)
print(ans)