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

print(R*G*B - notToAllow)

Submission status: Time Limit Exceeded

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

l_R = [i for i , x in enumerate(s) if x == "R"]
l_G = [i for i , x in enumerate(s) if x == "G"]
l_B = [i for i , x in enumerate(s) if x == "B"]

RGB = [[l_R, l_B, l_G], [l_R, l_G, l_B], [l_B, l_G, l_R], 
        [l_B, l_R, l_G], [l_G, l_R, l_B], [l_G, l_B, l_R]]

for A, B, C in RGB:
    for i in A:
        for j in B:
            for k in C:
                if (j - i != k - j) & (i < j < k):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    r = [0]*n
    g = [0]*n
    b = [0]*n
    ans = 0

    for i in range(n):
        s1 = s[i]
        if s1 == "R":
            r[i] = 1
        elif s1 == "G":
            g[i] = 1
        else:
            b[i] = 1
    
    dict = {"R":r, "G":g, "B":b}

    def rgb_sum(t,i,j):
        for s3 in "RGB":
            if s3 not in t:
                if 2*j-i < n:
                    return sum(dict[s3][j+1:]) - dict[s3][2*j-i]
                else:
                    return sum(dict[s3][j+1:])

    for i in range(n):
        for j in range(i,n):
            s1 = s[i]
            s2 = s[j]
            t = (s1,s2)
            if s1 != s2:
                ans += rgb_sum(t,i,j)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = list(input())

if len(S) <= 3:
    print(0)
    exit()

c = Counter(S)
# print('c', c, len(c))
if len(c) != 3:
    print(0)
    exit()

condition1 = 1
for v in c.values():
    condition1 *= v

# print('condition1', condition1)

condition2 = 0
for i in range(N):
    for d in range(N // 2 + 2):
        j = i + d
        k = j + d
        if j >= N or k >= N:
            continue
        # print('i, j, k', i, j, k)
        if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]):
            condition2 += 1
ans = condition1 - condition2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
s = list(input())
baf = [i for i, x in enumerate(s) if x == 'B']
raf = [i for i, x in enumerate(s) if x == 'R']
gaf = [i for i, x in enumerate(s) if x == 'G']
bbf = []
rbf = []
gbf = []
ans = 0
for i,item in enumerate(s):
    if item == 'B':
        baf.remove(i)
        bbf.append(i)
        for j in rbf:
            if 2*i-j in gaf:
                ans += len(gaf)-1
            else:
                ans += len(gaf)
        for j in gbf:
            if 2*i-j in raf:
                ans += len(raf)-1
            else:
                ans += len(raf)
    elif item == 'R':
        raf.remove(i)
        rbf.append(i)
        for j in gbf:
            if 2*i-j in baf:
                ans += len(baf)-1
            else:
                ans += len(baf)
        for j in bbf:
            if 2*i-j in gaf:
                ans += len(gaf)-1
            else:
                ans += len(gaf)
    else:
        gaf.remove(i)
        gbf.append(i)
        for j in rbf:
            if 2*i-j in baf:
                ans += len(baf)-1
            else:
                ans += len(baf)
        for j in bbf:
            if 2*i-j in raf:
                ans += len(raf)-1
            else:
                ans += len(raf)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
res=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  elif s[i]=="B":
    b.append(i)

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

for i in r:
  for j in g:
    if (i+j)%2==0 and int((i+j)/2) in b:
      res-=1
    if abs(i-j)+max(i,j) in b:
      res-=1
    if min(i,j)-abs(i-j) in b:
      res-=1
print(res)

Submission status: Time Limit Exceeded

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

positions = {}
positions['R'] = [i for i in range(n) if s[i]=='R']
positions['G'] = [i for i in range(n) if s[i]=='G']
positions['B'] = [i for i in range(n) if s[i]=='B']
positions['RG'] = sorted(positions['R']+positions['G'])
positions['RB'] = sorted(positions['R']+positions['B'])
positions['GB'] = sorted(positions['G']+positions['B'])

