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:
from functools import lru_cache
from sys import stdin
import sys
sys.setrecursionlimit(10000)

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

sum = 0

mapR = []
mapG = []
mapB = []

for i in range(len(s)):
    if s[i] == "R":
        mapR.append(i)
    elif s[i] == "G":
        mapG.append(i)
    else:
        mapB.append(i)

#print(mapR, mapG, mapB)

for i in range(len(mapR)):
    for j in range(len(mapG)):
        for k in range(len(mapB)):
            (ii, jj, kk) = sorted((mapR[i], mapG[j], mapB[k]))
            if jj-ii != kk-jj:
                sum += 1

print(sum)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1,n-1):
    sr = 0
    sg = 0
    sb = 0
    er = 0
    eg = 0
    eb = 0

    for j in range(i):
        if s[j] != s[i]:
            if s[j] == "R":
                sr += 1
            elif s[j] == "G":
                sg += 1
            else:
                sb += 1
        pass
    for j in range(i+1,n):
        if s[j] != s[i]:
            if s[j] == "R":
                er += 1
            elif s[j] == "G":
                eg += 1
            else:
                eb += 1
    ans += sr*eb + sr*eg + sg*eb + sg*er + sb*er + sb*eg
    for j in range(min(i,n-i-1)):
        if s[i+j+1] != s[i-j-1] and s[i+j+1] != s[i] and s[i] != s[i-j-1]:
            ans -= 1
    # print(i,ans)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(len(s[:-2])):
    a = s[i]

    for j in range(i+1,len(s[:-1])):
        if s[j] == a:
            continue
        else:
            b = s[j]
        for k in range(j+1,len(s)):
            if s[k] == a or s[k] == b:
                continue
            elif j - i != k - j:
                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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

if min([lr, lg, lb]) == lb:
    r, b = b, r

for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if a in b:
            ans -= 1
        if c in b:
            ans -= 1
        if (i + j) / 2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
whole = (n*(n-1)*(n-2))//6
num = n-2
cnt = 0
for i in range(n-3):
    for j in range(i+1,n-1):
        if j == i+1:
            if s[i] == s[j]:
                cnt += (n-(j+2))
                continue
            for k in range(j+2,n):
                if (j-i) == (k-j):
                    cnt += 1
                    continue
                if s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
                    cnt+= 1
        else:
            if s[i] == s[j]:
                cnt += (n-(j+1))
                continue
            for k in range(j+1,n):
                if (j-i) == (k-j):
                    cnt += 1
                    continue
                if s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
                    cnt+= 1
print(whole-num-cnt)

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)):
        for l in range(j,len(S)):
            if not S[i] == S[j] and 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 = list(input())
r = []
g = []
b = []
for i,j in enumerate(s):
    if j == "R":
        r.append(i)
    elif j == "G":
        g.append(i)
    else:
        b.append(i)
ans = 0
for i in r:
    for j in g:
        for k in b:
            a=[i,j,k]
            a.sort()
            if a[2]-a[1] != a[1]-a[0]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c_list = [(0, 0, 0) for i in range(n)]
r = 0
g = 0
b = 0
n_list = [0]*n
p = -1
for i in range(n):
  if s[i] == "R":
    r += 1
    p = 0
  elif s[i] == "G":
    g += 1
    p = 1
  else:
    b += 1
    p = 2
  c_list[i] = (r, g, b)
  n_list[i] = p
count = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c1 = n_list[i]
    c2 = n_list[j]
    if s[i] != s[j]:
      if j+j-i < n and s[j+j-i] != s[i] and s[j+j-i] != s[j]:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2] -1
      else:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2]
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)
res = 0
if max(len(r),len(g),len(b)) == len(g):
  for i in r:
    for j in b:
      flg = 0
      if (i + j) / 2 in g:
        flg += 1
      if 2 * i - j in g:
        flg += 1
      if 2 * j - i in g:
        flg += 1
      res += len(g) - 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=input()
ans=0

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

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):
        for k in range(j,N):
            chars = set()
            chars.add(S[i])
            chars.add(S[j])
            chars.add(S[k])
            if len(chars) == 3:
                if j - i != k - j:
                    total += 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
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 = len(r) * len(g) * len(b)

for rx in r:
    for gx in g:
        for bx in b:
            if abs(rx-gx) == abs(rx-bx)  or abs(gx-rx) == abs(gx-bx) or abs(bx-rx) == abs(bx -gx):
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
A = [[],[],[]]
ans = 0
for i in range(N):
    if S[i] == 'R':
        A[0].append(i)
    elif S[i] == 'G':
        A[1].append(i)
    else:
        A[2].append(i)

