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 = list(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)
    #if i / 2 in R or i / 2 in G or i / 2 in B
count = len(R)*len(G)*len(B)


for a in R:
    for b in G:
        if 2 * a - b in B:
            count -= 1
        if 2 * b - a in B:
            count -= 1
        if (a + b)/2 in B:
            count -= 1
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

RGBs = []
count = 0
RGBs.append({"value": S[0], "left_R": 0, "left_G": 0, "left_B": 0})

for i in range(1, N):
    new = S[i]
    for j in range(i):
        if (S[j] == 'G' and new == 'B') or (S[j] == 'B' and new == 'G'):
            count += RGBs[j]["left_R"]
            if j - (i - j) >= 0 and S[j - (i - j)] == 'R':
                count -= 1

        elif (S[j] == 'R' and new == 'B') or (S[j] == 'B' and new == 'R'):
            count += RGBs[j]["left_G"]
            if j - (i - j) >= 0 and S[j - (i - j)] == 'G':
                count -= 1

        elif (S[j] == 'R' and new == 'G') or (S[j] == 'G' and new == 'R'):
            count += RGBs[j]["left_B"]
            if j - (i - j) >= 0 and S[j - (i - j)] == 'B':
                count -= 1

    tmp_left_R = RGBs[i - 1]["left_R"]
    tmp_left_G = RGBs[i - 1]["left_G"]
    tmp_left_B = RGBs[i - 1]["left_B"]
    if S[i - 1] == 'R':
        tmp_left_R += 1
    elif S[i - 1] == 'G':
        tmp_left_G += 1
    elif S[i - 1] == 'B':
        tmp_left_B += 1
    RGBs.append({"value": S[i], "left_R": tmp_left_R, "left_G": tmp_left_G, "left_B": tmp_left_B})

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
R_cnt = [0] * (N + 1)
B_cnt = [0] * (N + 1)
G_cnt = [0] * (N + 1)
for i in range(N):
    if s[i] == 'R':
        R_cnt[i + 1] = R_cnt[i] + 1
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i]
    elif s[i] == 'B':
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i] + 1
        G_cnt[i + 1] = G_cnt[i]
    else:
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i] + 1
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if s[i] == s[j]:
            continue
            
        if s[i] == 'R' and s[j] == 'B' or s[i] == 'B' and s[j] == 'R':
            ans += G_cnt[N] - G_cnt[j]
        elif s[i] == 'B' and s[j] == 'G' or s[i] == 'G' and s[j] == 'B':
            ans += R_cnt[N] - R_cnt[j]
        elif s[i] == 'R' and s[j] == 'G' or s[i] == 'G' and s[j] == 'R':
            ans += B_cnt[N] - B_cnt[j]
        if j + (j - i) < N and s[i] != s[j] != s[j + (j - i)] != s[i]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

RS = []
GS = []
BS = []
for i, s in enumerate(STR):
    if s == "R":
        RS.append(i+1)
        continue
    if s == "G":
        GS.append(i+1)
        continue
    if s == "B":
        BS.append(i+1)
        continue

total = 0
for i in RS:
    for j in GS:
        for k in BS:
            a, b, c = sorted((i, j, k))
            if b - a != c - b:
                total += 1

print(total)

Submission status: Time Limit Exceeded

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

counta = 0
i = 0

while i < N - 2:
    j = i + 1
    while j < N - 1:
        if S[j] != S[i]:
            k = j + 1
            rest = [l for l in ['R', 'G', 'B'] if l != S[i] and l != S[j]]
            while k < N:
                if S[k] == rest[0] and  k - j != j - i:
                    counta += 1
                k += 1
        j += 1
    i += 1

print(counta)

Submission status: Time Limit Exceeded

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

rgb = set()
init = set(['R', 'G', 'B'])

R = [S[0:i+1].count('R') for i in range(N)]
G = [S[0:i+1].count('G') for i in range(N)]
B = [S[0:i+1].count('B') for i in range(N)]
ans = 0