for p1 in range(n-3):
    if s[p1] == 'R':
        pos2 = positions['GB']
    elif s[p1] == 'G':
        pos2 = positions['RB']
    else:
        pos2 = positions['RG']

    for p2 in pos2:
        if p2 > p1:
            for p3 in pos2:
                if p3 > p2:                
                    #print(s[p1], s[p2], s[p3])
                    if (s[p1] != s[p2]) and (s[p1] != s[p3]) and (s[p2] != s[p3]) and (p1-p2 != p2-p3):
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
def findb(ind,checker):
    b = S[ind:].count("B")
    if b > 0:
        if ind + checker < N:
            if S[ind+checker] == "B":
                return b - 1
            else:
                return b
        else:
            return b
    return 0
def findg(ind,checker):
    g = S[ind:].count("G")
    if g > 0:
        if ind + checker < N:
            if S[ind+checker] == "G":
                return g - 1
            else:
                return g
        else:
            return g
    return 0
def findr(ind,checker):
    r = S[ind:].count("R")
    if r > 0:
        if ind + checker < N:
            if S[ind+checker] == "R":
                return r - 1
            else:
                return r
        else:
            return r
    return 0
for i in range(N):
    if S[i] == "R":
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            count += findb(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            count += findg(i+j,j)
    elif S[i] == "G":
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            count += findr(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            count += findb(i+j,j)
    else:
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            count += findg(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            count += findr(i+j,j)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import time
# start = time.time()
import sys
# sys.stdin = open('input.txt')

from math import gcd


n = int(input())
s = input()
i_indices = []
j_indices = []
k_indices = []

for i in range(n):
    if s[i] == 'R':
        i_indices.append(i+1)
    elif s[i] == 'G':
        j_indices.append(i+1)
    else:
        k_indices.append(i+1)

# print(i_indices, len(i_indices))
# print(j_indices, len(j_indices))
# print(k_indices, len(k_indices))

ans = 0

for i in i_indices:
    for j in j_indices:
        for k in k_indices:
            tmp  = [i, j, k]
            tmp.sort()
            if tmp[1] - tmp[0] != tmp[2] - tmp[1]:
                ans += 1
print(ans)
# end = time.time()
# print(end - start)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

import numpy as np

snp=np.array(S)
R=np.where(snp=='R')[0]
G=np.where(snp=='G')[0]
B=np.where(snp=='B')[0]
ans=0
for r in R:
    for g in G:
        d=abs(r-g)
        front=min(r,g)
        back=max(r,g)
        ans+=sum((B!=front-d)*(B!=back+d)*(B!=(front+back)/2))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy
import sys
import statistics

num=int(input())
txt=input()
count=0
ri=[]
gi=[]
bi=[]
for i in range(len(txt)):
  if txt[i]=='R':
    ri.append(i)
  elif txt[i]=='G':
    gi.append(i)
  else:
    bi.append(i)
for i in ri:
  for j in gi:
    for k in bi:
      mean=statistics.mean([float(i),float(j),float(k)])
      med=statistics.median([i,j,k])
      diff=abs(mean-med)
      if diff<=1e-5:
        continue
      count+=1
print(count)

Submission status: Time Limit Exceeded

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

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)
        elif x =="B":b.append(i)
 
    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
 
 
    def rgb(r, g, b):
        sb = set(b)
        ans = 0
        for r_ in r:
            # rgb
            for g_ in g[g>r_]:
                if (g_ + g_ - r_) in sb:
                    ans -= 1
                ans += len(b[b>g_])
            # grb
            for g_ in g[g<r_]:
                if (r_ + (r_ - g_)) in sb:
                    ans -= 1
                ans += len(b[b>r_])
        return ans
 
    k = rgb(r,g,b) + rgb(r,b,g) + rgb(b,g,r)
    print(k)
 
 
    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:
n = int(input())
s = input()

count = 0

for i in range(n-3):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        else:
            for k in range(j+1, n):
                if j - i == k - j:
                    continue
                else:
                    if s[i] == s[k] or s[j] == s[k]:
                        continue
                    else:
                        count += 1

print(count)

Submission status: Time Limit Exceeded

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

count=0

for index1, val1 in enumerate(S):
  for index2, val2 in enumerate(S[index1+1:]):
    if val1 != val2:
      for index3, val3 in enumerate(S[index1+index2+2:min(index1+1+(index2+1)*2-1,N)]):
        if val3 not in [val1, val2]:
          count+=1
      for index3, val3 in enumerate(S[min(index1+1+(index2+1)*2,N):]):
        if val3 not in [val1, val2]:
          count+=1
          
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
str = input()
sum = 0
ans = []
for i in range(n):
  if str[i] == "R":
    ans.append(2)
  elif str[i] == "G":
    ans.append(3)
  else:
    ans.append(5)

for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      if ans[i] == ans[j]:
        break
      elif ans[i] * ans[j] * ans[k] == 30 and j - i != k - j:
        sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

total = 0
for i in range(N) :
    for j in range(i,N) :
        if (i >= j) :
            continue

        if (S[i] == S[j]) :
            continue

        for k in range(j,N) :
            if (j >= k) :
                continue

            if (S[i] == S[k] or S[j] == S[k]) :
                continue

            if (j-i == k-j) :
                continue
            total += 1

print(total)

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):
      for k in range(j+1,N):
        if(S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
          if(j-i != k-j):
            ct += 1
  print(ct)
  return 0
 
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

rs = {}
gs = {}
bs = {}

for i, e in enumerate(s):
  if e == 'R':
    rs[i] = ''
  if e == 'G':
    gs[i] = ''
  if e == 'B':
    bs[i] = ''
   
      
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  for i in rs.keys():
    for j in gs.keys():
      mi = i if i < j else j
      ma = j if i < j else i
      p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
      for e in p:
        if e%1 != 0 or e < 0 or e > len(s):
          continue
        if e in bs:
          saidai -= 1
  print(int(saidai))

Submission status: Time Limit Exceeded

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

def main():
    cnt = 0
    cnt_1 = 0
    for i in range(N) :
        for j in range(i+1,N) :
            for k in range(j+1,N) :
                cnt_1 += 1
                if ((S[i] == S[j]) or (S[i] == S[k]) or (S[j] == S[k])) or (k == 2*j-i) :
                    cnt += 1
    print(cnt_1 - cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    ans = 0
    for r_i in r_arr:
        for g_i in g_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2
            central = (max_i + min_i) / 2

            tgt_b_arr = b_arr[::]

            if minmin in tgt_b_arr:
                tgt_b_arr.remove(minmin)
            if maxmax in tgt_b_arr:
                tgt_b_arr.remove(maxmax)
            if exists_central == 0:
                if central in tgt_b_arr:
                    tgt_b_arr.remove(central)
            ans += len(tgt_b_arr)

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
#
from collections import deque


def main():
    n = int(input())
    s = [s for s in input()]
    count = 0
    # これだとTLE
    ps = ""
    pc = 0
    for j in range(1, n):
        if ps == s[j]:
            count += pc
            continue
        pc = 0
        for i in range(j - 1, -1, -1):
            if s[i] != s[j]:
                offset = j - i
                for k in range(j + 1, n):
                    if s[k] != s[j] and s[k] != s[i]:
                        if k - j != offset:
                            pc += 1
        count += pc
    print(count)
    # これもTLE
    # s = deque()
    # r = deque()
    # g = deque()
    # b = deque()
    # for i, c in enumerate(input()):
    #     s.append(c)
    #     if c == "R":
    #         r.append(i)
    #     elif c == "G":
    #         g.append(i)
    #     else:
    #         b.append(i)
    # count = 0
    # t = []
    # for j in range(1, n):
    #     if s[j] == "R":
    #         t = [g, b]
    #     elif s[j] == "G":
    #         t = [r, b]
    #     else:
    #         t = [r, g]
    #     for z in range(2):
    #         for i in t[z]:
    #             if i >= j:
    #                 break
    #             offset = j - i
    #             for k in t[1 - z]:
    #                 if k <= j:
    #                     continue
    #                 if offset != k - j:
    #                     count += 1
    # print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    r = 0
    for k in range(1, n):
        sk = s[k]
        for j in range(1, k):
            sj = s[j]
            kj = k - j
            if sj != sk:
                for i in range(j):
                    si = s[i]
                    if j - i != kj and si != sk and si != sj:
                        r += 1
    print(r)


main()

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input(''))
s=input('')
u=0
 
l=[]
for a in s:
    l.append(a)
 
for i in range(0,len(l)):
    for j in range(i+1,len(l)):
        if l[i]!=l[j]:
            for k in range(j+1,len(l)):
                if j-i!=k-j:
                    if l[j]!=l[k] and l[k]!=l[i]:
                        u=u+1

print(u)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

diff_dic = {}
diff_dic[('R', 'G')] = []
diff_dic[('R', 'B')] = []
diff_dic[('G', 'R')] = []
diff_dic[('G', 'B')] = []
diff_dic[('B', 'G')] = []
diff_dic[('B', 'R')] = []

for i in range(N):
    for j in range(i + 1, N):

        if S[i] == 'R':
            if S[j] != 'R':
                diff_dic[('R', S[j])].append((i, j - i))

        if S[i] == 'G':
            if S[j] != 'G':
                diff_dic[('G', S[j])].append((i, j - i))

        if S[i] == 'B':
            if S[j] != 'B':
                diff_dic[('B', S[j])].append((i, j - i))

cnt = 0
for i in range(N):
    if S[i] == 'R':
        for j, d in diff_dic[('G', 'B')]:
            if j > i and (j - i) != d:
                cnt += 1

        for j, d in diff_dic[('B', 'G')]:
            if j > i and (j - i) != d:
                cnt += 1

    if S[i] == 'G':
        for j, d in diff_dic[('R', 'B')]:
            if j > i and (j - i) != d:
                cnt += 1

        for j, d in diff_dic[('B', 'R')]:
            if j > i and (j - i) != d:
                cnt += 1

    if S[i] == 'B':
        for j, d in diff_dic[('R', 'G')]:
            if j > i and (j - i) != d:
                cnt += 1

        for j, d in diff_dic[('G', 'R')]:
            if j > i and (j - i) != d:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

R = [i for i, x in enumerate(s) if x == "R"]
G = [i for i, x in enumerate(s) if x == "G"]
B = [i for i, x in enumerate(s) if x == "B"]
a1 = [R,G,B]
l1=list(itertools.product(*a1))
a2 = [R,B,G]
l2=list(itertools.product(*a2))
a3 = [G,R,B]
l3=list(itertools.product(*a3))
a4 = [G,B,R]
l4=list(itertools.product(*a4))
a5 = [B,R,G]
l5=list(itertools.product(*a5))
a6 = [B,G,R]
l6=list(itertools.product(*a6))

l=l1+l2+l3+l4+l5+l6
ans = list(filter(lambda i : i[1]-i[0]!=i[2]-i[1] and i[0]<i[1]<i[2] ,l))
print (len(ans))

Submission status: Time Limit Exceeded

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

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

ans = 0

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


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

print(ans)

Submission status: Time Limit Exceeded

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

R_count = [0]*n
G_count = [0]*n
B_count = [0]*n

Rc = 0
Gc = 0
Bc = 0

for i in range(1,n+1):
    if s[-i] == "R":
        Rc += 1
    elif s[-i] == "G":
        Gc += 1
    else:
        Bc += 1
        
    R_count[-i] = Rc
    G_count[-i] = Gc
    B_count[-i] = Bc

ans = 0

for i in range(n):
    if s[i] == "R":
        for j in range(i+1,n-1):
            if  s[j] == "G":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "B":
                    ans -= 1
            if s[j] == "B":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "G":
                    ans -= 1               

    if s[i] == "B":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "G":
                    ans -= 1
            if s[j] == "G":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "R":
                    ans -= 1

    if s[i] == "G":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "B":
                    ans -= 1
            if s[j] == "B":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if s[j+j-i] == "R":
                    ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
S = list(input())
ans = 0
for i,j,k in list(itertools.combinations(range(1,N+1),3)):
  # print(i,j,k)
  if len(set([S[i-1],S[j-1],S[k-1]]))==3:
    if k-j != j-i:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)

for i in range(1, len(s)):
    if s[i] == "R":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)

