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()
Sl = list(S)
Nl = [int(x) for x in range(1,N+1)]
res =0
for (i,Ni) in zip(Sl,Nl):
    x = i
    Nx = Ni
    Sl2 = Sl[Nx:]
    Nl2 = Nl[Nx:]
    for (j,Nj) in zip(Sl2,Nl2):
        y = j
        Ny =Nj 
        Sl3 =Sl[Ny:]
        Nl3 =Nl[Ny:]
        for (k,Nk) in zip(Sl3,Nl3):
            z = k
            Nz = Nk
            Ma = Nz-Ny
            Mb = Ny-Nx
            judge = [x,y,z]
            alllen = len(judge)
            unilen = len(set(judge))
            if alllen == unilen:
                if Ma != Mb:
                    res =res+1
print(res)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
S = input()
sum = 0
for i in range(K-2):
  for j in range(i+1, K-1):
    if S[i] == S[j]:
      pass
    else:
      for k in range(j+1, K):
        if S[j] != S[k] and S[k] != S[i] and j - i != k - j:
          sum += 1
print(sum)

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

Submission status: Time Limit Exceeded

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def main():
    N = si()
    S = ss()
    ans = 0
    a = {}
    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
                else:
                    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    input()
    t = input()
    dic = {'R': [], 'G': [], 'B': []}
    for i in range(len(t)):
        dic[t[i]].append(i)
    count = 0
    for i in dic['R']:
        for j in dic['G']:
            for k in dic['B']:
                ids = sorted([i, j, k])
                if ids[1] - ids[0] != ids[2] - ids[1]:
                    count += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
R=[]
G=[]
B=[]
for i in range(n):
  if s[i]=="R":
    R.append(i)
  if s[i]=="G":
    G.append(i)
  if s[i]=="B":
    B.append(i)
    
r=len(R)
g=len(G)
b=len(B)
ans=0
for i in range(r):
  for j in range(g):
    for k in range(b):
      if R[i]-G[j]==G[j]-B[k]:
        continue
      if R[i]-B[k]==B[k]-G[j]:
        continue
      if G[j]-R[i]==R[i]-B[k]:
        continue
      else:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            sa = j - i
            for k in range(j+1, n):
                if s[k] != s[j] and s[k] != s[i] and sa != k - j:
                    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):
            tmp = set([S[i-1],S[j-1],S[k-1]])
            
            if (len(tmp)==3) & (j-i != k-j):
                count += 1
print(count)

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)

def func(a,b,c):
    ans = 0
    for i in a:
        for j in [num1 for num1 in b if num1 >i]:
            for k in [num2 for num2 in c if num2 > j]:
                if j-i != k-j:
                    ans += 1
    return ans
print(func(r,g,b) + func(r,b,g) + func(g,r,b) + func(g,b,r) + func(b,g,r) + func(b,r,g))

Submission status: Time Limit Exceeded

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

comibinations = [
    ['R', 'G', 'B'],
    ['R', 'B', 'G'],
    ['G', 'R', 'B'],
    ['G', 'B', 'R'],
    ['B', 'R', 'G'],
    ['B', 'G', 'R'],
]


# ind 番目以降
def search(ind, chars, indices):
    first = chars[0]
    rests = chars[1:]
    total = 0
    if len(rests) > 0:
        for k in range(ind, n):
            if s[k] == first:
                total += search(k + 1, rests, indices + [k])
    else:
        for k in range(ind, n):
            i, j = indices
            if s[k] == first and j - i != k - j:
                total += 1
            else:
                total += 0

    return total


total = 0
for chars in comibinations:
    total += search(0, chars, [])


print(total)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
# 1<=i<j<k<=n
# j-i!=k-j
# si!=sj, sj!=sk, si!=sk
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        #ans+=len(s[j+1:].replace(s[i],'').replace(s[j],''))
        ans+=sum([x!=s[i] and x!=s[j] for x in s[j+1:]])
        if 2*j-i < n and s[2*j-i] != s[i] and s[2*j-i] != s[j]:
            ans-=1