for i in range(N):
  rgb.add(S[i])
  #S2 = [s for s in S if s != S[i]]
  #N2 = len(S2)
  #print(S2)
  for j in range(i+1, N):
    #print(i, j, ans)
    if S[j] in rgb:
    #if S[i] == S[j]:
     continue
    rgb.add(S[j])
    d1 = j - i
    rest = init - rgb
    #c = list(rest)[0]
    c = rest.pop()
    #tmp = S[j+1:j+d1].count(c)
    #tmp += S[j+d1+1:N].count(c)
    #tmp = S[j+1:N].count(c)
    if c == 'R':
      tmp = R[N-1] - R[j]
    elif c == 'G':
      tmp = G[N-1] - G[j]
    else:
      tmp = B[N-1] - B[j]
    #print(i, j, tmp)
    
    if j+d1 < N and S[j+d1] == c:
      tmp -= 1
    ans += tmp    
    rgb.remove(S[j])
  rgb.remove(S[i])
      
print(ans)

Submission status: Time Limit Exceeded

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


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


NR,NG,NB=0,0,0
for i in range(N):
    if S[i]=="R": 
        NR+=1
    if S[i]=="G": 
        NG+=1
    if S[i]=="B": 
        NB+=1

ans=NR*NG*NB


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

        


print(int(ans))

Submission status: Time Limit Exceeded

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

total = 0

for i in range(n-3):
    a = s[i]
    for j in range(i+1, n-1):
        if s[j] == a:
            continue
        else:
            b = s[j]
            for k in range(j+1, n):
                if k-j != j-i:
                    if s[k] != a and s[k] != b:
                        total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
order=list('RGB')
cnt=[[0,0,0] for _ in range(N)]
for i in range(N):
    cnt[i][order.index(S[i])]+=1
    if i!=0:
        for j in range(3):
            cnt[i][j]+=cnt[i-1][j]
ans=0
for j in range(1,N):
    for k in range(j+1,N):
        # print(j,k)
        if S[j]==S[k]:
            continue
        else:
            s=list(set(order)-set([S[j],S[k]]))[0]
            ans+=cnt[j-1][order.index(s)]
            # print('db',ans)
            i=2*j-k
            if i>=0 and S[i]==s:
                # print('!')
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  s = input()
  
  params = [1,2,2]
  count = 0
  
  while True:
    if params[2] < n:
      params[2] = params[2]+1
    elif params[1] < n-1:
      params[1] = params[1]+1
      params[2] = params[1]+1
    elif params[0] < n-2:
      params[0] = params[0]+1
      params[1] = params[0]+1
      params[2] = params[1]+1
    else:
      break
    
    if params[2] - params[1] == params[1] - params[0]:
      continue
    if s[params[0]-1] == s[params[1]-1]:
      continue
    if s[params[1]-1] == s[params[2]-1]:
      continue
    if s[params[0]-1] == s[params[2]-1]:
      continue
      
    count += 1
  
  return count

if __name__ == '__main__':
  print(main())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []

ans = 0

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)

for i in r:
    for j in g:
        for k in b:
            li = [i,j,k]
            li.sort()
            if li[1]-li[0]==li[2]-li[1]:
                continue
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n=int(input())
S=input()

sum=0

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

len_R=len(R)
len_G=len(G)
len_B=len(B)

for i in range(len_R):
  for j in range(len_G):
    for k in range(len_B):
      x1=max([R[i],G[j],B[k]])
      x2=int(np.median([R[i],G[j],B[k]]))
      x3=min([R[i],G[j],B[k]])
      if(x2-x1 != x3-x2):
	      sum=sum+1

print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

lst = list(product(r,g,b))
ans = 0
for i in range(len(lst)):
    tmp = sorted(lst[i])
    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()
ans=0
r=[]
g=[]
b=[]
for i in range(n):
    tmp=s[i]
    if tmp=="R":
        r.append(i+1)
    elif tmp=="G":
        g.append(i+1)
    else:
        b.append(i+1)
for i in r:
    for j in g:
        for k in b:
            if (i<j and j<k):
                if j-i!=k-j:
                    ans+=1
            elif (i<k and k<j):
                if k-i!=j-k:
                    ans+=1
            elif (j<i and i<k):
                if i-j!=k-i:
                    ans+=1
            elif (j<k and k<i):
                if k-j!=i-k:
                    ans+=1
            elif (k<i and i<j):
                if i-k!=j-i:
                    ans+=1
            elif (k<j and j<i):
                if j-k!=i-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(res)