for i in A[0]:
    for j in A[1]:
        for k in A[2]:
            if  (i < j and j < k) or (k < j and j < i):
                if abs(i-j) != abs(j-k):
                    ans += 1
            elif (i < k and k < j) or (j < k and k < i):
                if abs(i-k) != abs(k-j):
                    ans += 1
            elif (j < i and i < k) or (k < i and i < j):
                if abs(j-i) != abs(k-i):
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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


def rule2(i, j, k):
    if j-i != k-j:
        return True
    else:
        return False



result = 0


for i in range(N):

    ai = S[i]

    for j in range(i+1, N):
        aj = S[j]

        if ai != aj:
            pass
        else:
            continue
        for k in range(j+1, N):
            ak = S[k]
            if ai != ak and aj != ak:
                if rule2(i, j, k):
                    result += 1
            else:
                continue

print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from sys import exit

N = int(input())
S = input()
if N < 3:
    print(0)
    exit()

d = {'R': 0, 'G': 1, 'B': 2}
rgb = {'R', 'G', 'B'}

table = [[0] * N for _ in range(3)]
for i, s in enumerate(S):
    table[d[s]][i] = 1

for x in range(3):
    table[x] = tuple(itertools.accumulate(table[x]))

ans = 0
for k in range(2, N):
    for j in range(1, k):
        if S[j] != S[k] :
            x = (rgb - {S[j], S[k]}).pop()
            ans += table[d[x]][j - 1]
            if 2 * j - k >= 0 and S[2 * j - k] == x:
                ans -= 1
print(ans)

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.append(i)
  if e == 'G':
    gs.append(i)
  if e == 'B':
    bs.append(i)
      
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  for i in rs:
    for j in gs:
      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 in bs:
          saidai -= 1
  print(int(saidai))

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(len(S)):
  for j in range(len(S)-i-1):
    if S[i] == S[i+j+1]:
      continue
    for k in range(len(S)-i-j-2):
      if j != k and S[i] != S[i+j+1] and S[i] != S[i+k+j+2] and S[i+k+j+2] != S[i+j+1]:
        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)
ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        for b in B:
            if abs(r-g) == abs(r-b) or abs(b-g) == abs(b-r) or abs(g-b)==abs(g-r):
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
s=input()
count=0
for a in range(num-2):
  for b in range(a+1,num-1):
    for c in range(b+1,num):
      if (s[a]!=s[b] and s[b]!=s[c] and s[a]!=s[c]) and c-b!=b-a:
        count=count+1
print(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 S[i-1] == S[j-1]:
            pass
        else:
            if (S[i-1] == "R" and S[j-1] == "G" )or (S[i-1] == "G" and S[j-1] == "R"):
                k = "B"
            elif (S[i-1] == "R" and S[j-1] == "B") or (S[i-1] == "B" and S[j-1] == "R"):
                k = "G"
            elif (S[i-1] == "G" and S[j-1] == "B") or (S[i-1] == "B" and S[j-1] == "G"):
                k = "R"
            S_cp = S[j:N]
            count += S_cp.count(k)
            if 2*j-i <= N :
                if S[2*j-i-1] == k:
                    count -= 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
L=len(s)
ans=0
N=[0]*n
r=0
g=0
b=0
for i in reversed(range(L)):
    if s[i]=='R':
        r+=1
        N[i]=r
    elif s[i]=='G':
        g+=1
        N[i]=g
    else:
        b+=1
        N[i]=b

for i in range(L-2):
    x=s[i]
    for j in range(i+1,L-1):
        if s[j]!=x:
            y=s[j]
            for k in range(j+1,L):
                if s[k]!=x and s[k]!=y:
                    ans+=N[k]
                    if k<=j+j-i and j+j-i<=L-1:
                        if s[j+j-i]==s[k]:
                            ans-=1
                    break
print(ans)

Submission status: Time Limit Exceeded

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

r = [0] * n
g = [0] * n
b = [0] * n
ri = gi = bi = 0
for i, c in enumerate(s):
  if c == "R":
    ri += 1
  elif c == "G":
    gi += 1
  if c == "B":
    bi += 1
  r[i] = ri
  g[i] = gi
  b[i] = bi

 
ans = 0

for k in range(2, n):
  for j in range(1, k):
    if s[k] == s[j]:
      continue
    i = 2 * j - k
    x = None
    c = None
    if s[k] != "R" and s[j] != "R":
      x = r
      c = "R"
    elif s[k] != "G" and s[j] != "G":
      x = g
      c = "G"
    elif s[k] != "B" and s[j] != "B":
      x = b
      c = "B"
    ans += x[j - 1]
    if i >= 0 and s[i] == c:
      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 (S[i] != S[j]) and (S[i] != S[k]) and (S[j] != S[k]):
                if (j-i) != (k-j):
                    ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # ######### Index
    # R, G, B = [], [], []
    # for i, s in enumerate(S):
    #     if s == 'R':
    #         R.append(i)
    #     elif s == 'G':
    #         G.append(i)
    #     elif s == 'B':
    #         B.append(i)
    
    C = 0
    out = 0
    # for i in R:
    # for i in B:
    # for i in range(len(S)):
        
        
    #     # for j in G:
    #     # for j in B:
    #     for j in range(i+1, len(S)):
            

    #         # for k in B:
    #         # for k in G:
    #         # for k in R:
    #         for k in range(j+1, len(S)):
                
    #             # print(i, j, k)
                
    #             if not (j - i) == (k - j):
    #                 if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
                    
                        
    #                     out += 1
    
    combinations = [[i, j, k]
                for i in range(len(S))
                for j in range(i+1, len(S))
                for k in range(j+1, len(S))
                ]
    # print(combinations)
    
    for i, j, k in combinations:
        if not (j - i) == (k - j):
            if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
            
                
                out += 1



    print(out)

    
main()

Submission status: Time Limit Exceeded

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

d = {}
for i in range(N):
  d[i] = S[i]

l = list(itertools.combinations(d, 3))
ans = 0
for x in l:
  if (x[1] - x[0] != x[2] - x[1]) and (d[x[0]] != d[x[1]] and d[x[0]] != d[x[2]] and d[x[1]] != d[x[2]]):
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def solve(rr,gg,bb):
    d1,d2=defaultdict(list),defaultdict(list)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(rr)):
            jj=rr[j]
            if ii>jj:
                d1[ii].append(ii-jj)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(bb)):
            jj=bb[j]
            if ii<jj:
                d2[ii].append(jj-ii)
    s=0
    #print(d1,d2)
    for x in d1:
        for y in d1[x]:
            for z in d2[x]:
                if y!=z:
                    s+=1
    return s
        