print(ans)

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)
res = 0
if max(len(r),len(g),len(b)) == len(r):
  for i in g:
    for j in b:
      flg = 0
      if (i + j) / 2 in r:
        flg += 1
      if 2 * i - j in r:
        flg += 1
      if 2 * j - i in r:
        flg += 1
      res += len(r) - flg
else:
  for i in g:
    for j in r:
      flg = 0
      if (i + j) / 2 in b:
        flg += 1
      if 2 * i - j in b:
        flg += 1
      if 2 * j - i in b:
        flg += 1
      res += len(b) - flg
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
R=[]
G=[]
B=[]
count=0
for i in range(n):
    if s[i]=="R":
        R.append(i)
    elif s[i]=="G":
        G.append(i)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            middle=r+g+b-min(r,g,b)-max(r,g,b)
            if (middle-min(r,g,b))!=(max(r,g,b)-middle):
                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

ans = 0

for i in range(0,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:
                    ans += 1

                    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
a = 0
for i in range(len(S)):
    for j in range(i,len(S)):
        if S[i] == S[j]:
            continue
        else:
            for l in range(j,len(S)):
                if not S[i] == S[l] and not S[j] == S[l]:
                    if not i-j == j-l:
                        a = a +1
                    
print(a)

Submission status: Time Limit Exceeded

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

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

print(count)

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]:
            for k in range(j+1,n):
                if s[k] not in (s[i],s[j]) and j-i != k-j:
                    cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s_list= list(input())
r_list=[]
g_list=[]
b_list=[]
count=0
for i in range(0,n):
    if s_list[i]=="R":
        r_list.append(i)
    elif s_list[i]=="G":
        g_list.append(i)
    else:
        b_list.append(i)

for pp in r_list:
    for qq in g_list:
        for rr in b_list:
            if rr-pp != -1*(qq-pp) and rr-pp !=2*(qq-pp) and qq-pp !=2*(rr-pp) :
                count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

ans=0
s=list(str(input()))
r=[]
b=[]
g=[]
for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='B':
        b.append(i)
    if s[i]=='G':
        g.append(i)
count=0
aida=1

while aida*2-1<=n:
    for i in range(n-aida*2):
        #print("DF")
       # print(s[i],i,s[i+aida],i+aida,s[i+aida*2],i+aida*2)
        aa=set()
        aa.add(s[i])
        aa.add(s[i+aida])
        aa.add(s[i+aida*2])
        #print(aa)
        if len(aa)==3:
            count+=1
    aida+=1
#print(count)

