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()
ans = 0
r = []
g = []
b = []
for i in range(N):
    if(s[i] == "R"):
        r.append(i)
    elif(s[i] == "G"):
        g.append(i)
    elif(s[i] == "B"):
        b.append(i)
ans = len(r)*len(g)*len(b)
cta = 0
ctb = 0
ctc = 0
for i in range(len(r)):
    for j in range(len(g)):
        if(int((r[i] + g[j]))/2 in b) == True:
            ans -= 1
            cta += 1
        if (int((2*r[i] -g[j])) in b) == True: 
            ans -= 1
            ctb += 1
        if (int((2*g[j] - r[i])) in b) == True:
            ans -= 1
            ctc += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
A = 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[i] != S[k] and S[j] != S[k]:
        A += 1
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r_list = []
g_list = []
b_list = []
for _ in range(n):
    if s[_] == "R":
        r_list.append(_)
    elif s[_] == "G":
        g_list.append(_)

count = 0
for r in r_list:
    for g in g_list:
        if min(r, g) - abs(r - g) > 0:
            if s[min(r, g) - abs(r - g)] == "B":
                count += 1
        if max(r, g) + abs(r - g) < n:
            if s[max(r, g) + abs(r - g)] == "B":
                count += 1
        if (r + g) % 2 == 0:
            if s[(r + g) // 2] == "B":
                count += 1


print(len(r_list) * len(g_list) * (n - len(r_list) - len(g_list)) - count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

after_cumsum_RGB = {}
R, G, B = np.zeros(N+1), np.zeros(N+1), np.zeros(N+1)

def rgb_hash(a):
  if S[i] == 'R':
    return 0
  elif S[i] == 'G':
    return 1
  elif S[i] == 'B':
    return 2
  
def ci_hash(a):
  if 'R' not in a:
    return 0
  elif 'G' not in a:
    return 1
  elif 'B' not in a:
    return 2

def c_hash(a):
  if 'R' not in a:
    return 'R'
  elif 'G' not in a:
    return 'G'
  elif 'B' not in a:
    return 'B'
  
for i in range(N):
  if S[i] == 'R':
    R[i] = 1
  if S[i] == 'G':
    G[i] = 1
  if S[i] == 'B':
    B[i] = 1
after_cumsum_RGB = {'R': R[::-1].cumsum()[::-1], 'G': G[::-1].cumsum()[::-1], 'B': B[::-1].cumsum()[::-1]}

ans = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      c = c_hash([S[i], S[j]])
      ans += after_cumsum_RGB[c][j+1]
      if 2*j-i <= N-1:
        if S[2*j-i] == c:
          ans -= 1
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

seq = list(range(1, N + 1))

c_list = list(itertools.combinations(seq, 3))
s_list = list(S)

sum = 0

for i, j, k in c_list:
    if s_list[i - 1] != s_list[j - 1] and s_list[i - 1] != s_list[k - 1] and s_list[j -1 ] != s_list[k - 1] and j - i != k - j:
        sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n= int(input())
    slis = tuple(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]
 
    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB
 
    if lenB <= lenG <= lenR:
        for i in lis_R:
            for j in lis_G:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
 
    elif lenB <= lenR <= lenG:
        for i in lis_G:
            for j in lis_R:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
 
    elif lenG <= lenB <= lenR:
        for i in lis_R:
            for j in lis_B:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenG <= lenR <= lenB:
        for i in lis_B:
            for j in lis_R:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenB <= lenG:
        for i in lis_G:
            for j in lis_B:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenG <= lenB:
        for i in lis_B:
            for j in lis_G:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    print(total)
 
 
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

A  = []
for i in range(n):
    if S[i] == "R":
        A.append(-1)
    elif S[i] == "G":
        A.append(-2)
    else:
        A.append(3)

cnt = 0
for i in range(0, n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if A[i]+A[j]+A[k]== 0:
                if (j - i) != (k - j):
                    cnt += 1

print(cnt)

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:
    if s=="R":
        if a>=i:
            for n in range(i,a):
                if c[n]=="G":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="B":
                                k=m-n
                                if k!=j:
                                    sum+=1
                if c[n]=="B":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="G":
                                k=m-n
                                if k!=j:
                                    sum+=1

    if s=="G":
        if a>=i:
            for n in range(i,a):
                if c[n]=="R":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="B":
                                k=m-n
                                if k!=j:
                                    sum+=1
                if c[n]=="B":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="R":
                                k=m-n
                                if k!=j:
                                    sum+=1

    if s=="B":
        if a>=i:
            for n in range(i,a):
                if c[n]=="R":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="G":
                                k=m-n
                                if k!=j:
                                    sum+=1
            for n in range(i,a):
                if c[n]=="G":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="R":
                                k=m-n
                                if k!=j:
                                    sum+=1


    i+=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
ans = 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[i] != s[k] and j-i != k-j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input("")
S = input("")
N = int(N)
X = 0

for i in range(N-3):
    j = i+1

    for jj in range(N):
        if not(S[j] == S[i]):
            k = j+1

            for kk in range(N):
                if not(S[k] == S[j] or S[k] == S[i]):
                    if j-i != k-j :
                        X += 1
                k += 1
                if k == N:
                    break            
        j += 1
        if j == N-1:
            break
print(X)

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//2)+1):
        if(j+j+i < N):
            if ((S[i] != S[i+j])):
                if(S[i] != S[i+j+j]):
                    if(S[i+j] != S[i+j+j]):
                        notToAllow = notToAllow + 1

print(R*G*B - notToAllow)

Submission status: Time Limit Exceeded

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

def solve(N: int, S: str):
    r = []
    g = []
    b = []
 
    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        else:b.append(i)

    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
  
    sb = set(b)
    ans = 0
    for r_ in r:
        # rg
        for g_ in g[g>r_]:
            # rgb
            if (g_ + (g_ - r_)) in sb:
                ans -= 1
            # brg
            if (r_ - (g_ - r_)) in sb:
                ans -= 1
            # rbg
            if ((r_ + g_)/2) in sb:
                ans -= 1

        # gr
        for g_ in g[g<r_]:
            # grb
            if (r_ + (r_ - g_)) in sb:
                ans -= 1
            # bgr
            if (g_ - (r_ - g_)) in sb:
                ans -= 1
            # gbr
            if ((r_ + g_)/2) in sb:
                ans -= 1
        ans += len(b) * len(g)
    print(ans)
  
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(0, N-3):
        si = S[i]
        for a in retA(si):
            idx1 = S.find(a, i)
            b = retB(a, si)
            while idx1 != -1:
                cnt += S[idx1+1:].count(b)
                if 2*idx1 - i < N:
                    if S[2*idx1 - i] == b:
                        cnt -= 1
                idx1 = S.find(a, idx1+1)

    print(cnt)

def retA(a):
    if a == 'R':
        A = ('G', 'B')
    elif a == 'G':
        A = ('R', 'B')
    elif a == 'B':
        A = ('R', 'G')
    return A

def retB(a, x):
    if (a == 'R' or x == 'R') and (a == 'G' or x == 'G'):
        b = 'B'
    elif (a == 'B' or x == 'B') and (a == 'G' or x == 'G'):
        b = 'R'
    elif (a == 'B' or x == 'B') and (a == 'R' or x == 'R'):
        b = 'G'
    return b

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
S = input()
S_R = []
S_G = []
S_B = []
for a in range(N):
  if(S[a]=="R"):
    S_R.append(a+1)
  elif(S[a]=="G"):
    S_G.append(a+1)
  else:
    S_B.append(a+1) 
ans=0
for i in range(len(S_R)):
  for j in range(len(S_G)):
    for k in range(len(S_B)):
      li = [S_R[i],S_G[j],S_B[k]]
      li.sort()
      if(li[1]-li[0] != li[2]-li[1]):
        ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
from numba import jit
if __name__ == '__main__':


    @jit
    def solve():
        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)
        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)
    solve()

Submission status: Time Limit Exceeded

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

total = 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 (j - i) == (k - j):
        continue
      if (S[i] == S[k]) | (S[j] == S[k]):
        continue
      total += 1
      
print(total)

Submission status: Time Limit Exceeded

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

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

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

result = 0
import itertools

results = [ (sorted(x)[1] - sorted(x)[0] != sorted(x)[2] - sorted(x)[1]) for x in itertools.product(R, G, B)]
print(sum(results))

Submission status: Time Limit Exceeded

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

c = 0

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

print ( c )

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:
            if index < g:
                for b in b_list:
                    if  g < b and g - index != b - g:
                        count += 1
        for g in b_list:
            if index < g:
                for b in g_list:
                    if g < b and g - index != b - g:
                        count += 1
                    
    elif a == "G":
        for g in b_list:
            if index < g: 
                for b in r_list:
                    if g < b and g - index != b - g:
                        count += 1  
        for g in r_list:
            if index < g:
                for b in b_list:
                    if  g < b and g - index != b - g:
                        count += 1
                    
    elif a == "B":
        for g in r_list:
            if index < g:
                for b in g_list:
                    if  g < b and g - index != b - g:
                        count += 1
                    
        for g in g_list:
            if index < g:
                for b in r_list:
                    if  g < b and g - index != b - g:
                        count += 1            
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    alls = set(range(n))
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = alls - r - g

    ans = 0
    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


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

Submission status: Time Limit Exceeded

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

import itertools
cnt = 0                                    
for v in itertools.combinations(range(0,N), 2):
    if S[v[0]] != S[v[1]]:
        for i in range(v[1]+1,N):
            if (v[1] -v[0]) != (i -v[1]):
                if S[v[0]] != S[i] and S[v[1]] != S[i]:
                    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i)
	elif s[i] == "G":
		g.append(i)
	else:
		b.append(i)
