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:
def main():
    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]:
                k1=S[j+1:].count(S[i])
                k2=S[j+1:].count(S[j])
                ans=ans+len(S[j+1:])-k1-k2
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0

cnt = 0
for i in range(N):
    a = S[i]

    if a == 'R':
        r_cnt += 1
    elif a == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i):
        b = S[j]

        d = i - j
        if (a == 'R' and b == 'G') or (a == 'G' and b == 'R'):
            if j - 1 >=0 and B_lst[j - 1] > 0:
                cnt += B_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'B':
                    cnt -= 1

        if (a == 'G' and b == 'B') or (a == 'B' and b == 'G'):
            if j - 1 >=0 and R_lst[j - 1] > 0:
                cnt += R_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'R':
                    cnt -= 1

        if (a == 'B' and b == 'R') or (a == 'R' and b == 'B'):
            if j - 1 >= 0 and G_lst[j - 1] > 0:
                cnt += G_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'G':
                    cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            lis=["R","G","B"]
            if S[i]!=S[j]:
                lis=["R","G","B"]
                lis.remove(S[i])
                lis.remove(S[j])
                k=S[j+1:].count(lis[0])
                ans=ans+k
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import *
from bisect import *
n = int(input())
s = input()
r = deque()
g = deque()
b = deque()
ans = 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)

def cnt(x, y, z):
    global ans
    for i in x:
        index1 = bisect_left(y, i)
        if index1 <= len(y) - 1:
            for j in range(index1, len(y)):
                index2 = bisect_left(z, y[j])
                if index2 <= len(z) - 1:
                    for k in range(index2, len(z)):
                        if z[k] == y[j] + (y[j] - i):
                            ans -= 1
                    ans += len(z) - index2
cnt(r, g, b)
cnt(r, b, g)
cnt(g, r, b)
cnt(g, b, r)
cnt(b, r, g)
cnt(b, g, r)
print(ans)

Submission status: Time Limit Exceeded

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

l_r = []
l_g = []
l_b = []
for i in range(N):
  if S[i]=='R':
    l_r.append(i)
  if S[i]=='G':
    l_g.append(i)
  if S[i]=='B':
    l_b.append(i)

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

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
a=int(input())
b=str(input())
sum=0
c=b
d=b
i=0
for s in b:
    for n in range(i,a):
        if c[n]!=c[i]:
            j=n-i
            if a>=n:
                for m in range(n,a):
                    if c[m]!=c[i] and c[m]!=c[n]:
                        k=m-n
                        if k!=j:
                            sum+=1



    i+=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n,s=open(0)
n=int(n)
d={i:s.count(i)for i in'RGB'}
a=0
for i in range(n):
    d[s[i]]-=1
    b=d.copy()
    for j in range(i+1,n):
        b[s[j]]-=1
        if s[i]==s[j]:continue
        c,*_=set('RGB')-{s[i],s[j]}
        a+=b[c]
        k=j+j-i
        if k<n and s[k]==c:
            a-=1
print(a)

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+1)
    elif s[i]=="G":
        g.append(i+1)
    else:
        b.append(i+1)
cnt=0
sum=len(b)*len(g)*len(r)
if len(r)*len(g)*len(b)==0:
    print(0)
else:
    for re in r:
        for gr in g:
            
            re1=re
            re1,gr=min(re,gr),max(re,gr)
            #print(re,gr,b)
            if (re1+gr)%2==0:
                if (re1+gr)//2 in b:
                    cnt-=1
            if 2*gr-re1 in b:
                cnt-=1
            if 2*re1-gr in b:
                cnt-=1
            
            #print(cnt)
    print(sum+cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

ans = r*g*b

for i in range(n):
    for j in range(i+1,n):
        k = j + (j-i)
        if k<n and set([s[i],s[j],s[k]])==set(["R","G","B"]):
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N-2):
  for j in range(N-i-2):
    if S[i]!=S[i+j+1]:
      A=["R","B","G"]
      A.remove(S[i])
      A.remove(S[i+j+1])
      for k in range(N-i-j-2):
        if A[0]==S[i+j+k+2] and j!=k:
          ans+=1