n=int(raw_input())
s=raw_input()
l=list(s)
rr,gg,bb=[],[],[]
for i in range(len(s)):
    ii=s[i]
    if ii=='R':rr.append(i)
    if ii=="G":gg.append(i)
    if ii=="B":bb.append(i)
#print(rr,gg,bb)
print(solve(rr,gg,bb)+solve(rr,bb,gg)+solve(bb,rr,gg)+solve(bb,gg,rr)+solve(gg,rr,bb)+solve(gg,bb,rr))

Submission status: Time Limit Exceeded

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

ans = []
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[i] != s[k] and s[j] != s[k] and j-i != k-j:
                ans.append((i, j, k))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()
S = input()

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(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
if ans == 0:
    print("0")
else:
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            sub = j - i
            sub2 = (n - 1) - j
            if sub <= sub2:
                if s[i] != s[j] and s[j] != s[j + sub] and s[i] != s[j + sub]:
                    ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

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

	ans = 0

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

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

R = []
G = []
B = []
for i in range(n):
  if s[i] == "R":
    R.append(i+1)
  elif s[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)

ans = 0
for i in range(len(R)):
  for j in range(len(G)):
    for k in range(len(B)):
      if abs(R[i]-G[j]) != abs(R[i] - B[k]) and abs(R[i]-G[j]) != abs(G[j] - B[k]) and abs(R[i] - B[k]) != abs(G[j] - B[k]):        
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k in b:
			ans -= 1
		if m in b:
			ans -= 1
		if i > j:
			k = i-abs(i-j)
		else:
			k = i+abs(i-j)
		if k in b:
			ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			if l in b:
				ans -= 1
print (ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
    n = int(input())
    s = input()
    dic = {'R':[],'G':[],'B':[]}
    for i in range(n):
        if s[i]== 'R':
            dic['R'].append(i)
        elif s[i]== 'G':
            dic['G'].append(i)
        else:
            dic['B'].append(i)
    cnt = 0
    for i in dic['R']:
        for j in dic['G']:
            cnt += len(dic['B'])
            if j+j-i in dic['B']:
                cnt -=1
            if abs(j-i)%2==0 and (j+i)//2 in dic['B']:
                cnt -=1
            if i+i-j in dic['B']:
                cnt -=1 
    print(cnt)
     
            
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import combinations

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

	sum = 0

	l = list(range(N))

	comb = list(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()
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)
s = 0
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]:
                s += 1
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i)
    elif ss == 'G':
        b_li.append(i)
    else:
        c_li.append(i)
if len(a_li) == 0 or len(b_li)== 0 or len(c_li) == 0:
    print(0)
    sys.exit()