print(len(r)*len(b)*len(g)-count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
from bisect import bisect_left
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def counter(S, L1, L2, L3, C12, C23):
    ans = 0
    lenL2 = len(L2)
    lenL3 = len(L3)
    for ir in L1:
        # j = bisect_left(L2, ir)
        j = C12[ir]
        for _j in range(j, lenL2):
            ig = L2[_j]
            # k = bisect_left(L3, ig)
            k = C23[ig]
            ng = ig + (ig - ir)
            ans += lenL3 - k
            if ng in L3:
                ans -= 1
    return ans


def main():
    from builtins import int, map
    N = int(input())
    S = input()
    iR, iG, iB = [], [], []
    for i in range(N):
        if S[i] == 'R':
            iR.append(i)
        elif S[i] == 'G':
            iG.append(i)
        elif S[i] == 'B':
            iB.append(i)

    bisect_cache = []
    cache_rg = {r: bisect_left(iG, r) for r in iR}
    cache_rb = {r: bisect_left(iB, r) for r in iR}
    cache_gr = {g: bisect_left(iR, g) for g in iG}
    cache_gb = {g: bisect_left(iB, g) for g in iG}
    cache_br = {b: bisect_left(iR, b) for b in iB}
    cache_bg = {b: bisect_left(iG, b) for b in iB}
    # print(cache_rg)

    ans1 = counter(S, iR, iG, iB, cache_rg, cache_gb)
    ans2 = counter(S, iR, iB, iG, cache_rb, cache_bg)
    ans3 = counter(S, iG, iR, iB, cache_gr, cache_rb)
    ans4 = counter(S, iG, iB, iR, cache_gb, cache_br)
    ans5 = counter(S, iB, iR, iG, cache_br, cache_rg)
    ans6 = counter(S, iB, iG, iR, cache_bg, cache_gr)
    ans = ans1 + ans2 + ans3 + ans4 + ans5 + ans6
    print(ans)


if __name__ == '__main__':
    main()

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

print(cnt)

Submission status: Time Limit Exceeded

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

count = 0
rs = []
gs = []
bs = []

for i in range(n):
  if s[i] == "R":
    rs.append(i)
  elif s[i] == "G":
    gs.append(i)
  else:
    bs.append(i)

for r in rs:
  for g in gs:
    for b in bs:
      if max(r, g, b) + min(r, g, b) != (r + g + b) * 2 / 3:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_list = []
g_list = []
b_list = []
cnt = 0
for i, j in enumerate(s):
    if j == 'R':
        r_list.append(i)
    if j == 'G':
        g_list.append(i)
    if j == 'B':
        b_list.append(i)
for r in r_list:
    for g in g_list:
        for b in b_list:
            if not(r * 2 == g + b or g * 2 == r + b or b * 2 == r + g):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Bcount = S.count("B")
for i in range(0,N-1):
    for j in range(i+1,N):
        if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R") :
            count += Bcount
            if j+(j-i) <N  and S[j+(j-i)] == "B":
                count  -=1
            if i-(j-i) >=0 and S[i-(j-i)] =="B":
                count -=1
            if (i+j)%2 ==0 and S[int((i+j)/2)] == "B":
                count -=1
print(count)

Submission status: Time Limit Exceeded

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

rlist, glist, blist = [], [], []
for i in range(N):
    if S[i] == "R":
        rlist.append(i)
    elif S[i] == "G":
        glist.append(i)
    else:
        blist.append(i)

rnumber = len(rlist)
gnumber = len(glist)
bnumber = N - rnumber - gnumber
total = rnumber * gnumber * bnumber

overcount = 0
for r in rlist:
    for g in glist:
        for b in blist:
            options = sorted([r,g,b])
            if options[0]+options[2] == 2*options[1]:
                overcount += 1
    
print(total - overcount)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
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:
                pass
            else:
                
                if S[i]=="R" and S[j]=="G" and S[k]=="B":
                    count=count+1
                elif S[i]=="R" and S[j]=="B"and S[k]=="G":
                    count=count+1
                elif S[i]=="B" and S[j]=="G" and S[k]=="R":
                    count=count+1
                elif S[i]=="B" and S[j]=="R" and S[k]=="G":
                    count=count+1
                elif S[i]=="G" and S[j]=="B" and S[k]=="R":
                    count=count+1
                elif S[i]=="G" and S[j]=="R" and S[k]=="B":
                    count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = tuple(input())
ans = 0
anset = set()
for i in range(n):
    a = S[i]
    for j in range(i):
        b = S[j]
        if a == b:
            continue
        else:
            for d in ['R','G','B']:
                if d ==a or d == b:
                    continue
                else:
                    c = d
            ans += S.count(c)
            if (2*j-i)<0 :
                pass
            elif S[2*j-i] == c :
                ans -=1
            if (2 * i - j) >= n:
                pass
            elif S[2*i-j] ==c:
                ans -=1
            if (i+j)%2 == 0:
                if S[(i+j)//2] == c:
                    ans -=1
ans /=3
print(int(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
N = int(input())
#A = list(map(int,input().split()))
S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
R = []
G = []
B = []
for i in range(N):
    a = S[i]
    if a == "R":
        R.append(i)
    elif a == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R) * len(G) * len(B)
def binary_search(S,key):  #昇順リストSからkeyを探す。
    n = len(S)             #存在したらindex,なかったら-1を返す
    left  = 0
    right = n
    while left < right:
        mid = (left+right) // 2
        if S[mid] == key:
            return mid
        elif S[mid] > key:
            right = mid
        else:
            left = mid + 1
    return -1
for r in R:
    for g in G:
        Min = min(r,g)
        Max = max(r,g)
        if 2 * Min - Max > 0:
            c1 = 2 * Min - Max
        else:
            c1 = -1
        if (Min + Max ) % 2 == 0:
            c2 = (Min + Max) / 2
        else:
            c2 = -1
        if 2 * Max - Min < N:
            c3 = 2 * Max - Min
        else:
            c3 = -1
        if c1 != -1:
            if binary_search(B,c1) != -1:
                s -= 1
        if c2 != -1:
            if binary_search(B,c2) != -1:
                s -= 1
        if c3 != -1:
            if binary_search(B,c3) != -1:
                s -= 1
print(s)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

s = input()

cnt = 0
for i1 in range(0,N):
    for i2 in range(i1+1,N):
        for i3 in range(i2+1,N):
            # print(s[i1], s[i2], s[i3])
            if s[i1] != s[i2] and s[i2] != s[i3] and s[i3] != s[i1]:
                if i2 - i1 != i3 - i2:
                    cnt += 1
print(cnt)

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))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
for p in range(len(s)):
    for q in range(p):
        if s[p]==s[q]:
            continue
        for r in range(q):
            if s[q]==s[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 product as pro

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

r = [i for i in range(n) if s[i] == 'R']
g = [i for i in range(n) if s[i] == 'G']
b = [i for i in range(n) if s[i] == 'B']
cnt = len(r)*len(g)*len(b)

for i, j in pro(r, g):
    if 2*j-i in b:
        cnt -= 1
    if 2*i-j in b:
        cnt -= 1
    if (i+j)/2 in b:
        cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
x = 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], s[k]}=={"R", "G", "B"} and i+k != 2*j:
        x += 1