print(str(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
char_list = list(s)
sum=0

for i in range(n):
    for j in range(n):
        for k in range(n):
            #print("i,j,k:{},{},{}".format(i,j,k))
            if not (i<j<k):
                continue
            if ([char_list[i],char_list[j],char_list[k]]==["R", "G", "B"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))
            elif ([char_list[i],char_list[j],char_list[k]]==["R", "B", "G"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))
            elif ([char_list[i],char_list[j],char_list[k]]==["G", "R", "B"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))
            elif ([char_list[i],char_list[j],char_list[k]]==["G", "B", "R"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))
            elif ([char_list[i],char_list[j],char_list[k]]==["B", "G", "R"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))
            elif ([char_list[i],char_list[j],char_list[k]]==["B", "R", "G"])&(j-i!=k-j):
                sum+=1
                #print("i,j,k:{},{},{}".format(i,j,k))

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
a = 0
for i in range(3, n + 1):
    for j in range(2, i):
        for k 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[k-1] != s[j-1]:
                    a += 1
                    
print(a)

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 r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# NOTE: PyPyだと通る

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

count_R = (0,)
count_G = (0,)
count_B = (0,)
for c in s:
    count_R += (count_R[-1]+1,) if c == 'R' else (count_R[-1],)
    count_G += (count_G[-1]+1,) if c == 'G' else (count_G[-1],)
    count_B += (count_B[-1]+1,) if c == 'B' else (count_B[-1],)
d = {'R': count_R, 'G': count_G, 'B': count_B}

res = 0
for i in range(n-2):
    for k in range(i+1, n-1):
        if s[i] == s[k]:
            continue
        st = {'R', 'G', 'B'}
        st.discard(s[i])
        st.discard(s[k])
        c = st.pop()
        cnt = d[c]
        res += cnt[-1] - cnt[k]
        if k+k-i < n and s[k+k-i] == c:
            res -= 1

print(res)

Submission status: Time Limit Exceeded

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

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

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

for i in range(n):
    for j in range(i+1,n):
        k = j + (j-i)
        if k<n and set([s[i],s[j],s[k]])==set(["R","G","B"]):
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
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)
for x in r:
	for y in g:
		for z in b:
			L=[x,y,z]
			#print(L)
			L=sorted(L)
			if (L[1]-L[0])!=(L[2]-L[1]):
				count+=1
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0

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

      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
i_c = j_c = k_c =1
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k_c>j_c and j_c>i_c):
                if(j_c-i_c!=k_c-j_c):
                    ijk.append([i,j,k])
            k_c+=1
        j_c+=1
        k_c = 1
    i_c+=1
    j_c = 1
for i in range(len(ijk)):
    if(S[ijk[i][0]]!= S[ijk[i][1]] and
       S[ijk[i][0]]!= S[ijk[i][2]] and
       S[ijk[i][1]]!= S[ijk[i][2]]):
       f+=1
print(f)

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

sum=0
if len(r)*len(g)*len(b)==0:
    print(0)
else:
    for re in r:
        for gr in g:
            cnt=0
            re1=re
            re1,gr=min(re,gr),max(re,gr)
            #print(re,gr,b)
            if (re1+gr)%2==0:
                if (re1+gr)//2 in b:
                    cnt-=1
            if 2*gr-re1 in b:
                cnt-=1
            if 2*re1-gr in b:
                cnt-=1
            sum+=cnt+len(b)
            #print(cnt)
    print(sum)

Submission status: Time Limit Exceeded

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

idx = {'R': 0, 'G': 1, 'B': 2}
cums = [[0] * N for _ in range(3)]

for i in range(N):
    cums[idx[S[i]]][i] = 1

for cum in cums:
    for i in range(1, N):
        cum[i] += cum[i - 1]

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

        sk = 'R' if S[i] == 'G' and S[j] == 'B' or S[j] == 'G' and S[i] == 'B'\
             else 'G' if S[i] == 'B' and S[j] == 'R' or S[j] == 'B' and S[i] == 'R'\
             else 'B' 

        sum += cums[idx[sk]][N - 1] - cums[idx[sk]][j]
        if j * 2 - i < N and S[j * 2 - i] == sk:
            sum -= 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s=input()
r=[]
g=[]
b=[]
for i,c in enumerate(s):
    if 'R' == c:
        r.append(i+1)
    elif 'G' == c:
        g.append(i+1)
    elif 'B' == c:
        b.append(i+1)
count=0
for i in r:
    for j in g:
        for k in b:
            l=sorted([i,j,k])
            if l[1]*2-l[0]-l[2]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=input()
b=[str(c) for c in a]
sum=0
for i in range(n):
  for j in range(n):
    for k in range(n):
      if i<j<k:
        if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
          if (j-i)!=(k-j):
            sum+=1
print(int(sum))

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(int(i))
    elif S[i]=="G":
        G.append(int(i))
    else:
        B.append(int(i))
 
ans = len(R)*len(G)*len(B)

for i in R:
    for j in G:
        if max(i,j)+abs(i-j) in B:
            ans-=1
        if min(i,j)-abs(i-j) in B:
            ans-=1
        if  (i+j)/2 in B:
            ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
