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

def judge(i,j,k):
  if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
    if j-i != k-j:
      # print(i,j,k, "True")
      return True
  # print(i,j,k, "False")
  return False

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if judge(i,j,k):
        R+=1

print(R)

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+1)
    elif S[i] == 'G':
        G.append(i+1)
    elif S[i] == 'B':
        B.append(i+1)
ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        if abs(r-g) + max([r,g]) in B:
            ans -= 1
        if min([r,g]) - abs(r-g) in B:
            ans -= 1
        if abs(r-g) % 2 == 0 and int(min([r,g]) + abs(r-g) * 0.5) in B:
            ans -= 1
print(ans)

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]):
    continue

  if 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:
import numpy as np
n=int(input())
s=list(input())
r=0
for ii in np.arange(1,n+1):
  for jj in np.arange(ii+1,n+1):
    for kk in np.arange(jj+1,n+1):
      #print(ii,jj,kk)
      if jj-ii!=kk-jj:
        if s[ii-1]!=s[jj-1] and s[jj-1]!=s[kk-1] and s[ii-1]!=s[kk-1]:
          r+=1
print(r)

Submission status: Time Limit Exceeded

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

counter = 0

for p in range(n):
    for q in range(n - p):
        a = s[p]
        b = s[p + q]
        if a == b:
            continue
        for r in range(n - p - q):
            c = s[p + q + r]
            if b != c and c != a and q != r:
                # print(a, b, c, p, q, r)
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n-2):
    if s[i] == "R":
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "B":
                        if j-i != k-j:
                            count += 1
            elif s[j] == "B":
                for k in range(j, n):
                    if s[k] == "G":
                        if j-i != k-j:
                            count += 1
    elif s[i] == "G":
        for j in range(i, n-1):
            if s[j] == "R":
                for k in range(j, n):
                    if s[k] == "B":
                        if j-i != k-j:
                            count += 1
            elif s[j] == "B":
                for k in range(j, n):
                    if s[k] == "R":
                        if j-i != k-j:
                            count += 1
    else:
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "R":
                        if j-i != k-j:
                            count += 1
            elif s[j] == "R":
                for k in range(j, n):
                    if s[k] == "G":
                        if j-i != k-j:
                            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S = input()
sum=0
for i in range(0, N - 2):
    a=S[i]
    for j in range(i, N - 1):
        if a!=S[j]:
            b=S[j]
            if a=='R' and b == 'G' or a=='G' and b=='R':
                sum += S[j + 1 :].count('B')
                if 2*j-i<N and(S[2 * j - i] == 'B'):
                    sum-=1
            elif a == 'G' and b == 'B' or a == 'B' and b == 'G':
                sum += S[j + 1 :].count('R')
                if 2*j-i<N and(S[2 * j - i] == 'R'):
                    sum-=1
            else:
                sum += S[j + 1 :].count('G')
                if 2*j-i<N and(S[2 * j - i] == 'G'):
                    sum-=1
print(sum)

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):
        if Nj <= Ni:
            pass
        else:
            y = j
            Ny =Nj 
            Sl3 =Sl[Ny:]
            Nl3 =Nl[Ny:]
            for (k,Nk) in zip(Sl3,Nl3):
                if Nk <= Nj:
                    pass
                else:
                    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:
n=int(input())
s=list(input())
rlist=[]
glist=[]
blist=[]
ans=0
if len(s)<=3:
    print(0)
    exit()
for i in range(n):
    if s[i]=="R":
        rlist.append(i)
    elif s[i]=="G":
        glist.append(i)
    else:
        blist.append(i)
if len(rlist)==0 or len(glist)==0 or len(blist)==0:
    print(0)
    exit()
ans+=len(rlist)*len(glist)*len(blist)
for i in rlist:
    for j in blist:
        if i>j :
            if 2*i-j in glist:
                ans-=1
        else:
            if 2*j-i in glist:
                ans-=1
for i in glist:
    for j in blist:
        if i>j :
            if 2*i-j in rlist:
                ans-=1
        else:
            if 2*j-i in rlist:
                ans-=1
for i in rlist:
    for j in glist:
        if i>j :
            if 2*i-j in blist:
                ans-=1
        else:
            if 2*j-i in blist:
                ans-=1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
index = [i for i in range(N)]