for i in range(1, len(s)):
    if s[i] == "G":
        g.append(g[-1] + 1)
    else:
        g.append(g[-1])

b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

for i in range(1, len(s)):
    if s[i] == "B":
        b.append(b[-1] + 1)
    else:
        b.append(b[-1])

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

            if "R" != s[i] and "R" != s[j]:
                ans += r[-1] - r[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "R":
                    ans -= 1
            elif "G" != s[i] and "G" != s[j]:
                ans += g[-1] - g[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += b[-1] - b[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "B":
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
S = input()
ans = 0

for i in range(N-3):
    A = ["R", "G", "B"]
    A.remove(S[i])
    for k in range(i+3, N):
        B = copy.copy(A)
        if S[i] != S[k]:
            B.remove(S[k])
            ans += S[i+1:k].count(B[0])
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == B[0]:
                    ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
count = 0
for i in range(n):
    for j in range(n):
        for k 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:
n=int(input())
S=list(input())
ans=0
for i in range(n-2):
  for j in range(i,n-1):
    if S[i]==S[j]:
      continue
    for k in range(j,n):
      if S[j]==S[k] or S[i]==S[k]:
        continue
      if j-i != k-j:
        ans+=1
    continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = list(input())

r_ind = []
g_ind = []
b_ind = []
base_set = set(["R", "G", "B"])
for i, v in enumerate(s):
  if v == "R":
    r_ind.append(i)
  elif v == "G":
    g_ind.append(i)
  else:
    b_ind.append(i)
char_dict = {
  "R": np.array(r_ind),
  "G": np.array(g_ind),
  "B": np.array(b_ind)
}
res = 0
for i, v in enumerate(s):
  rest_s = s[i+1:]
  len_s = len(s)
  if i + 1 > (len_s-2):
    break
  for j, v2 in enumerate(rest_s):
    cur_index = j + i + 1
    diff = j + 1
    ban_index = i + diff * 2
    if cur_index + 1 > (len_s-1):
      break
    if v == v2:
      continue
    c_set = set([v, v2])
    rest_char = list(base_set - c_set)[0]
    use_ind = char_dict[rest_char]
    flg_2 = ban_index in use_ind
    flg = (use_ind > cur_index)
    res += (flg.sum() - int(flg_2))
print(res)

Submission status: Time Limit Exceeded

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

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

count = 0
for s in S:
    if s == "R":
        R.append(count)
    elif s == "G":
        G.append(count)
    elif s == "B":
        B.append(count)
    count += 1

def checker(i,E,F):
    wa =0
    for e in E:
        for f in F:
            if i>e or i > f:
                pass
            else:
                if f > e and e-i != f-e:
                    wa += 1
                elif e > f and f-i != e-f:
                    wa += 1
    return(wa)

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

print(goukei)

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    all = {'R','G','B'}

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

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

    res = len(R) * len(G) * len(B)

    count = 0

    for r in R:
        for g in G:
            for b in B:
                l = [r,g,b]
                l.sort()
                if l[1]-l[0] == l[2]-l[1]:
                    count += 1

    res -= count
    print(res)




main()

Submission status: Time Limit Exceeded

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

print(ans)