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()
output=0
for i in range(n-2):
  j=i+1
  while(j<n-1):
    if(s[i] != s[j]):
      h=j+1
      while(h<n):
        if (s[j] != s[h] and (h-j) != (j-i)):
          output += 1
print(output)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

K = int(input())
S = list(input())
R_idx = [i for i, x in enumerate(S) if x == 'R']
G_idx = [i for i, x in enumerate(S) if x == 'G']
B_idx = [i for i, x in enumerate(S) if x == 'B']
cnt = 0
for r in R_idx:
    for g in G_idx:
        for b in B_idx:
            if (r + g == 2 * b) or (g + b == 2 * r) or (b + r == 2 * g):
                pass
            else:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
RGB.sort(key=lambda x:len(x))

len_B=len(RGB[2])
max_B=max(RGB[2])
min_B=min(RGB[2])
for r in RGB[0]:
    for g in RGB[1]:
        dif = abs(r - g)
        up = max(r,g)+dif
        down = min(r,g)-dif
        if max_B >=up:
            if RGB[2][bisect.bisect_left(RGB[2],up)]==up:
                total-=1
        if min_B <= down and max_B >= down:
            if RGB[2][bisect.bisect_left(RGB[2],down)]==down:
                total-=1
        if dif % 2 == 0:
            center = (r+g)/2
            center_posi=bisect.bisect_left(RGB[2], center)
            if center_posi < len_B:
                if RGB[2][center_posi]==center:
                    total-=1
print(total)

Submission status: Time Limit Exceeded

Solution code:


n = int(input())
s = input()
lis = s
length = len(lis)

def nexts2(pospos):
    i = pospos[0]
    j = pospos[1]
    l = []
    for k in range(j+1, length):
        if lis[k] != lis[i] and lis[k] != lis[j] and j-i != k-j:
            l.append((i, j, k))
    return l

def nexts(pos):
    val = lis[pos]
    l = []
    for i in range(pos+1, length):
        if lis[i] != val:
            l.append((pos, i))
    return list(map(nexts2, l))


a = 0
for w in range(0, n):
    a += (sum(list(map(len, nexts(w)))))
print(a)

Submission status: Time Limit Exceeded

Solution code:
def RGBto123(c):
    if c == 'R':
        return 1
    elif c == 'G':
        return 2
    return 4


N = int(input())
S = list(map(RGBto123, input()))

rel = [1 for i in range(0, N-2) for j in range(i+1, N-1) for k in range(j+1, N) if S[i]+S[j]+S[k]==7 and i+k!=2*j ]



print(sum(rel))

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

Submission status: Time Limit Exceeded

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

if ('R' in S and 'G' in S and 'B' in S):
    for i in range(N-2):
        a = S[i]
        for j in range(i+1,N-1):
            b = S[j]
            if a==b:
                continue
            for k in range(j+1,N):
                c = S[k]
                if (b==c)or(a==c):
                    continue
                if (j-i)!=(k-j):
                    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:

def check(a,b,c):
  if S[a] != S[b] and S[a] != S[c] and S[b] != S[c] and (j-i) != (k-j):
    return 1
  else:
    return 0


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):
      ans += check(i,j,k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c = [0, 0, 0]
col1 = []
col2 = []
col3 = []
res = 0

for i in range(n):
    if s[i] == "R":
        col1.append(i)
    elif s[i] == "G":
        col2.append(i)
    else:
        col3.append(i)

for i1 in col1:
    for i2 in col2:
        for i3 in col3:
            if abs(i2 - i1) == abs(i3 - i2) or abs(i3 - i1) == abs(i3 - i2) or abs(i2 - i1) == abs(i3 - i1):
                continue
            else:
                res = res + 1
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    s = input()

    rgb = defaultdict(list)

    for i in range(n):
        if s[i] == 'R':
            rgb[0].append(i)
        elif s[i] == 'G':
            rgb[1].append(i)
        else:
            rgb[2].append(i)

    ans = len(rgb[0]) * len(rgb[1]) * len(rgb[2])

    for r in rgb[0]:
        for g in rgb[1]:
            tmp = sorted([r, g])
            if tmp[0] - (tmp[1]-tmp[0]) in rgb[2]:
                ans -= 1
            if tmp[1] + (tmp[1]-tmp[0]) in rgb[2]:
                ans -= 1
            if (tmp[1]+tmp[0]) % 2 == 0 and (tmp[1]+tmp[0])//2 in rgb[2]:
                ans -= 1

    print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys
import bisect


sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N = ii()
S = si()

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)