ans = 0

from itertools import permutations, combinations,combinations_with_replacement,product,chain, zip_longest

it = zip_longest(index, S)

C = list(combinations(it,3))

i = 0

for c in C:
    i, j, k = c[0][0]+1, c[1][0]+1, c[2][0]+1
    c1, c2, c3 = c[0][1], c[1][1], c[2][1]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
#from sys import stdin
#import math
#from functools import reduce
    
#A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = input()
 
counter = 0
 
for k in range(N):
    sk = S[k]
    for j in range(k):
        sj = S[j]
        #print(sk, sj)
        if (sk == sj):
            continue
        for i in range(j):
            if (j-i)==(k-j):
                continue
            si = S[i]
            if (si==sj):
                continue
            elif (si==sk):
                continue
            else:
                counter = counter + 1
 
print(counter)

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    I = S[i]

    for j in range(i+1, N-1):
        if(S[j] != I):
            J = S[j]
            Dif1 = j-i
            #K = 'RGB'.strip(I).strip(J)

            sub = 0
            if(j + Dif1 < N):
                if(S[j+Dif1] != I and S[j+Dif1] != J):
                    sub = 1

            tmp = S[j+1:].replace(I, '').replace(J, '')
            ans += len(tmp) - sub

print(ans)

Submission status: Time Limit Exceeded

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

ans = int(0)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ss=list(range(n))
cnt=0
from itertools import combinations
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 i<j and j<k:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

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


def run():
    n = int(input())
    s = input()[:n]

    r = 0
    for _i, _is in enumerate(s[:-2]):
        _ii = _i + 1
        for _j, _js in enumerate(s[_ii:]):
            if _is != _js:
                _ji = _ii + _j + 1
                for _k, _ks in enumerate(s[_ji:]):
                    _ki = _ji + _k + 1
                    if _is != _ks and _js != _ks and _ji - _ii != _ki - _ji:
                        r += 1
    print(r)


if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    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)
    ans = 0
    for i in range(len(rlist)):

        for j in range(len(glist)):
            r = rlist[i]
            ng = 0
            g = glist[j]
            s = sorted([r, g])
            r = s[0]
            g = s[1]
            if (r-(g-r)) in blist:
                ng += 1
            if (g+(g-r)) in blist:
                ng += 1
            if (r+g) % 2 == 0 and (r+g)//2 in blist:
                ng += 1
            ans += len(blist)-ng
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

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:
        if jj != kk and kk!=ii:
          count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = input()