#listG = [j for j, x in enumerate(S) if x == 'G']
#listB = [k for k, x in enumerate(S) if x == 'B']
SUMSUM = 0
for a in range(N-2):
    #Rから
    if S[a] == "R":
        for b in range (N -2 - a):
            k = 0
            if S[a+b+1] == "G":
                k = S[(a+b+2):].count("B")
                if a+b+2+b <= (N-1):
                    if S[a+b+b+2] == "B":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "B":
                k = S[(a+b+2):].count("G")
                if a+b+2+b <= (N-1):
                    if S[a+b+b+2] == "G":
                        SUMSUM -= 1
                SUMSUM += k
    elif S[a] == "G":
        k = 0
        for b in range (N -2 - a):
            if S[a+b+1] == "B":
                k = S[(a+b+2):].count("R")
                if a+b+b+2 <= (N-1):
                    if S[a+b+b+2] == "R":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "R":
                k = S[(a+b+2):].count("B")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+b+2] == "B":
                        SUMSUM -= 1
                SUMSUM += k
    elif S[a] == "B":
        k = 0
        for b in range (N -2 - a):
            if S[a+b+1] == "G":
                k = S[a+b+2:].count("R")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+b+2] == "R":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "R":
                k = S[a+b+2:].count("G")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+2+b] == "G":
                        SUMSUM -= 1
                SUMSUM += k
print(SUMSUM)

Submission status: Time Limit Exceeded

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

num_r, num_g = S.count('R'), S.count('G')
num_b = N - num_r - num_g
max_ans = num_r * num_g * num_b
count_not_ans = 0
S = list(S)

for i in range(N-2):
    for j in range(i+1, N-1):
        k = 2 * j - i
        if (0 <= k <= N-1) and len(set([S[i], S[j], S[k]])) == 3:
            count_not_ans += 1

print(max_ans - count_not_ans)

Submission status: Time Limit Exceeded

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

dict_x = {"R":0, "G":1, "B":2}
s = [dict_x[s] for s in s]

result = 0

for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        sum_ij = s[i] + s[j]
        for k in range(j + 1, n):
            if (j - i) != (k - j):
                if sum_ij + s[k] == 3:
                    result += 1

print(result)

Submission status: Time Limit Exceeded

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

import time
t1 = time.time()
# ******************************************************************
import itertools

counta = 0

R = []
G = []
B = []

for i, x in enumerate(S):
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i)