colors = [
    [R, G, B],
    [R, B, G],
    [G, R, B],
    [G, B, R],
    [B, R, G],
    [B, G, R],
]

ans = 0

for C1, C2, C3 in colors:
    for c1 in C1:
        for c2 in C2:
            if c1 > c2:
                continue
            ans += len([c3 for c3 in C3 if c3 > c2 and abs(c3-c2) != abs(c2-c1)])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
S = input()
ans = 0
JK = list()
for k in range(2, N):
    for j in range(1, k):
        if not S[j] == S[k]:
            JK.append((j, k))
for jk in JK:
    j, k = jk
    for i in range(j):
        if S[i] == S[j]:
            continue
        if S[i] == S[k]:
            continue
        if j-i == k-j:
            continue
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    s = 0
    Rl = [i for i in range(N) if S[i] == "R"]
    Gl = [i for i in range(N) if S[i] == "G"]
    Bl = [i for i in range(N) if S[i] == "B"]
    for i in Rl:
        for j in Gl:
            if j > i:
                for k in Bl:
                    if k > j:
                        if k-j != j-i:
                            s += 1
        for j in Bl:
            if j > i:
                for k in Gl:
                    if k > j:
                        if k-j != j-i:
                            s += 1

    for i in Gl:
        for j in Bl:
            if j > i:
                for k in Rl:
                    if k > j:
                        if k-j != j-i:
                            s += 1
        for j in Rl:
            if j > i:
                for k in Bl:
                    if k > j:
                        if k-j != j-i:
                            s += 1

    for i in Bl:
        for j in Rl:
            if j > i:
                for k in Gl:
                    if k > j:
                        if k-j != j-i:
                            s += 1
        for j in Gl:
            if j > i:
                for k in Rl:
                    if k > j:
                        if k-j != j-i:
                            s += 1


    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def judge(i,j,k):
  if j-i == k-j:
    return False
  else:
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      # print(i,j,k, "True")
      return True
  # print(i,j,k, "False")
    else:
      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())  # N<= 4000 6*10^10なので、削減しないとだめ
S = list(input())


# Si Sj Skがどれも別のもの。ただしi<j<k
# また、j-i != k-j
# 1,2,3はだめ1,2,4はok
# 1,3,5もだめ
# この組の数を求める

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


permutation_hash = {'R': [], 'G': [], 'B': []}
for i, char in enumerate(S):
    permutation_hash[char].append(i + 1)
# print(permutation_hash)
count = 0
for R_index in permutation_hash['R']:
    for G_index in permutation_hash['G']:
        for B_index in permutation_hash["B"]:
            indexes = [R_index, G_index, B_index]
            indexes.sort()
            if (indexes[2] - indexes[1]) != (indexes[1] - indexes[0]):
                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):
        for k in range(j+1, N+1):
            if (S[i-1] != S[j-1]) and (S[i-1] != S[k-1]) and (S[j-1] != S[k-1]) 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+1,n-1):
        for k in range(j+1,n):
            if s[j]!=s[i] and s[i]!=s[k] and s[j]!=s[k] and j-i!=k-j:
                ans = 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)

t=0
k=0
for i in range(len(r)):
    for j in range(len(g)):

        if 2*r[i]-g[j] in b:
            k+=1
        if 2*g[j]-r[i] in b:
            k+=1
        if (r[i]+g[j])/2 in b:
            k+=1
        t+=1

print(t*len(b)-k)

Submission status: Time Limit Exceeded

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

#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

Slist = list(S)

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

            if(notEqualColor and notEqualDistance):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
S=list(ns())
Rlist=[]
Glist=[]
Blist=[]
ans=0
for i in range(N):  
    if(S[i]=='R'):
        Rlist.append(i)
    elif(S[i]=='G'):
        Glist.append(i)
    else:
        Blist.append(i)

RGBlist=list(itertools.product(Rlist,Glist,Blist))

tmp=[0]*3
for i in RGBlist:    
    tmp[0]=i[0]
    tmp[1]=i[1]
    tmp[2]=i[2]
    tmp.sort()
    if((tmp[1]-tmp[0])!=(tmp[2]-tmp[1])):
            ans+=1
            
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in range(j+1,N):
            if S[i]==S[k]:
                continue
            if S[j]==S[k]:
                continue
            if not j-i == k-j:
                A += 1
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  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[i] != S[k] and S[j] != S[k] and (j - i) != (k - j):
          ans += 1
  print(ans)