s = 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 j - i == k - j:
                continue
            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 = [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:
N = int(input())
S = input()

# 1を満たす数
# 1,~2を満たす数を求める

def checkone(a,b,c):
	if (a!=c and b!=c and c!=a):
		return True
	else:
		return False

def check(i,j,N): # i,j,2j-iは違ったらTrue
	# i < jは保証
				# (0 > (2*i-j) or (2*i-j) >= N or checkone(S[i], S[j], S[2*i-j])) and
              	# (0 > (2*j-i) or (2*j-i) >= N or checkone(S[i], S[j], S[2*j-i])) #and
			  	# ( ((i+j)%2!=0) or checkone(S[i], S[j], S[abs(int((i+j)/2))]))
	if (0 > (2*j-i) or (2*j-i) >= N):
		# print("out of range")
		return False
	else:
		return checkone(S[i], S[j], S[2*j-i])

n_equal = 0
for i in range(N):
	for j in range(i,N):
		if( S[i]==S[j]):
			continue
		else:
			if(check(i,j,N)==True):
				n_equal+=1
				# print(i, j, 2*j-i)
				# print(S[i],S[j],S[2*j-i])
				# print("toukankaku")

# print("n_equal=",n_equal)

n_R = S.count('R')
n_G = S.count('G')
n_B = S.count('B')

n_all = n_R*n_G*n_B
# print("n_all=",n_all)

print(n_all-n_equal)

Submission status: Time Limit Exceeded

Solution code:

N = input()
S = input()
sli = [c for c in S]

nR = sli.count('R')
nG = sli.count('G')
nB = sli.count('B')

dx = 1
ng = 0
for i, c in enumerate(sli):
    rl = []
    dx = 1
    while 1:
        if dx * 2 + i > len(sli) - 1:
            break
        rl.append([i, i + dx, i + dx*2])
        dx += 1

    for ind in rl:
        if len(set([sli[ind[0]], sli[ind[1]], sli[ind[2]]])) == 3:
            ng += 1


n = nR * nG * nB - ng
print(str(n))

Submission status: Time Limit Exceeded

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

def solve(s, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(s)
        if len(s)>dis:
            if s[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        for i, j in enumerate(s):
            if j==p:
                ans += solve(s[i:], [q], i)
            elif j==q:
                ans += solve(s[i:], [p], i)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(s):
            if j==p:
                ans += solve(s[i:], [q, r], i)
            elif j==q:
                ans += solve(s[i:], [p, r], i) 
            elif j==r:
                ans += solve(s[i:], [p, q], i) 
        return ans

print(solve(S, ('R', 'G', 'B'), 0))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(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)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            x=min(r[i], g[j], b[k])
            y=max(r[i], g[j], b[k])
            if r[i] != x and r[i] != y:
                if r[i]-x != y-r[i]:
                    cnt += 1
            elif g[j] != x and g[j] != y:
                if g[j]-x != y-g[j]:
                    cnt += 1
            else:
                if b[k]-x != y-b[k]:
                    cnt += 1
print(cnt)

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)
import itertools
t = 0
for i in itertools.product(R, G, B):
    a, b, c = i
    B = list([a, b, c])
    B = sorted(B)
    a, b, c = B[0], B[1], B[2]
    if b - a != c - b:
        t += 1
    
print(t)

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

Submission status: Time Limit Exceeded

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

num_rgb = {'R':0, 'G':0, 'B':0}
num_list = []
for i in range(n):
    num_rgb[s[i]] += 1
    num_list.append(num_rgb.copy())

total = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        target = ''
        if not (s[i] == 'R' or s[j] == 'R'):
            target = 'R'
        elif not (s[i] == 'G' or s[j] == 'G'):
            target = 'G'
        elif not (s[i] == 'B' or s[j] == 'B'):
            target = 'B'
        num = num_list[n-1][target]-num_list[j][target]
        if 2*j-i < n and s[j+j-i] == target:
            num -= 1
        total += num
print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    a = S[i]
    for j in range(i+1,N-1):
        b = S[j]
        if a != b:
            if a != 'R' and b != 'R':
                ans += S[j+1:].count('R')
                if 2*j-i < N and S[j+j-i] == 'R':
                    ans -= 1
            if a != 'G' and b != 'G':
                ans += S[j+1:].count('G')
                if 2*j-i < N and S[j+j-i] == 'G':
                    ans -= 1
            if a != 'B' and b != 'B':
                ans += S[j+1:].count('B')
                if 2*j-i < N and S[j+j-i] == 'B':
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = str(input())

    if N <= 3 :
        print("0")
    elif "R" in S and "G" in S and "B" in S :
        count = 0
        for i in range(1, N - 1):
            for j in range(i+1, N):
                if S[i-1] != S[j-1] :
                    for k in range(j+1 , N+1):
                        if S[i-1] != S[k-1] and S[j-1] != S[k-1] and (j-i) != (k-j) :
                            count = count + 1
                        #print(i,S[i-1])
                        #print(j,S[j-1])
                        #print(k,S[k-1])
        #print("count")
        print(count)
    else :
        print("0")

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
from bisect import bisect_left
def resolve():
    N = ir()
    nS = sr()
    q = deque([])
    S = []
    R = []
    G = []
    B = []
    for i in range(N):
        if nS[i] == 'R':
            S.append(2)
            R.append(i)
        elif nS[i] == 'G':
            S.append(3)
            G.append(i)
        else:
            S.append(5)
            B.append(i)
        q.append([S[i], i])
    ans = 0
    for j in range(N):
        u, n = S[j], j
        for i in range(n+1, N):
            if u != S[i]:
                if u*S[i] == 6:
                    x = bisect_left(B, i)
                    ans += len(B[x:])
                    if 2*i-n in B:
                        ans -= 1
                elif u*S[i] == 10:
                    x = bisect_left(G, i)
                    ans += len(G[x:])
                    if 2*i-n in G:
                        ans -= 1
                else:
                    x = bisect_left(R, i)
                    ans += len(R[x:])
                    if 2*i-n in R:
                        ans -= 1   
    print(ans)
resolve()

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):
        for k in range(j, N):
            if len({S[i], S[j], S[k]}) == 3:
                if (j - i) != (k - 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)
    elif s[i] == 'B':
        b.append(i)


ans = 0
for x in r:
    for y in g:
        for z in b:
            i = min(x,y,z)
            k = max(x,y,z)
            if (i == x or k == x) and (i == y or k == y):
                j = z
            elif (i == x or k == x) and (i == z or k == z):
                j = y
            else:
                j = x
            if j-i != k-j:
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

dp=[{"R":-5000,"G":-5000,"B":-5000}]

count = 0
for num, i in enumerate(s):
    new_dp=[]
    for j in dp:
        #print(j)
        if not(j in new_dp):
            new_dp.append(j)
        if i=="R":
            if j[i]==-5000 and not(abs(j["G"]-j["B"])==num-max(j["G"],j["B"])):
                if j["G"]>=0 and j["B"]>=0:
                    count += 1
                else:
                    new_dp.append({"R":num,"G":j["G"],"B":j["B"]})
                
        if i=="G":
            if j[i]==-5000 and not(abs(j["R"]-j["B"])==num-max(j["R"],j["B"])):
                if j["R"]>=0 and j["B"]>=0:
                    count += 1
                else:
                    new_dp.append({"R":j["R"],"G":num,"B":j["B"]})
            
        if i=="B":
            if j[i]==-5000 and not(abs(j["R"]-j["G"])==num-max(j["R"],j["G"])):
                if j["R"]>=0 and j["G"]>=0:
                    count += 1
                else:
                    new_dp.append({"R":j["R"],"G":j["G"],"B":num})
                    
    dp = new_dp
    #print(i,dp)

print(count)

Submission status: Time Limit Exceeded

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

r_list = []
g_list = []
b_list = []

res = 0

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

# print(r_list)
# print(g_list)
# print(b_list)

for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            if b_index != g_index and g_index != r_index and r_index != b_index:
                # if b_index-g_index != g_index-r_index:
                tmp = sorted([r_index, g_index, b_index])
                if tmp[2]-tmp[1] != tmp[1]-tmp[0]:
                    # print(r_index, g_index, b_index)
                    res += 1
print(res)

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)