# print(len([0 for i in itertools.product(R, G, B) if sorted(i)[1] * 3 != sum(i)]))
print(len(R) * len(G) * len(B) - len([0 for i in itertools.product(R, G, B) if sorted(i)[1] * 3 == sum(i)]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x =="R":
        r += 1
    elif x == "G":
        g += 1
    else:
        b += 1
ans = r * g * b
for i in range(1, 1+n//2):
    for j in range(1, n-1):
        if j + i*2 <= n:
            if s[j-1] != s[j+i-1] and s[j-1] != s[j+i*2-1] and s[j+i-1] != s[j+i*2-1]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

def d162(n , sli):

    ans = 0

    for i in range(n - 3):
        for w in range(i + 1 , n):
            for q in range(w + 1 , n):
                if sli[i] != sli[w] and sli[i] != sli[q] and sli[w] != sli[q] and w - i != q - w:
                    ans += 1
    return ans

def main():
    n = int(input())
    sli = list(str(input()))
    print(d162(n , sli))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

counta = 0

R = []
G = []
B = []

for i, x in enumerate(S):
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i)

print(len([0 for i in itertools.product(R, G, B) if sorted(i)[1] * 3 != sum(i)]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str, input()))
f=0
i_c = j_c = k_c =1
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k_c>j_c and j_c>i_c):
                if(j_c-i_c!=k_c-j_c):
                    ijk.append([i,j,k])
            k_c+=1
        j_c+=1
        k_c = 1
    i_c+=1
    j_c = 1
for i in range(len(ijk)):
    if(S[ijk[i][0]]!= S[ijk[i][1]] and
       S[ijk[i][0]]!= S[ijk[i][2]] and
       S[ijk[i][1]]!= S[ijk[i][2]]):
       f+=1
print(f)

Submission status: Time Limit Exceeded

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

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

ans = r * g * b

for i in range(0,n-2):
    for j in range(1,int(n/2)+1):
        if i + j + j <= n-1:
            if s[i] != s[i+j] and s[i] != s[i+j+j] and s[i+j] != s[i+j+j] :
                ans = ans - 1

print(ans)

Submission status: Time Limit Exceeded

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

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

print(str(ans))

Submission status: Time Limit Exceeded

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

c = 0

for k in r:
    for j in g:
        for i in b:
            a = []
            a.append(k)
            a.append(j)
            a.append(i)
            a.sort()
            if a[1] - a[0] != a[2] - a[1]:
                c += 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
str_length = int(input())
rgb_str = str(input())

flag = 0
for num_1 in range(1, str_length + 1):
	for num_2 in range(num_1 + 1, str_length + 1):
		for num_3 in range(num_2 + 1, str_length + 1):
			# print("result :{} {} {}".format(num_1, num_2, num_3))
			if num_2 - num_1 != num_3 - num_2:
				if rgb_str[num_1 - 1] != rgb_str[num_2 - 1] and rgb_str[num_2 - 1] != rgb_str[num_3 - 1] and rgb_str[num_1 - 1] != rgb_str[num_3 - 1]:
					# print("↑bingo!!!\n")
					flag += 1
				else:
					continue
			else:
				continue
print(str(flag))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import itertools
count = 0
N = int(input())
slist = deque(list(input()))
maslist = ["R", "G", "B"]
lists = maslist.copy()
for a in range(N-2):
    lists.remove(slist[a])
    for b in range(a+1, N-1):
        if slist[b] in lists:
            lists.remove(slist[b])
            t=deque(itertools.islice(slist,b+1,N)).count(lists[0])
            count+=t
            lists.append(slist[b])
    lists = maslist.copy()
   
for a in range(N-2):
    for b in range(a+1,N):
        if 2*b-a<N and slist[a]!=slist[b] and slist[b]!=slist[2*b-a] and slist[a]!=slist[2*b-a]:
                count -= 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_S = list(input())
list_R = []
list_B = []
list_G = []
ans = 0
for i, c in enumerate(list_S):
    if c == "R":
        list_R.append(i+1)
    if c == "B":
        list_B.append(i+1)
    if c == "G":
        list_G.append(i+1)
 
for i in list_R:
    for j in list_G:
        for k in list_B:
            l = [i,j,k]
            l.sort()
            if l[1]-l[0] != l[2]-l[1]:
                ans += 1
 
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
ss=input()
l=[["R","G","B"],["R","B","G"],["G","R","B"],["G","B","R"],["B","R","G"],["B","G","R"]]
ans=0
for v in l:
    for i in range(n-2):
        if ss[i]==v[0]:
            for k in range(i+1,n-1):
                if ss[k]==v[1]:
                    ans+=ss[k+1:].count(v[2])
                    if (k+k-i <=n-1) and ss[k+k-i]==v[2]:
                        ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
ans=0
R=[]
G=[]
B=[]
for i in range(N):
    if S[i]=="R":
        R.append(i+1)
    elif S[i]=="G":
        G.append(i+1)
    else:
        B.append(i+1)
lenb=len(B)
for r in R:
    for g in G:
        flag=0
        left=max(min(r,g)-abs(r-g),0)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for b in B:
            if b>right:
                break
            if b==right or b==left or b==middle:
                flag=flag+1
        ans=ans+lenb-flag
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

rgb = [[], [], []]

for i, c in enumerate(s):
    if c == 'R':
        rgb[0].append(i)
    elif c == 'G':
        rgb[1].append(i)
    else:
        rgb[2].append(i)

comb = len(rgb[0]) * len(rgb[1]) * len(rgb[2])

summ = 0
for i in rgb[0]:
    for j in rgb[1]:
        for k in rgb[2]:
            ii = min(i, j, k)
            kk = max(i, j, k)
            jj = sum([i, j, k]) - ii - kk
            if jj - ii == kk - jj:
                summ += 1

print(comb - summ)

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):
        if S[i] == S[j]: continue
        for k in range(j + 1, k2):
            if j - i == k - j: continue
            if S[i] != S[k] and S[j] != S[k]:
                count += 1
# num_R = S.count('R')
# num_G = S.count('G')
# num_B = S.count('B')

# num_R * num_G * num_B / (len(S) ** 3)

# print(3 ** len(S), num_R * num_G * num_B / (len(S) ** 3), num_R, num_G, num_B)