print(x)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0,n-3):
  ii = s[i]
  for j in range(i+1, n-1):
    jj = s[j]
    if ii==jj:
      continue
    for k in range(j+1,n):
      kk=s[k]
      if j-i == k-j:
        continue
      if jj != kk and kk!=ii:
          count = count + 1
print(count)

Submission status: Time Limit Exceeded

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

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

    remain.append([r,g,b])

total = 0
for i in range(n-2):
    if s[i] == "R":
        for j in range(i+1, n):
            if s[j] == "G":
                total += remain[-j-1][2]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="B":
                        total -= 1
            elif s[j] == "B":
                total += remain[-j-1][1]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="G":
                        total -= 1
    elif s[i] == "G":
        for j in range(i+1, n):
            if s[j] == "B":
                total += remain[-j-1][0]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="R":
                        total -= 1
            elif s[j] == "R":
                total += remain[-j-1][2]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="B":
                        total -= 1
    elif s[i] == "B":
        for j in range(i+1, n):

            if s[j] == "R":
                total += remain[-j-1][1]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="G":
                        total -= 1
            elif s[j] == "G":
                total += remain[-j-1][0]
                if i+((j-i)*2) < n:
                    if s[i+((j-i)*2)]=="R":
                        total -= 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = [i for i in input()]

r = [i for i in range(n) if s[i] == 'R']
b = [i for i in range(n) if s[i] == 'B']
g = [i for i in range(n) if s[i] == 'G']

a = 0


for x in r:
    for y in b:
        for z in g:
            i, j, k = sorted([x, y, z])
            if j - i != k - j:
                a += 1

print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import combinations

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

	sum = 0

	l = list(range(N))

	comb = tuple(combinations(l,3))

	for c in comb:
		if (S[c[0]] != S[c[1]]) and (S[c[0]] != S[c[2]]) and (S[c[1]] != S[c[2]]):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i,N):
            for k in range(j,N):
                x,y,z=S[i],S[j],S[k]
                if not (x==y or x==z or y==z or 2*j-i==k):
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
#１文字目を１とカウントする
S = ' ' + S
ans = 0
for i in range(1, N-1):
    for j in range(i+1, N):
        for k in range(j+1, N+1):
            if (j-i) != (k-j):
                if (S[i] != S[j]) and (S[i] != S[k]) and (S[j] != S[k]):
                    ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

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