ans = 0
for r in R:
    for g in G:
        for b in B:
            if r < g and g < b and (b - g) != (g - r):
                ans += 1
            elif r < b and b < g and (g - b) != (b - r):
                ans += 1
            elif g < b and b < r and (r - b) != (b - g):
                ans += 1
            elif g < r and r < b and (b - r) != (r - g):
                ans += 1
            elif b < r and r < g and (g - r) != (r - b):
                ans += 1
            elif b < g and g < r and (r - g) != (g - b):
                ans += 1 

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#from operator import itemgetter
#from collections import Counter
#import heapq
N = int(input())
S = input()
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
#A = [int(input()) for i in range(N)]
#C = [list(map(int, input().split())) for i in range()]
R  =[]
G  =[]
B  =[]
for i, s in enumerate(S):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)
answer = len(R)*len(G)*len(B)
hiku = 0
for r in R:
    for g in G:
        for b in B:
            if abs(r-g) == abs(g-b) or abs(r-b) == abs(b-g) or abs(b-r) == abs(r-g):
                hiku += 1
answer -= hiku
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

import bisect

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)
        
r, g, b = len(R), len(G), len(B)

ans = 0
for i in range(N):
  for j in range(i+1, N):
    if S[j] == S[i]:
      continue
      
    if S[i] == 'R' and S[j] == 'G':
      ans += b - bisect.bisect_left(B, j)
      if 2*j-i < N:
        if S[2*j-i] == 'B':
          ans -= 1
    elif S[i] == 'R' and S[j] == 'B':
      ans += g - bisect.bisect_left(G, j)
      if 2*j-i < N:
        if S[2*j-i] == 'G':
          ans -= 1
    elif S[i] == 'G' and S[j] == 'R':
      ans += b - bisect.bisect_left(B, j)
      if 2*j-i < N:
        if S[2*j-i] == 'B':
          ans -= 1
    elif S[i] == 'G' and S[j] == 'B':
      ans += r - bisect.bisect_left(R, j)
      if 2*j-i < N:
        if S[2*j-i] == 'R':
          ans -= 1
    elif S[i] == 'B' and S[j] == 'G':
      ans += r - bisect.bisect_left(R, j)
      if 2*j-i < N:
        if S[2*j-i] == 'R':
          ans -= 1
    else:
      ans += g - bisect.bisect_left(G, j)
      if 2*j-i < N:
        if S[2*j-i] == 'G':
          ans -= 1
    #print(i, j)