count = 0
x = []
y = []
z = []
for i in [r,g,b]:
	if len(i) == min([len(r),len(g),len(b)]) and x == []:
		x = i
	elif len(i) == max([len(r),len(g),len(b)]) and z == []:
		z = i
	else:
		y = i
for i in x:
	for j in y:
		u = abs(i-j)
		count += [0,1][(max(i,j)+u) in z] + [0,1][(min(i,j)-u) in z] + [0,1][u%2 == 0 and (i+j)//2 in z]
print(len(r)*len(g)*len(b)-count)

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):
        if i<j:
            if not s[i-1] == s[j-1]:
                for k in range((j+1),n+1):
                    if j<k:
                        if not j-i == k-j:
                            if 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:
#problem D
input_d41 = int(input())
input_d42 = input()

N = int(input_d41)
S = input_d42

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

ans = 0
answ = 0

for i in range(0,N):
    for j in range(i+1,N):
        if S[i] != S[j] and 2*j-i < N:
            if S[j] != S[2*j-i] and S[2*j-i] != S[i]:
                answ += 1
            
ans = R*G*B - answ

print(ans)

Submission status: Time Limit Exceeded

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

rgb = 'RGB'
ans = 0
for i in range(0, n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j]: continue
        k = rgb[:rgb.find(s[i])] + rgb[rgb.find(s[i])+1:]
        k = k[:k.find(s[j])] + k[k.find(s[j])+1:]
        r = s[j+1:2*j-i] + s[2*j-i+1:]
        ans += r.count(k)