Submission status: Time Limit Exceeded

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

r_index = []
g_index = []
b_index = []

for i in range(N):
    if S[i] == 'R':
        r_index.append(i)

    if S[i] == 'G':
        g_index.append(i)

    if S[i] == 'B':
        b_index.append(i)

res = 0

# print(len(r_index), len(g_index), len(b_index))

max_numbers = len(r_index) * len(g_index) * len(b_index)
# print(max_numbers)


def cnt(x, y, z):
    res = 0
    for xi in x:
        for yi in y:
            for zi in z:
                if yi - xi == zi - yi:
                    res += 1

        for zi in z:
            for yi in y:
                if zi - xi == yi - zi:
                    res += 1

    for yi in y:
        for xi in x:
            for zi in z:
                if xi - yi == zi - xi:
                    res += 1

        for zi in z:
            for xi in x:
                if zi - yi == xi - zi:
                    res += 1

    for zi in z:
        for xi in x:
            for yi in y:
                if xi - zi == yi - xi:
                    res += 1

        for yi in y:
            for xi in x:
                if yi - zi == xi - yi:
                    res += 1

    return res


ans = []

ans.append(cnt(r_index, g_index, b_index))
# ans.append(cnt(b_index, r_index, g_index))
# ans.append(cnt(g_index, b_index, r_index))

# ans.append(cnt(r_index, b_index, g_index))
# ans.append(cnt(b_index, r_index, g_index))