def get_rest(si, sj):
    for c in ['R', 'G', 'B']:
        if not c in [si, sj]: return c

cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[j] == s[i]: continue
        rest = get_rest(s[i], s[j])
        cnt += s[j+1:].count(rest)
        if j+(j-i) >= n: continue
        if s[j+(j-i)] == rest: cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
# Code for D - RGB Triplets
# Use input() to fetch data from STDIN
from collections import defaultdict
d = defaultdict(lambda: [])

n = input()
S = input()

for i, s in enumerate(S):
    d[s] = d[s] + [i]

total = 0
for i in d['R']:
    for j in d['G']:
        for k in d['B']:
            s = [i, j, k]
            s.sort()
            if not ( s[1] - s[0] == s[2] - s[1] ):
                total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
import math

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

def Word(i, a, b):
    if S[i] != S[i + a] and S[i] != S[i + a + b] and S[i + a] != S[i + a + b]:
        return True
    else:
        return False

ans = 0
for i in range(0, N):
    for a in range(1, N - 1): #a=j-i
        for b in range(1, a): #b=k-j
            if a + b + i >= N:
                break
            if Word(i, a, b):
                ans += 1
            if Word(i, b, a):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter

m = Counter(S)

ans = m['R']*m['G']*m['B']
non = 0


for i in range ((N-1)//2):
    st = 0
    while st + (i+1)*2 < N:
        if S[st] != S[st+i+1] and S[st+i+1] != S[st+2*(i+1)] and S[st] != S[st+2*(i+1)]:
            non += 1
        st += 1
        
print(ans-non)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = list(input())

res = 0

s_dif_g = [0]*n
s_dif_r = [0]*n
s_dif_b = [0]*n

for i in range(n):
    s_dif_r[i] = s[i::].count('R')
    s_dif_g[i] = s[i::].count('G')
    s_dif_b[i] = s[i::].count('B')

sample = ["R","G","B"]
for i in range(n):
    fst = s[i]
    for j in range(i+1,n-1):
        if(s[j] != fst):
            scd = s[j]
            for x in sample:
                if(x != fst and x != scd):
                    if(x == "R"):
                        res += s_dif_r[j+1]
                    if(x == "G"):
                        res += s_dif_g[j+1]
                    if(x == "B"):
                        res += s_dif_b[j+1]
                    if(2*j-i < n):    
                        if(s[2*j-i] == x):
                            res -= 1
            
            

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
S = input().strip()
res = 0
rgb = set(['R','G','B'])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

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

    ans = 0
    ans += search(r, g, b)
    ans += search(r, b, g)
    ans += search(b, r, g)
    ans += search(b, g, r)
    ans += search(g, r, b)
    ans += search(g, b, r)

    print(ans)


def search(a, b, c):
    ans = 0
    for i in range(len(a)):
        if a[i] == -1:
            continue
        for j in range(len(b)):
            if b[j] == -1:
                continue
            ji = b[j]-a[i]
            if ji < 1:
                continue
            for k in range(len(c)):
                if c[k] == -1:
                    continue
                kj = c[k]-b[j]
                if kj < 1:
                    continue
                if ji == kj:
                    continue
                ans += 1
    return ans


main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for ir in range(0, len(rs)):
        for ig in range(0, len(gs)):
            for ib in range(0, len(bs)):
                i = rs[ir]
                j = gs[ig]
                k = bs[ib]
                if i < j and j < k and (j - i) != k - j:
                    res += 1
    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


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

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(rs, bs, gs) + f(gs, rs, bs) + f(gs, bs, rs) + f(bs, rs, gs) + f(bs, gs, rs))


if __name__ == "__main__":
    main()