all_count = 0
for a in a_li:
    for b in b_li:
        tmp = len(c_li)
        lili = []
        if (2*b - a)  in c_li:
            lili.append(2*b - a)
            #tmp -= 1
        if (2*a - b)  in c_li:
            lili.append(2*a - b)
            #tmp -= 1
        if (a + b)% 2 == 0 and (a + b)//2   in c_li:
            lili.append((a + b)//2)
            #tmp -= 1
        tmp -= len(set(lili))
        all_count += tmp
print(all_count)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        elif set([S[i], S[j]]) == {"R", "G"}:
            if 2*j - i < N and S[2*j - i] == "B":
                ans += S[j+1:].count("B") - 1
            else:
                ans += S[j+1:].count("B")
        elif set([S[i], S[j]]) == {"B", "G"}:
            if 2*j - i < N and S[2*j - i] == "R":
                ans += S[j+1:].count("R") - 1
            else:
                ans += S[j+1:].count("R")
        else:
            if 2*j - i < N and S[2*j - i] == "G":
                ans += S[j+1:].count("G") - 1
            else:
                ans += S[j+1:].count("G")

print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
S = input()
S_num = S.replace("R","1")
S_num = S_num.replace("G","2")
S_num = S_num.replace("B","4")
ans=0
for i in range(N):
  for j in range(N):
    for k in range(N):
      if((j-i != k-j) and (i<j and j<k ) and (int(S_num[i]) + int(S_num[j]) + int(S_num[k]))==7):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
S = i_s()

ans = 0
for i in range(N):
    for j in range(i, N):
        if S[i] != S[j]:
            if S[i] == 'R' and S[j] == 'G':
                tmp = 'B'
            elif S[i] == 'R' and S[j] == 'B':
                tmp = 'G'
            elif S[i] == 'G' and S[j] == 'R':
                tmp = 'B'
            elif S[i] == 'B' and S[j] == 'R':
                tmp = 'G'
            else:
                tmp = 'R'
            
            if tmp in S[j:]:
                for k in range(j, N):
                    if S[k] == tmp and k - j != j - i:
                        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

R_inds = []
G_inds = []
B_inds = []

for i in range(n):
    if s[i] == 'R':
        R_inds.append(i)
    elif s[i] == 'G':
        G_inds.append(i)
    else:
        B_inds.append(i)
    
ans = 0
for r in R_inds:
    for g in G_inds:
        big = max(r,g)
        small = min(r,g)
        bigger = big + (big - small)
        smaller = small - (big - small)
        if (big - small) % 2 == 0:
            middle = (big + small) // 2
        else:
            middle = -1
        
        if not (bigger in B_inds or smaller in B_inds or middle in B_inds):
            ans += len(B_inds)
        elif (bigger in B_inds and smaller in B_inds and middle in B_inds):
            ans += max(0,len(B_inds) - 3)
        elif (bigger in B_inds and smaller in B_inds and not middle in B_inds) or (smaller in B_inds and middle in B_inds and not bigger in B_inds) or \
            (middle in B_inds and bigger in B_inds and not smaller in B_inds):
            ans += max(0, len(B_inds) - 2)
        else:
            ans += max(0, len(B_inds) - 1)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s_l = {}
for i in range(N-1):
    s_l[i] = S[i+1:]

s = 0
for a in range(N-2):
    s1 = S[a]
    for b in range(a+1, N-1):
        s2 = S[b]
        if s1 == s2:
            continue
        n = s_l[b]
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = n.count(pat[0])
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

rgb = set()
ans = 0

for i in range(N):
  rgb.add(S[i])
  for j in range(i+1, N):
    if S[j] in rgb:
      continue
    rgb.add(S[j])
    d1 = j - i
    for k in range(j+1, N):
      if k - j == d1 or S[k] in rgb:
        continue
      ans += 1
    rgb.remove(S[j])
  rgb.remove(S[i])
      
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(len(s)):
    for j in range(i + 1,len(s)):
        for k in range(j + 1,len(s)):
            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:
N = input("")
S = input("")
N = int(N)
X = 0

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

Submission status: Time Limit Exceeded

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

def myindex(List,liter):
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L

def scal(a,b,c):
  if a < b < c:
    return a,b,c
  elif c < b < a:
    return c,b,a
  elif b < c < a:
    return b,c,a
  elif a < c < b:
    return a,c,b
  elif b < a < c:
    return b,a,c
  elif c < a < b:
    return c,a,b
  
R = myindex(S,"R")
G = myindex(S,"G")
B = myindex(S,"B")
total = 0

for r in R:
  for g in G:
    for b in B:
      i,j,k = scal(r,g,b)
      if 2 * j != i+k:
        total += 1
      
print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
moji = {'R', 'G', 'B'}

count = 0
for j in range(1, N-1):
    sj = S[j]
    for i in range(0, j):
        si = S[i] 
        if si == sj:
            continue
        
        diff_m = moji - {sj, si}
        m = list(diff_m)[0]
        c = S.count(m, j+1, N)

        if (j+j-i) < N:
            sk = S[j + j - i]
            if sk != sj and sk != si:
                c -= 1
        count += c

print(count)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N = int(input())
    S = input()[:-1]

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0

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

print(ans)