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 = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            for ib in range(ia+1,n-1):
                if sg[ib]:
                    r += sum(sb[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sb[ib * 2 - ia]
                elif sb[ib]:
                    r += sum(sg[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sg[ib * 2 - ia]
        elif ta == 'G':
            for ib in range(ia+1,n-1):
                if sb[ib]:
                    r += sum(sr[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sr[ib * 2 - ia]
                elif sr[ib]:
                    r += sum(sb[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sb[ib * 2 - ia]
        else:
            for ib in range(ia+1,n-1):
                if sr[ib]:
                    r += sum(sg[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sg[ib * 2 - ia]
                elif sg[ib]:
                    r += sum(sr[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sr[ib * 2 - ia]

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

cnt = 0
for i in range(0, n-2):
    for j in range(n-1, i+1, -1):
        if s[i] == 'R' and s[j] == 'G':
            cnt += b[j] - b[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'B':
                cnt -= 1
        elif s[i] == 'G' and s[j] == 'R':
            cnt += b[j] - b[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'B':
                cnt -= 1
        elif s[i] == 'R' and s[j] == 'B':
            cnt += g[j] - g[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'G':
                cnt -= 1
        elif s[i] == 'B' and s[j] == 'R':
            cnt += g[j] - g[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'G':
                cnt -= 1
        elif s[i] == 'G' and s[j] == 'B':
            cnt += r[j] - r[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'R':
                cnt -= 1
        elif s[i] == 'B' and s[j] == 'G':
            cnt += r[j] - r[i]
            if (i+j) % 2 == 0 and s[(i+j)//2] == 'R':
                cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

        for k in range(j+1, n):
            c = s[k]
            if a == c or b == c or (j-i == k-j):
                continue

            ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

RGB = list(S)
count = 0

def index_Multi(List,liter):
    #Listはリスト本体・literは検索したい文字
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L
    
r_list = index_Multi(RGB,"R")
g_list = index_Multi(RGB,"G")
b_list = index_Multi(RGB,"B")
for index, a in enumerate(RGB):
    if a == "R":
        for g in g_list:
            for b in b_list:
                if index < g and g < b and g - index != b - g:
                    count += 1
        for g in b_list:
            for b in g_list:
                if index < g and g < b and g - index != b - g:
                    count += 1
                    
    elif a == "G":
        for g in b_list:
            for b in r_list:
                if index < g and g < b and g - index != b - g:
                    count += 1
        for g in r_list:
            for b in b_list:
                if index < g and g < b and g - index != b - g:
                    count += 1
                    
    elif a == "B":
        for g in r_list:
            for b in g_list:
                if index < g and g < b and g - index != b - g:
                    count += 1
                    
        for g in g_list:
            for b in r_list:
                if index < g and g < b and g - index != b - g:
                    count += 1            
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
list1=[]
for a in s:
    list1.append(a)
u=0
for i in range(1,n+1):
    for j in range(1,n+1):
        for h in range(1,n+1):
            i1=list1[i-1]
            j1=list1[j-1]
            h1=list1[h-1]
            if i1!=j1 and i1!=h1 and j1!=h1 and (j-i)!=(h-j) and h+1>j+1 and j+1>i+1:
                u+=1
print(u)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    # min_len={len(r):r,len(g):g,len(b):b}
    # tmp=list(min_len.keys()).sort()
    for i in (r):
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

Submission status: Time Limit Exceeded

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

Rcnt = 0
Gcnt = 0

excep = 0

for i in range(len(S)):
    if S[i] == 'R':
        Rcnt += 1
    elif S[i] == 'G':
        Gcnt += 1
    for j in range(1, len(S)):
        if i+j+j < len(S) and S[i] != S[i+j]:
            if S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                excep += 1

RGBcalc = Rcnt * Gcnt * (N - Rcnt - Gcnt)

print(RGBcalc - excep)

Submission status: Time Limit Exceeded

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    for k in range(j+1, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1)] = total
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        if a+str(j) in check:
            total += check[a+str(j)]
            break
        if a != s[j]:
            total += get_pattern2(i, j)
    check[a+str(i+1)] = total

    # k-j = i-jなものをぬく
    for j in range(i+1, n):
        k = j + j - i
        if k < n:
            if a != s[k] and s[j] != s[k] and a != s[j]:
                total -= 1

    return total

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

Submission status: Time Limit Exceeded

Solution code:
s=int(input())
t=input()
a=0
b=0
c=0
d=[]
e=[]
f=[]
for i in range(s):
  if t[i]=='R':
    a=a+1
    d.append(i)
  elif t[i]=='G':
    b=b+1
    e.append(i)
  else:
    c=c+1
    f.append(i)
g=a*b*c
for j in range(len(d)):
  for k in range(len(e)):
    for l in range(len(f)):
      p=max(d[j],e[k],f[l])
      q=min(d[j],e[k],f[l])
      r=d[j]+e[k]+f[l]
      r=r-p-q
      if 2*r==p+q:
        g=g-1
print(g)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b

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

print(ans)

Submission status: Time Limit Exceeded

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

import itertools
lis = list(range(1, N+1))
c = 0

s = [1 for v in itertools.combinations(lis, 3)
     if v[1] - v[0] != v[2] - v[1] and S[v[0]-1] != S[v[1]-1] and S[v[0]-1] != S[v[2]-1] and S[v[1]-1] != S[v[2]-1]]

print(len(s))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = input()
count = 0
list1 = []
list = []
list2= []
for i in range(1, len(s)+1):
    list.append(i)
for c in combinations(list, 3):
    list1.append(c)
for item in list1:

    if (item[1]-item[0]) != (item[2]-item[1]):
        list2.append(item)
for k in list2:
    if s[k[0]-1]!=s[k[1]-1] and s[k[0]-1]!=s[k[2]-1] and s[k[1]-1]!=s[k[2]-1]:
        count= count+1
print(count)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(N):
    S_i = S[i]
    for j in range(i+1,N):
        S_j = S[j]
        if S_i != S_j:
            k_list = list(range(j+1,N))
            if (j-i-1) < len(k_list):
                del k_list[j-i-1]
            for k in k_list:
                if S[k] != S_i and S[k] != S_j:
                    count += 1
                

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for k in range(n):
    for j in range(n):
        for i in range(n):
            if i < j < k:
                a = (s[i], s[j], s[k])
                #print(a)
                a_set = set(a)
                #print(a_set)
                if len(a_set) == 3 and (j - i) != (k - j):
                    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
inputs = open(0).readlines()
n = int(inputs[0])
S = inputs[1].strip()

R = [i for i, c in enumerate(S) if c == 'R']
G = [i for i, c in enumerate(S) if c == 'G']
B = [i for i, c in enumerate(S) if c == 'B']

def f(Z, i, x, y):
    for z in Z[i:]:
        if z == 2*y-x:
            return 1
        if z > 2*y-x:
            return 0
    return 0

ans = 0
for X, Y, Z in list(permutations([R, G, B])):
    for x in X:
        pi = 0
        for y in Y:
            if x < y:
                for i, z in enumerate(Z[pi:], pi):
                    if y < z:
                        ans += len(Z) - i - f(Z, i, x, y)
                        pi = i
                        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
a = 0
line = list(S)

for i in range(0, N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      if (line[i] != line[j]) and (line[i] != line[k]) and (line[j] != line[k]) and (j - i != k - j):
        a += 1

print(a)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right,bisect_left
N = int(input())
S = input().strip()
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)
cnt = 0
for i in range(N):
    if i in R:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(B,indg)
            if 2*indg-indr not in B:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(G,indb)
            if 2*indb-indr not in G:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
    elif i in G:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(R,indb)
            if 2*indb-indg not in R:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(B,indr)
            if 2*indr-indg not in B:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
    elif i in B:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(G,indr)
            if 2*indr-indb not in G:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(R,indg)
            if 2*indg-indb not in R:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
print(cnt)

Submission status: Time Limit Exceeded

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

ans=0
RGB=[]
for i in range(N-2):
  for j in range(i+1,N-1):
    if S[i]!=S[j]:
      if not 'R' in [S[i],S[j]]:
        ans+=S[j+1:].count('R')
        if 2*j-i<N and S[2*j-i]=='R':
          ans-=1
      elif not 'G' in [S[i],S[j]]:
        ans+=S[j+1:].count('G')
        if 2*j-i<N and S[2*j-i]=='G':
          ans-=1
      else:
        ans+=S[j+1:].count('B')
        if 2*j-i<N and S[2*j-i]=='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):
        for k in range(j+1,N):
            if (j-i)!=(k-j) and S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                    ans+=1
         
                
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

_sum=0
for i in range(N):
    for j in range(N):
        for k in range(N):
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                if j-i != k-j and i<j and j<k: 
                    _sum+=1
print(_sum)

Submission status: Time Limit Exceeded

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

from collections import Counter

ct = Counter(s)
res = ct['R'] * ct['G'] * ct['B']

for ni,i in enumerate(s[:-2]):
    lst3 = ['R','G','B']
    lst3.remove(i)
    for nj in range(ni+1,n):
        nk = nj + (nj - ni)
        if nk < n:
            if set(lst3) == set([s[nj],s[nk]]):
                res -= 1
        else:
            break

print(res)

Submission status: Time Limit Exceeded

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


ridxlist = []
gidxlist = []
bidxlist = []

for i in range(len(S)):
    if S[i] == 'R':
        ridxlist.append(i)
    if S[i] == 'G':
        gidxlist.append(i)
    if S[i] == 'B':
        bidxlist.append(i)

cnt = 0
rgbs = set()
for r in ridxlist:
    for g in gidxlist:
        for b in bidxlist:
            if abs(r - g) != abs(g - b) and abs(r - g) != abs(r - b) and abs(g - b) != abs(r - b):
                rgbs.add(tuple(sorted([r,g,b])))
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

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

for i,s in enumerate(S):
    if s == "R":
        R.append(i)
    if s == "G":
        G.append(i)
    if s == "B":
        B.append(i)

ans = 0

for i in R:
    for j in G:
        for k in B:
            if i < j < k and j-i != k-j:
                ans += 1

            if j < i < k and i-j != k-i:
                ans += 1

            if j < k < i and k-j != i-k:
                ans += 1

            if i < k < j and k-i != j-k:
                ans += 1

            if k < i < j and i-k != j-i:
                ans += 1

            if k < j < i and j-k != i-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
S=input()
r=[]
g=[]
b=[]
A=0
B=0
C=0
for i in range(N):
    if S[i]=="R":
        r.append(i)
        A+=1
    elif S[i]=="G":
        g.append(i)
        B+=1
    else:
        b.append(i)
        C+=1
k=0


for i in range(A):
    x = r[i]
    for j in range(B):

        y=g[j]
        if 2*x-y in b:
            k+=1
        if 2*y-x in b:
            k+=1
        if (x+y)/2 in b:
            k+=1


print(A*B*C-k)

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np

def main():
    ##get int
    first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    #first_input = input().split(" ")
    second_input = input().split(" ")

    N = first_input[0]
    S = second_input[0]

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

    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

def main2():
    input()
    t = input()
    pat = []
    for i in range(len(t)):
        if t[i] == 'R':
            pat.append(0)
        if t[i] == 'G':
            pat.append(1)
        if t[i] == 'B':
            pat.append(2)
    count = 0
    for i in range(len(pat)):
        for j in range(i + 1, len(pat)):
            for k in range(j + 1, len(pat)):
                tmp = {pat[i], pat[k], pat[j]}
                if j - i != k - j and len(tmp) == 3:
                    count += 1
    print(count)


if __name__ == '__main__':
    main2()

Submission status: Time Limit Exceeded

Solution code:
# from numba import njit

# @njit
# def regular_intervals(r,g,b):
#     i,j,k = sorted([r,b,g])
#     if k-j == j-i:
#         return True
#     else:
#         return False


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

Rids = []
Gids = []
Bids = []

for i,c in enumerate(S):
    if c == 'R':
        Rids.append(i)
    elif c == 'G':
        Gids.append(i)
    elif c == 'B':
        Bids.append(i)

# print(sum(not regular_intervals(r,g,b) for r in Rids
#               for g in Gids
#               for b in Bids))

# print(sum(abs(r-b) != abs(b-g) and abs(b-g) != abs(g-r) and abs(g-r) != abs(r-b)
#           for r in Rids
#           for g in Gids
#           for b in Bids))

# print(sum(len(set((abs(r-b), abs(b-g), abs(g-r)))) == 3
#           for r in Rids
#           for g in Gids
#           for b in Bids))

# ans = 0
# for r in Rids:
#     for g in Gids:
#         diff_rg = abs(r-g)
#         for b in Bids:
#             if (diff_rb := abs(r-b)) == diff_rg:
#                 continue
#             elif (diff_gb := abs(g-b)) == diff_rg:
#                 continue
#             # elif abs(r-b) == abs(g-b):
#             elif diff_rb == diff_gb:
#                 continue
#             else:
#                 ans +=1
# print(ans)


ans = 0
for r in Rids:
    for g in Gids:
        d = abs(r-g)

        l = max(r,g)
        s = min(r,g)
        ll = l + d
        ss = s - d
        m = (l+s)/2
        ans += len(set(Bids).difference({ll,ss,m}))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
ans_list = []
for j in range(1, n):
    for i in range(j):
        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
                ans_list.append([i,j,k])
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
if __name__ == '__main__':
    input = sys.stdin.readline
    n = int(input())
    s = list(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)
    x = len(b)
    ans = 0
    for i in range(n):
        for j in range(i, n):
            if s[i] == s[j]:
                continue
            if s[i] == "R" and s[j] == "G":
                out = j * 2 - i
                tmp = bisect.bisect_left(b, j)
                t = len(b) - tmp
                ans += (t - 1) if out in b[tmp:] else t

            elif s[i] == "R" and s[j] == "B":
                out = j * 2 - i
                tmp = bisect.bisect_left(g, j)
                t = len(g) - tmp
                ans += (t - 1) if out in g[tmp:] else t

            elif s[i] == "G" and s[j] == "B":
                out = j * 2 - i
                tmp = bisect.bisect_left(r, j)
                t = len(r) - tmp
                ans += (t - 1) if out in r[tmp:] else t

            elif s[i] == "G" and s[j] == "R":
                out = j * 2 - i
                tmp = bisect.bisect_left(b, j)
                t = len(b) - tmp
                ans += (t - 1) if out in b[tmp:] else t

            elif s[i] == "B" and s[j] == "R":
                out = j * 2 - i
                tmp = bisect.bisect_left(g, j)
                t = len(g) - tmp
                ans += (t - 1) if out in g[tmp:] else t

            elif s[i] == "B" and s[j] == "G":
                out = j * 2 - i
                tmp = bisect.bisect_left(r, j)
                t = len(r) - tmp
                ans += (t - 1) if out in r[tmp:] else t

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from itertools import combinations
N=int(input())
S=list(input())
Pattern=[x for x in combinations(range(N),3) if (len(set(S[y] for y in x))==3 and x[2]-x[1] != x[1]-x[0])]
print(len(Pattern))

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
a = 0
b = S.count("R")*S.count("B")*S.count("G")
for i in range(1,len(S)+1):
    for j in range(i,len(S)+1):

        k = 2*j-i
        if k >= len(S)+1:
            continue
        else:
            if not S[i-1] == S[k-1] and not S[k-1] == S[j-1] and not S[i-1] ==S[j-1]:
                    a = a+1
print(b-a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
rset = set()
gset = set()
bset = set()

for i in range(N):
    if S[i] == "R":
        rset.add(i)
    if S[i] == "G":
        gset.add(i)
    if S[i] == "B":
        bset.add(i)
ans = 0
for i in range(0, N - 2):
    if i in rset:
        j_candidate = gset | bset
        k_candidate = gset | bset
    if i in gset:
        j_candidate = rset | bset
        k_candidate = rset | bset
    if i in bset:
        j_candidate = gset | rset
        k_candidate = gset | rset
    j_candidate = set(range(i+1, N-1)) & j_candidate
    for j in j_candidate:
        k_candidate_save = k_candidate
        if j in rset:
            k_candidate = k_candidate - rset
        if j in gset:
            k_candidate = k_candidate - gset
        if j in bset:
            k_candidate = k_candidate - bset
        k_candidate = set(range(j+1, N)) & k_candidate
        k_candidate = k_candidate - set([j-i+j])
        ans += len(k_candidate)
        k_candidate = k_candidate_save
print(ans)

Submission status: Time Limit Exceeded

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

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

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
        
    l_useable = [ii for ii in l_col if ii<ind]
    r_useable = [ii for ii in r_col if ii>ind]
    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
  return n

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

print(n)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=input()
c=0
B1B=0
B1R=0
B1G=0
if A[0]=="B":
 B1B+=1
elif A[0]=="R":
 B1R+=1
else:
 B1G+=1
for i in range(N-2):
 B2=A[i+1]
 for k in range(N-i-2):
  B3=A[i+2+k]
  if (B2=="B" and B3=="R")or (B2=="R" and B3=="B"):
   c+=B1G
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="G":
     c-=1
  if (B2=="G" and B3=="R")or (B2=="R" and B3=="G"):
   c+=B1B
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="B":
     c-=1
  if (B2=="B" and B3=="G")or (B2=="G" and B3=="B"):
   c+=B1R
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="R":
     c-=1
 if B2=="B":
  B1B+=1
 elif B2=="R":
  B1R+=1
 else:
  B1G+=1
print(c)

Submission status: Time Limit Exceeded

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

for x in range(N):
    if S[x] =="R":
        R +=1
        S2.append(["R",R])
    elif S[x] =="G":
        G +=1
        S2.append(["G",G])
    else:
        B +=1
        S2.append(["B",B])
ans =0
a = 0
b =0
for x in range(N):
    i = S2[x][0]
    for y in range(x,N):
        j = S2[y][0]
        if j != i:
            if j =="R":
                a =R-S2[y][1]+1
            elif j =="G":
                a =G-S2[y][1]+1
            else:
                a =B-S2[y][1]+1
            for z in range(x,N):
                k = S2[z][0]
                if k != i and k != j:
                    if k =="R":
                        b =R-S2[z][1]+1
                    elif k =="G":
                        b =G-S2[z][1]+1
                    else:
                        b =B-S2[z][1]+1
                    break
            else:
                b = 0
            break
    else:
        a = 0
    ans +=a*b
subn =0
for x in range(N):
    i = S2[x][0]
    for y in range(x,N):
        j = S2[y][0]   
        if j != i:
            d = y-x
            if y+d<N:
                if i!= S2[y+d][0] and j!=S2[y+d][0]:
                    subn +=1
            else:
                break
print(ans-subn)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(len_s):
    for j in range(i+1, len_s):
        if s[i] == s[j]:
            continue
        for k in range(j+1, len_s):
            if (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=list(input())
cnt=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):
                cnt=cnt+1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N = int(input())
  S = input()
  ct = 0
  for i in range(N):
    for j in range(i+1,N):
      if(S[i] != S[j]):
        for k in range(j+1,N):
          if(S[j] != S[k] and S[k] != S[i] and j-i != k-j):
            ct += 1
  print(ct)
  return 0
 
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():
  n=int(input())
  s=str(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)
  ans=0
  for l in r:
    for m in g:
      for k in b:
        if abs(m-l)!=abs(k-m) and abs(k-l)!=abs(k-m) and abs(k-l)!=abs(l-m):
          ans+=1
  print(ans)
main()