print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def get_result(data):
    # N = int(data[0][0])
    S = data[1][0]
    r_index = [i for i in range(len(S)) if S[i] == 'R']
    g_index = [i for i in range(len(S)) if S[i] == 'G']
    b_index = [i for i in range(len(S)) if S[i] == 'B']
    all_case = len(r_index) * len(g_index) * len(b_index)
    invalid_case = 0
    for i in r_index:
        for j in g_index:
            s_i, s_j = sorted([i, j])
            tmp_max_b = s_j + (s_j-s_i)
            if tmp_max_b in b_index:
                invalid_case += 1
            tmp_min_b = s_i - (s_j-s_i)
            if tmp_min_b in b_index:
                invalid_case += 1
            tmp_mid_b = (s_i + s_j) / 2
            if tmp_mid_b in b_index:
                invalid_case += 1
    return all_case - invalid_case


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Time Limit Exceeded

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


def main():
    count = 0
    char_candidates = "RGB"
    for i in range(len(s)):
        i_char = s[i]

        indexes =  [a+i+1 for a, c in enumerate(s[i+1:]) if c != i_char]
        for j in indexes:
            j_char = s[j]
            
            if (i_char == "R" and j_char == "G") or (i_char == "G" and j_char == "R"):
                valid_k_char = "B"
            if (i_char == "R" and j_char == "B") or (i_char == "B" and j_char == "R"):
                valid_k_char = "G"
            if (i_char == "G" and j_char == "B") or (i_char == "B" and j_char == "G"):
                valid_k_char = "R"

            target_chars = s[j+1:]
            match_count = target_chars.count(valid_k_char)

            if j + j - i < len(s) and s[j + j - i] == valid_k_char:
                match_count -= 1

            count += match_count

    print(count)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
str_length = int(input())
rgb_str = str(input())

flag = 0
for num_1 in range(1, str_length + 1):
	for num_2 in range(num_1 + 1, str_length + 1):
		for num_3 in range(num_2 + 1, str_length + 1):
			# print("result :{} {} {}".format(num_1, num_2, num_3))
			if rgb_str[num_1 - 1] != rgb_str[num_2 - 1] and rgb_str[num_2 - 1] != rgb_str[num_3 - 1] and rgb_str[num_1 - 1] != rgb_str[num_3 - 1]:
				if num_2 - num_1 != num_3 - num_2:
					# print("↑bingo!!!\n")
					flag += 1
				else:
					continue
			else:
				continue
print(str(flag))

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(int(i))
    elif S[i]=="G":
        G.append(int(i))
    else:
        B.append(int(i))
 
ans = len(R)*len(G)*len(B)

for i in range(len(R)):
    for j in range(len(G)):
        if max(R[i],G[j])+abs(R[i]-G[j]) in B:
            ans-=1
        if min(R[i],G[j])-abs(R[i]-G[j]) in B:
            ans-=1
        if  (R[i]+G[j])/2 in B:
            ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

counta = 0
i = 0

R = []
G = []
B = []

for i, x in enumerate(S):
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i)

print(len([i for i in itertools.product(R, G, B) if sorted(i)[1] * 3 != sum(i)]))

Submission status: Time Limit Exceeded

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

num_r, num_g = S.count('R'), S.count('G')
num_b = N - num_r - num_g
max_ans = num_r * num_g * num_b
count_not_ans = 0

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

print(max_ans - count_not_ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    ans = 0
    for i in range(N-2):
        for j in range(i, N-1):
            if S[i] == S[j]:
                continue
            for k in range(j, N):
                if S[i] != S[k] and S[j] != S[k]:
                    if j - i == k - j:
                        continue
                    ans += 1

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0

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

      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())  # N<= 4000 6*10^10なので、削減しないとだめ
S = list(input())


# Si Sj Skがどれも別のもの。ただしi<j<k
# また、j-i != k-j
# 1,2,3はだめ1,2,4はok
# 1,3,5もだめ
# この組の数を求める

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


print(count)

Submission status: Time Limit Exceeded

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

total = len(Rindexes) * len(Gindexes) * len(Bindexes)
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2*j - i
    if S[i] != S[j]:
      if S[i] != 'R' and S[j] != 'R' and k in Rindexes:
        count += 1
      elif S[i] != 'G' and S[j] != 'G' and k in Gindexes:
        count += 1
      elif S[i] != 'B' and S[j] != 'B' and k in Bindexes:
        count += 1
print(total - count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

counta = 0
i = 0

R = []
G = []
B = []

for i, x in enumerate(S):
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i)

print(len([i for i in itertools.product(R, G, B) if sorted(i)[1] * 3 != sum(i)]))

Submission status: Time Limit Exceeded

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

R = np.where(S == 'R')[0]
G = np.where(S == 'G')[0]
B = np.where(S == 'B')[0]
ans = 0
for r in R:
    for g in G:
        for b in B:
            mi, mid, ma = sorted([r, g, b])
            if mid - mi != ma - mid:
                ans +=1
print(ans)