# print(sum(ans)//2)
print(max_numbers - sum(ans)//2)

Submission status: Time Limit Exceeded

Solution code:
input()
data = input()

count = 0

position = 0
for p, letter in enumerate(data):
# poition = 0
# answer = [(letter, position)]
# stack = ''

    for i, t in enumerate(data[p+1:]):
        if letter == t:
            continue
        else:
    #         stack = copy.copy(t)
            for j, s in enumerate(data[p+1 + i+1:]):
                if t == s or letter == s:
                    continue
                else:
                    if (i+p+1) - p == (j+i+1) - i:
                        pass
                    else:
                        count += 1
#                         print('{}, {}, {}'.format(letter, t, s))
print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N = int(input())
S = list(input())

r = [i for i, x in enumerate(S) if x == "R"]
g = [i for i, x in enumerate(S) if x == "G"]
b = [i for i, x in enumerate(S) if x == "B"]
kotae = 0
# print(r,g,b)
rgb = [r,g,b]
len1 = [len(r),len(g),len(b)]
n3 = len1.index(min(len1))
if n3 == 0:
    n1,n2 = 1,2
elif n3 == 1:
    n1,n2 = 0,2
else:
    n1,n2 = 0,1
# print(len1)
# print(n1,n2,n3)

for i in rgb[n3]:
    k = 1
    y = len(rgb[n1])*len(rgb[n2])
    # print("y = ",y)
    while i-k >=0 or i+k < N+2:
        # print(i,k)
        if i-k in rgb[n1] and i+k in rgb[n2]:
            y -=1
        if i-k in rgb[n2] and i+k in rgb[n1]:
            y -= 1
        if i-k in rgb[n2] and i-2*k in rgb[n1]:
            y -= 1
        if i-k in rgb[n1] and i-2*k in rgb[n2]:
            y -= 1
        if i+k in rgb[n1] and i+2*k in rgb[n2]:
            y -= 1
        if i+k in rgb[n2] and i+2*k in rgb[n1]:
            y -= 1
        k += 1
    kotae += y
print(kotae)

Submission status: Time Limit Exceeded

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

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

print(S.count("R")*S.count("G")*S.count("B")-cnt)

Submission status: Time Limit Exceeded

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

counta = 0
i = 0

while i < N - 2:
    j = i + 1
    while j < N - 1:
        if S[j] != S[i]:
            k = j + 1
            rest = [l for l in ['R', 'G', 'B'] if l != S[i] and l != S[j]]
            while k < N:
                if k - j != j - i and S[k] == rest[0]:
                    counta += 1
                k += 1
        j += 1
    i += 1

print(counta)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(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)
lenb=len(B)
for r in R:
    for g in G:
        flag=0
        for b in B:
            a=r+g+b
            if a%3==0:
                a=a//3
                if a==r or a==g or a==b:
                    flag=flag+1
        ans=ans+lenb-flag
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# これは, u749194f さんの改造です。
# オリジナルではございません。
# https://atcoder.jp/contests/abc162/submissions/11825963
n = int(input())
s = input()
flag = True
if len(set(s))<=2:
    print(0)
    flag = False
if flag:
    r = [i+1 for i in range(n) if s[i]=='R']
    b = [i+1 for i in range(n) if s[i]=='B']
    g = [i+1 for i in range(n) if s[i]=='G']
    minus = 0
    def cnt_cmb(a, b, c):
        cnt = 0
        for i in a:
            for j in b:
                if (i+j)/2 in c:
                    cnt += 1
        return cnt
    minus += cnt_cmb(r, b, g)
    minus += cnt_cmb(b, g, r)
    minus += cnt_cmb(g, r, b)
    print(len(r)*len(b)*len(g)-minus)

Submission status: Time Limit Exceeded

Solution code:
#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N = int(input())
S = input()
result = 0
a = 1
while (a <= N -3):
    b = a + 1
    while (b <= N - 1):
        c = b + 1
        if (S[a - 1] == S[b - 1]):
            b += 1
            continue
        while (c <= N):
            if ((b - a) == (c - b)):
                c += 1
                continue
            if (S[a - 1] != S[c - 1] and S[b - 1] != S[c - 1]):
                result += 1
            c += 1
        b += 1
    a += 1
print(result)

Submission status: Time Limit Exceeded

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

import numpy as np

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

rind = set()
gind = set()
bind = set()

N = int(input())
S = input()
for i, ch in enumerate(S):
    if ch == 'R':
        rind.add(i)
    elif ch == 'G':
        gind.add(i)
    else:
        bind.add(i)

#print(rind)
#print(gind)
#print(bind)
total = len(rind) * len(gind) * len(bind)
choices = set()
for r in rind:
    for g in gind:
        diff = abs(r - g)
        i, j = min([r, g]), max([r, g])
        if i - diff in bind:
            choices.add(tuple([i-diff, i, j]))
        if j + diff in bind:
            choices.add(tuple([i, j, j + diff]))
        if diff % 2 == 0 and (i + (diff // 2)) in bind:
            choices.add(tuple([i, i + diff // 2, j]))
#print(choices)
print(total - len(choices))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R = list()
G = list()
B = list()
g = 0
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)
for i in R:
    for j in G:
        z = abs(i-j)
        if z + max(i,j) in B:
            g += 1
        if min(i,j) - z in B:
            g += 1
        if z % 2==0 and z // 2 + min(i,j) in B:
                g += 1

print(len(R)*len(G)*len(B)-g)

Submission status: Time Limit Exceeded

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

ans = S.count("R") * S.count("G") * S.count("B")

res = 0
for i in range(N-2):
    for d in range(1, (N-i)//2+1):
        if i+2*d < N and set([S[i], S[i+d], S[i+2*d]]) == {"R", "G", "B"}:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
z=input().split()
s=input()
a = 0
for i in range(len(s)):
  for i2 in range(i+1,len(s)):
    if(s[i] == s[i2]):
      continue
    for i3 in range(i2+1,len(s)):
      if(s[i]==s[i3] or s[i2]==s[i3]):
        continue
      if(i2-i == i3-i2):
        continue
      a+=1
print (a)

Submission status: Time Limit Exceeded

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

d = {"R":0, "G":1, "B":2}
a = [[],[],[]]
for index,x in enumerate(s):
    a[d[x]].append(index)


cnt = 0
for r in a[0]:
    for g in a[1]:
        for b in a[2]:
            tmp = sorted([r,g,b])
            if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
sum=0
for i in range(N-2):
    rgb = ['R','G','B']
    rgb.remove(S[i])
    for j in range(2):
        idx=0
        tmp=i
        while True:
            idx=S.find(rgb[j], tmp+1)
            if idx==-1: break
            sum+=S.count(rgb[1-j], idx+1)
            if 2*idx-i<=len(S)-1 and S[2*idx-i]==rgb[1-j]: sum-=1
            tmp=idx
print(sum)

Submission status: Time Limit Exceeded

Solution code:
#problem D
input_d41 = int(input())
input_d42 = input()

N = int(input_d41)
S = input_d42

R = S.count('R')
G = S.count('G')
B = N - (R + G)

ans = R*G*B
answ = 0

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

Submission status: Time Limit Exceeded

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

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

print(answer)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input()) + ['X']*100000
c = 0
for i in range(n):
    for j in range(i + 1, n):
        s_try = [] + s
        p = s_try[i]
        q = s_try[j]
        if p != q:
            del s_try[2*j - i]
            s_counter = s_try[j+1:]
            if s_try[i] == 'R':
                if s_try[j] == 'G':
                    c += s_counter.count('B')
                elif s_try[j] == 'B':
                    c += s_counter.count('G')
            elif s_try[i] == 'G':
                if s_try[j] == 'R':
                    c += s_counter.count('B')
                elif s_try[j] == 'B':
                    c += s_counter.count('R')
            else:
                if s_try[j] == 'R':
                    c += s_counter.count('G')
                elif s_try[j] == 'G':
                    c += s_counter.count('R')
print(c)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

n_list = list(range(n))

mylist = list(itertools.combinations(n_list,3))

ans = 0

for i in range(len(mylist)):
  if s[mylist[i][0]] != s[mylist[i][1]] and s[mylist[i][0]] != s[mylist[i][2]] and s[mylist[i][2]] != s[mylist[i][1]]:
    if mylist[i][1] - mylist[i][0] != mylist[i][2] - mylist[i][1]:
      ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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):
            si = S[i]
            sj = S[j]
            if not ( k == 2*j-i or si==S[k] or sj==S[k] ):
                A += 1
print(A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 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):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

# process
R = []
G = []
B = []
for i, s in enumerate(list(S)):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)

ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        i, j = r, g
        if i > j:
            i, j = j, i
        
        s = j-i
        t1 = -1
        if s%2 == 0:
            t1 = i+s//2
        t2 = i-s
        t3 = j+s
        
        for b in B:
            if b in (t1, t2, t3):
                ans -= 1

# output
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def get_diff_s(a, b):
    return list({'R', 'G', 'B'} - {a, b})[0]

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

r_cnt = 0
g_cnt = 0
b_cnt = 0

r_cnt_list = []
g_cnt_list = []
b_cnt_list = []
for si in s[::-1]:
    if si == 'R':
        r_cnt += 1
    elif si == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r_cnt_list.append(r_cnt)
    g_cnt_list.append(g_cnt)
    b_cnt_list.append(b_cnt)

r_cnt_list.sort(reverse=True)
g_cnt_list.sort(reverse=True)
b_cnt_list.sort(reverse=True)

ans = 0
for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ds = get_diff_s(s[i], s[j])
        di = j - i
        if ds == 'R':
            ans += r_cnt_list[j + 1]
            if j + di < len(s) and s[j + di] == 'R':
                ans -= 1
        if ds == 'G':
            ans += g_cnt_list[j + 1]
            if j + di < len(s) and s[j + di] == 'G':
                ans -= 1
        if ds == 'B':
            ans += b_cnt_list[j + 1]
            if j + di < len(s) and s[j + di] == 'B':
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

a = 0

for i in range(n):
  if s[i]=='R':
    for j in range(n):
      if s[j]=='G':
        for k in range(n):
          if s[k]=='B':
            if i<j<k:
              if (j-i)!=(k-j):
                a = a + 1
            elif i<k<j:
              if (k-i)!=(j-k):
                a = a + 1
            elif j<i<k:
              if (i-j)!=(k-i):
                a = a + 1
            elif j<k<i:
              if (k-j)!=(i-k):
                a = a + 1
            elif k<i<j:
              if (i-k)!=(j-i):
                a = a + 1
            elif k<j<i:
              if (j-k)!=(i-j):
                a = a + 1
          
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)
ans = 0
for r_i in r:
    for g_i in g:
        for b_i in b:
            check = [r_i, g_i, b_i]
            check.sort()
            if check[1]-check[0] != check[2]-check[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(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)
    lr=len(R)
    lg=len(G)
    ans=lr*lg*(N-lr-lg)

    for i in R:
        for j in G:
            for k in B:
                L=sorted([i,j,k])
                if L[-1]-L[1]==L[1]-L[0]:
                    ans-=1
    print(ans)




resolve()

Submission status: Time Limit Exceeded

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

ans = 0
R = [i for i, x in enumerate(S) if x == 'R']
G = [i for i, x in enumerate(S) if x == 'G']
B = [i for i, x in enumerate(S) if x == 'B']

for i,j,k in product(R,G,B):
    ijk = sorted([i,j,k])
    if (ijk[1]-ijk[0]) != (ijk[2]-ijk[1]):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
a=0
for i,m in enumerate(s):
    for j,M in enumerate(s[:i]):
        if m!=M:
            k=3-"RGB".index(m)-"RGB".index(s[j])
            if 2*i-j<=n-1:
                if s[2*i-j]=="RGB"[k]:
                    a-=1
print(r*g*b+a)

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"]

    RG = [(i,j) for i in Rl for j in Gl if j > i]
    GR = [(i,j) for i in Gl for j in Rl if j > i]
    GB = [(i,j) for i in Gl for j in Bl if j > i]
    BG = [(i,j) for i in Bl for j in Gl if j > i]
    BR = [(i,j) for i in Bl for j in Rl if j > i]
    RB = [(i,j) for i in Rl for j in Bl if j > i]

    for ij in RG:
        for k in Bl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    for ij in GR:
        for k in Bl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    for ij in GB:
        for k in Rl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    for ij in BG:
        for k in Rl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    for ij in BR:
        for k in Gl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    for ij in RB:
        for k in Gl:
            if k > ij[1]:
                if ij[1] - ij[0] != k - ij[1]:
                    s += 1
    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

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

res = s.count('R') * s.count('G') * s.count('B')

for ni,i in enumerate(s[:-2]):
    lst3 = ['R','G','B']
    lst3.remove(i)
    for nj in range(ni+1,n):
        nk = nj + (nj - ni)
        if nk < n:
            if set(lst3) == set([s[nj],s[nk]]):
                res -= 1
        else:
            break

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
S=input()
iR = [i for i,x in enumerate(S) if x=='R']
iG = [i for i,x in enumerate(S) if x=='G']
iB = [i for i,x in enumerate(S) if x=='B']
ol=0
for i in iR:
    for j in iG:
        for k in iB:
            l=sorted([i,j,k])
            if l[1]-l[0]==l[2]-l[1]:
                ol+=1
print(len(iR)*len(iG)*len(iB)-ol)

Submission status: Time Limit Exceeded

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

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

res = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            tmp = rgb - {S[i], S[j]}
            ok = True
            k = j
            while ok:
                k = S.find(list(tmp)[0], k + 1)
                if k == -1:
                    ok = False
                elif k - j != j - i:
                    res += 1
                else:
                    pass
print(res)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

c = Counter(s)

dbl = c["R"] * c["B"] * c["G"]
for l in range(1, n):
    for i in range(n):
        j = i + l
        k = j + l
        if j < n and k < n:
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                dbl -= 1

print(dbl)

Submission status: Time Limit Exceeded

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

Ans = 0
for i in range(N):
  s = S[i]
  a = 0
  b = 0
  L = ["R","G","B"]
  L.remove(s)
  for j in range(i+1,N):
    t = S[j]
    if s != t:
      if t == L[0]:
        a +=1
      else:
        b +=1
  Ans += a*b

for i in range(N):
  for j in range(i+1,N):
    s = S[i]
    t = S[j]
    k = 2*j-i
    if k <= N-1:
      u = S[k]
      if s != t and t != u and u != s:
        Ans -=1
print(Ans)

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):
        nn = n-j
        if s[i] == s[j]:
            continue
        w = j - i
        if j+w < n and (s[j+w] != s[j] and s[j+w] != s[i]):
            cnt -= 1
        not_same = nn - (s[j:].count(s[i]) + s[j:].count(s[j]))
        cnt += not_same

print(cnt)