print(ans)

Submission status: Time Limit Exceeded

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

#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

Slist = list(S)

count = 0
for i in range(len(Slist)):
    for j in range(i+1, len(Slist)):
        for k in range(j+1, len(Slist)):
            if(Slist[i] == Slist[j]):
                break
            notEqualColor = (Slist[i] == Slist[j] or Slist[j] == Slist[k] or Slist[k] == Slist[i])
            notEqualDistance = (j - i == k - j)

            if((notEqualColor or notEqualDistance) == False):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
for p in range(2,len(s)):
    for q in range(1,p):
        if s[p]==s[q]:
            continue
        for r in range(q):
            if s[q]==s[r] or p-q==q-r:
                continue
            if s[p]!=s[q] and s[p]!=s[r] and s[q]!=s[r] and p-q!=q-r:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from sys import stdin,setrecursionlimit
setrecursionlimit(10 ** 7)
n = int(stdin.readline().rstrip())
s = list(stdin.readline().rstrip())
li = [i for i in range(n)]
lin = list(combinations(li, 3))
point = 0
for i in lin:
    if s[i[0]] != s[i[1]] and s[i[2]] != s[i[1]] and s[i[0]] != s[i[2]]:
        if i[2]-i[1] != i[1]-i[0]:
            point += 1
print(point)

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):
        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 +=1