if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Sl = list(S)
Nl = [int(x) for x in range(1,N+1)]
res =0
for (i,Ni) in zip(Sl,Nl):
    x = i
    Nx = Ni
    Sl2 = Sl[Nx:]
    Nl2 = Nl[Nx:]
    for (j,Nj) in zip(Sl2,Nl2):
        y = j
        Ny =Nj 
        Sl3 =Sl[Ny:]
        Nl3 =Nl[Ny:]
        for (k,Nk) in zip(Sl3,Nl3):
            z = k
            Nz = Nk
            Ma = Nz-Ny
            Mb = Ny-Nx
            judge = [x,y,z]
            if len(judge) == len(set(judge)) :
                if Ma != Mb:
                    res =res+1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(str(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(G)*len(B)*len(R)
for r in R:
	for g in G:
		if (2*g-r) in B:
			ans-=1
		if (2*r-g) in B:
			ans-=1
		if (g+r)/2 in B:
			ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

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

# def checkone(a,b,c):
# 	if (a!=b 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 (S[i] != S[j] and S[j] != S[2*j-i] and S[2*j-i] != S[i])
		# 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:
import itertools
import sys
input = sys.stdin.readline  # 改行コード「\n」を呼んでしまうため、文字数など、注意！！

n = int(input().replace("\n",""))
s = input().replace("\n","")

ans = 0

"""
for i in range(n):
    chknum = []
    chknum.append(i)
    for j in range(i+1, n):
        if s[i] != s[j]:
            chknum.append(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
"""

for i, j, k in itertools.product(range(n), range(n), range(n)):
    if (s[i] != s[j]) and (s[i] != s[k]) and (s[j] != s[k]) and (j-i != k-j) and (i < j) and (j < k):
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
S = input()
C = list(itertools.combinations_with_replacement(range(1, N + 1), 3))
count = 0
for i, j, k in C:
    if (i != 2 * j - k) and (S[i - 1] != S[j - 1] and S[i - 1] != S[k - 1] and S[j - 1] != S[k - 1]):
        count += 1
print(count)

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
    # for j in range(1, n):
    #     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:
    #                         count += 1
    # print(count)
    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:
import sys, math

n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
res = 0

r = []
g = []
b = []
for i, c in enumerate(s):
    if c == "R":
        r.append(i)
    if c == "G":
        g.append(i)
    if c == "B":
        b.append(i)


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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
counter=[[0 for i in range(N)] for _ in range(3)]#i+1より右側にあるR(0),G(1),B(2)の数
if S[N-1]=="R":
  counter[0][N-1]=1
elif S[N-1]=="G":
  counter[1][N-1]=1
else:
  counter[2][N-1]=1
for i in range(1,N):
  if S[N-1-i]=="R":
    counter[0][N-1-i]=counter[0][N-i]+1
  elif S[N-1-i]=="G":
    counter[1][N-1-i]=counter[1][N-i]+1
  else:
    counter[2][N-1-i]=counter[2][N-i]+1
  counter[0][N-1-i]=max(counter[0][N-i],counter[0][N-1-i])
  counter[1][N-1-i]=max(counter[1][N-i],counter[1][N-1-i])
  counter[2][N-1-i]=max(counter[2][N-i],counter[2][N-1-i])
ans=0
for i in range(N-1):
  if S[i]=="R":
    for j in range(i+1,N):
      if S[j]=="G":
        ans+=counter[2][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="B":
            ans-=1
      elif S[j]=="B":
        ans+=counter[1][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="G":
            ans-=1
for i in range(N-1):
  if S[i]=="G":
    for j in range(i+1,N):
      if S[j]=="R":
        ans+=counter[2][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="B":
            ans-=1
      elif S[j]=="B":
        ans+=counter[0][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="R":
            ans-=1
for i in range(N-1):
  if S[i]=="B":
    for j in range(i+1,N):
      if S[j]=="R":
        ans+=counter[1][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="G":
            ans-=1
      elif S[j]=="G":
        ans+=counter[0][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="R":
            ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
# 整数の入力
n = int(input())
# 文字列の入力
s = 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[j] != s[k] and s[k] != s[i] and j-i != k-j:
#                 count +=1
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[j] != s[k] and s[k] != s[i] and j-i != k-j:
                    count +=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = input()

ans = 0

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = input()
R = []
G = []
B = []
import bisect
def func(l1,l2,l3,l3set):
  res =0
  for i in l1:
    for j in l2:
      if i>j:
        continue
      a = l3[bisect.bisect_left(l3,j):]
      res += len(a)
      if j*2-i in l3set:
        res -=1
  return res

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)
    
    
R_set = set(R)
G_set = set(G)
B_set = set(B)
ans = func(R,G,B,B_set)+func(R,B,G,G_set)+func(G,R,B,B_set)+func(G,B,R,R_set) + func(B,R,G,G_set)+func(B,G,R,R_set)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

index_r = [i for i, x in enumerate(S) if x =='R']
index_g = [i for i, x in enumerate(S) if x =='G']
index_b = [i for i, x in enumerate(S) if x =='B']

for r in index_r:
    for g in index_g:
        for b in index_b:
            idxs = sorted([r,g,b])
            if 2*idxs[1] != idxs[0]+idxs[2]:
                ans +=1

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

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

RGB = list(S)
count = 0
for a in range(1, N -1):
    for b  in range(a+1, N):
        if not RGB[a-1] == RGB[b-1]: 
            for c in range(b+1, N+1):
                if  RGB[a-1] != RGB[b-1] and RGB[b-1] != RGB[c-1] and RGB[c-1] != RGB[a-1]:
                    if not b-a == c-b:
                        count +=1
        else:
            continue
print(count)

Submission status: Time Limit Exceeded

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

rList = []
gList = []
bList = []

for indx in range(N):
    if S[indx] == 'R':
        rList.append(indx)
    elif S[indx] == 'G':
        gList.append(indx)
    else:
        bList.append(indx)

NoSum = 0
fList = rList
sList = gList
remain = 'B'

for fItem in fList:
    for sItem in sList:
        if (max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem)) <= N-1):
            if (S[max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1

fList = gList
sList = bList
remain = 'R'

for fItem in fList:
    for sItem in sList:
        if (max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem)) <= N-1):
            if (S[max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1

fList = bList
sList = rList
remain = 'G'

for fItem in fList:
    for sItem in sList:
        if (max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem)) <= N-1):
            if (S[max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1


print(len(rList) * len(gList) * len(bList) - NoSum)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(len(S)):    
    for j in range(i, len(S)):    
        for k in range(j, len(S)):
            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 = 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 i in R:
    for j in G:
        for k in B:
            x = sorted([i, j, k])
            if x[2] - x[1] != x[1] - x[0]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

sum=0
x1=""
x2=""
y1=0

for i in range(n-2):
  x1=S[i]
  for j in range(i+1,n-1):
    if(S[j]!=x1):
      x2=S[j]
      y1=j-i
      for k in range(j+1,n):
        if(S[k]!=x1 and S[k]!=x2 and (k-j)!=y1):
          sum=sum+1

print(int(sum))

Submission status: Time Limit Exceeded

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

cnt=0

for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):

      if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
        
        if (j-i)!=(k-j):
          cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import deque
N = int(input())
S = input()
Sd = deque(S)
ans = 0
R, G, B = [], [], []
for i in range(N):
    p = Sd.popleft()
    if p == "R":
        R.append(i)
    elif p == "G":
        G.append(i)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            if g - r != b - g and r < g < b:
                # L.append((r,g,b))
                ans += 1
    for b in B:
        for g in G:
            if b - r != g - b and r < b < g:
                # L.append((r,g,b))
                ans += 1
                
for g in G:
    for b in B:
        for r in R:
            if b - g != r - b and g < b < r:
                # L.append((r,g,b))
                ans += 1
    for r in R:
        for b in B:
            if r - g != b - r and g < r < b:
                # L.append((r,g,b))
                ans += 1

for b in B:
    for g in G:
        for r in R:
            if g - b != r - g and b < g < r:
                # L.append((r,g,b))
                ans += 1
    for r in R:
        for g in G:
            if r - b != g - r and b < r < g:
                # L.append((r,g,b))
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            a = s[i]
            b = s[j]
            for k in range(j+1, n):
                if a != s[k] and b != s[k] and j != (k + i)/2:
                    ans +=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
if n <= 3:
    sum = 0
else:
    sum = 0
    for i in range(n-3):
        if s[i] == "R":
            j = i + 1
            while j <= n-2:
                if s[j] == "G":
                    sum += s[j:n].count("B")
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += s[j:n].count("G")
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                j += 1
        if s[i] == "G":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += s[j:n].count("B")
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += s[j:n].count("R")
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
        if s[i] == "B":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += s[j:n].count("G")
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                if s[j] == "G":
                    sum += s[j:n].count("R")
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
print(sum)