print(ans)

Submission status: Time Limit Exceeded

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

    c = 0

    r_indices = [i for i in range(N) if S[i] == 'R']
    g_indices = [i for i in range(N) if S[i] == 'G']
    b_indices = [i for i in range(N) if S[i] == 'B']

    # print(r_indices)
    # print(g_indices)
    # print(b_indices)

    for r in r_indices:
        for g in g_indices:
            for b in b_indices:
                # print(r + g + b)
                # print(r + g + b)
                if r + g + b != 3 * (max([r, g, b]) + min([r, g, b])) / 2:
                    c += 1

    print(c)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

out = 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 j - i == k - j:
                continue
            elif s[i] == s[k]:
                continue
            elif s[j] == s[k]:
                continue
            else:
                out += 1
print(out)

Submission status: Time Limit Exceeded

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

result = 0

if n <= 2:
    print(0)
    
for i in range(n-2):
    for j in range(i+1, n-1):
        rgb = ["R", "G", "B"]
        rgb.remove(s[i])
        if s[j] in rgb:
            rgb.remove(s[j])
            for k in range(j+1, n):
                if s[k] in rgb:
                    if j-i != k-j:
                        result += 1
print(result)

Submission status: Time Limit Exceeded

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

t = 0
for i in range(len(s)-2):
    for j in range(1,len(s)-1):
        if j > i and s[i] != s[j]:
            for k in range(2,len(s)):
                if k > j:
                    if j-i != k-j:
                        if s[i] != s[k] and s[j] != s[k]:
                            t += 1
print(t)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

import sys

input = sys.stdin.readline
N = int(input())
S = input()
if N < 3:
    print(0)
    sys.exit()
ans = 0
for i in range(N):
    for s in range(1, N-i):
        if i+s > N-1:
            break
        for t in range(1, N-i-s):
            if i+s+t > N-1:
                break
            if s == t:
                continue
            ok = (S[i] != S[i+s]) & (S[i+s] != S[i+s+t]) & (S[i+s+t] != S[i])
            if ok:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

R_index = my_index_multi(s,'R')
G_index = my_index_multi(s,'G')
B_index = my_index_multi(s,'B')

#for i in range(len(R_index)):
#    R_index[i] += 1
#for j in range(len(G_index)):
#    G_index[j] += 1
#for k in range(len(B_index)):
#    B_index[k] += 1

count = 0
for i in range(len(R_index)):
    for j in range(len(G_index)):
        for k in range(len(B_index)):
            li = [R_index[i],G_index[j],B_index[k]]
            li.sort()
            if (li[1]*2 != li[0]+li[2]):
                count = count +1

#print(R_index)
#print(G_index)
#print(B_index)
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [*input()]
ans = 0
for left in range(N-2):
    for mid in range(left+1,N-1):
        for right in range(mid+1,N):
            if S[left]==S[mid] or S[left]==S[right] or S[mid]==S[right]:
                continue
            if mid - left == right - mid:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    ans = 0
    r, g, b = [], [], []
    for i in range(n):
        if s[i] == 'R':
            r.append(i)
        elif s[i] == 'G':
            g.append(i)
        else:
            b.append(i)
    for ri in r:
        for gi in g:
            for bi in b:
                rgb = sorted([ri, gi, bi])
                if rgb[2] - rgb[1] == rgb[1] - rgb[0]:
                    continue
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def rest(a, b):
    if (a == "R" and b == "G") or (a == "G" and b == "R"):
        return("B")
    elif (a == "R" and b == "B") or (a == "B" and b == "R"):
        return("G")
    else:
        return("R")

for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] != s[j]:
            parts = s[j + 1:]
            ans = ans + parts.count(rest(s[i], s[j]))

            if 2 * j - i < n:
                if s[2 * j - i] == rest(s[i], s[j]):
                    ans = ans - 1

print(ans)