print(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(n):
        if i+2*j < n:
            if (s[i],s[i+j],s[i+2*j]) == ("R","G","B") or (s[i],s[i+j],s[i+2*j]) == ("R","B","G")or (s[i],s[i+j],s[i+2*j]) == ("G","B","R") or (s[i],s[i+j],s[i+2*j]) == ("G","R","B") or (s[i],s[i+j],s[i+2*j]) == ("B","G","R") or (s[i],s[i+j],s[i+2*j]) == ("B","R","G"):
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline
s =int(readline().rstrip().decode())
t = readline().rstrip().decode()
res = 0
for i in range(s):
    for j in range(s-i):
        for k  in range(s-i-j):
            #if i < j and j < k:
                if t[i] != t[j+i] and t[j+i] != t[k+i+j] and t[i] !=t[k+i+j] and j != k:
                    res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = "0"+input()
lr = [0]
lg = [0]
lb = [0]
for j in range(1,N+1):
  if S[j] == "R":
    i = j-1
    lr.append(lr[i]+1)
    lg.append(lg[i])
    lb.append(lb[i])
  if S[j] == "G":
    i = j-1
    lr.append(lr[i])
    lg.append(lg[i]+1)
    lb.append(lb[i])
  if S[j] == "B":
    i = j-1
    lr.append(lr[i])
    lg.append(lg[i])
    lb.append(lb[i]+1)

cnt = 0
mcnt = 0
for i in range(1,N+1):
  s = S[i]
  for j in range(1,N-i+1): 
    #Rパターン
    if s == "R":      
      if S[i+j] == "R":
        continue
      else:
        if i+j <= N and i-j >=0:
          if (S[i+j] == "B" and S[i-j] == "G") or (S[i+j] == "G" and S[i-j] == "B"):
            mcnt += -1 
        if S[i+j] == "G":
          cnt += lb[i-1]
        if S[i+j] == "B":
          cnt += lg[i-1]
    #Gパターン
    if s == "G":      
      if S[i+j] == "G":
        continue
      else:
        if i+j <= N and i-j >=0: 
          if (S[i+j] == "B" and S[i-j] == "R") or (S[i+j] == "R" and S[i-j] == "B"):
            mcnt += -1 
        if S[i+j] == "R":
          cnt += lb[i-1]
        if S[i+j] == "B": 
          cnt += lr[i-1]   
    #Bパターン
    if s == "B":      
      if S[i+j] == "B":
        continue
      else:
        if i+j <= N and i-j >=0: 
          if (S[i+j] == "G" and S[i-j] == "R") or (S[i+j] == "R" and S[i-j] == "G"):
            mcnt += -1 
        if S[i+j] == "R":
          cnt += lg[i-1]
        if S[i+j] == "G": 
          cnt += lr[i-1]  
print(cnt+mcnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

RR = []
GG = []
BB = []
for i in range(N):
  if(S[i] == 'R'):
    RR.append(i)
  elif(S[i] == 'G'):
    GG.append(i)
  else:
    BB.append(i)

count = 0

cyc = []
for r in RR:
  for g in GG:
    for b in BB:
      cyc = [r, g, b]
      cyc.sort()
      if(cyc[0] - cyc[1] != cyc[1] - cyc[2]):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
def match(i,j,k):
    cond0 = S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]
    cond1 = (j-i) != (k-j)
    return cond0 and cond1
def check():
    c=0
    for i in range(0,N-2):
        for j in range(i+1,N-1):
            for k in range(j+1,N):
                if match(i,j,k):
                    c+=1
    return c

print(check())

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 - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            x = j + j - i
            if x <= N - 1 and S[i] != S[x] and S[x] != S[j]:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

index = dict()
for color in ['R', 'G', 'B']:
    index[color] = [i for i, v in enumerate(s) if v == color]

result = 0
for r in index['R']:
    for g in index['G']:
        for b in index['B']:
            i, j, k = sorted([r, g, b])
            if j - i != k - j:
                result += 1

print(result)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
c = int(input())
a = input()
count = 0
for i in range(1, len(a)+1):
    for j in range(i + 1, len(a)+1):
        for k in range(j + 1, len(a)+1):
            if a[i-1] != a[j-1] and a[j-1] != a[k-1] and a[i-1] != a[k-1] and j - i != k - j:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
z = 0
lnr = [i for i, x in enumerate(s) if x == 'R']
lng = [i for i, x in enumerate(s) if x == 'G']
lnb = [i for i in range(n)]
lnb = list(set(lnb) - set(lng) - set(lnr))
for i in lnr:
    for j in lng:
        a = max(i, j)
        b = min(i, j)
        if a + a - b in lnb:
            z += 1
        if (a - b) % 2 == 0:
            if b + ((a - b)//2) in lnb:
                z += 1
        if b * 2 - a >=0:
            if b - (a - b) in lnb:
                z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = 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(0,int(n-i)):
        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 = input()
O = {'R':[], 'G':[], 'B':[]}
for i,c in enumerate(S):
    O[c].append(i)
ans = len(O['R'])*len(O['G'])*len(O['B'])
for i in O['R']:
    for j in O['G']:
        for k in O['B']:
            m = sorted([i,j,k])
            if m[2]-m[1] == m[1]-m[0]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
n = int(input())
s = input()
s_l = list(s)


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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
r = [i for i,j in enumerate(s) if j=='R']
g = [i for i,j in enumerate(s) if j=='G']
b = [i for i,j in enumerate(s) if j=='B']

for i in r:
    for j in g:
        for k in b:
            l = [i,j,k]
            l.sort()
            if l[1]-l[0] != l[2]-l[1]:
                ans += 1
            '''
            if j-i != k-j:
                ans += 1
            '''
print(ans)

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

rlen = len(Rids)
glen = len(Gids)
blen = len(Bids)

maxlen = max(rlen,blen,blen)

def solve(ids1, ids2, ids3): # 3:max
    ans = 0
    for r in ids1:
        for g in ids2:
            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(ids3).difference({ll,ss,m}))
    return ans

if rlen == maxlen:
    print(solve(Gids, Bids, Rids))
elif glen == maxlen:
    print(solve(Bids, Rids, Gids))
else:
    print(solve(Rids, Gids, Bids))