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:
def main():
    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)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
I = input()

R = []
G = []
B = []
for i in range(N):
    if I[i] == 'R':
        R.append(i)
    if I[i] == 'G':
        G.append(i)
    if I[i] == 'B':
        B.append(i)

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

for a in R:
    for b in G:
        for c in B:
            abs1 = abs(a - b)
            abs2 = abs(b - c)
            abs3 = abs(c - a)
            
            if( abs1 == abs2 or abs2 == abs3 or abs3 == abs1 ):
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
I = input()

R = []
G = []
B = []
for i in range(N):
    if I[i] == 'R':
        R.append(i)
    if I[i] == 'G':
        G.append(i)
    if I[i] == 'B':
        B.append(i)

ans = 0

for a in R:
    for b in G:
        for c in B:
            n = sorted([a, b, c])
            if( (n[2] - n[1]) != (n[1] - n[0]) ):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s_list= list(input())
r_list=[]
g_list=[]
b_list=[]
count=0
rr,gg,bb=0,0,0
for i in range(0,n):
    if s_list[i]=="R":
        r_list.append(i)
        rr=rr+1
    elif s_list[i]=="G":
        g_list.append(i)
        gg=gg+1
    else:
        b_list.append(i)
        bb=bb+1
sum=rr*gg*bb

for pp in r_list:
    for qq in g_list:
        for rr in b_list:
            if rr-pp == -1*(qq-pp) or rr-pp ==2*(qq-pp) or qq-pp ==2*(rr-pp) :
                count=count+1
print(sum-count)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r, g, b = 0, 0, 0

for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        nk = 2 * j - i
        if not nk < n:
            continue
        snk = s[nk]
        if {si, sj, snk} == {'R','G','B'}:
            count -= 1

print("{}".format(count))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r, g, b = 0, 0, 0

for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        if si == sj:
            continue
        c_set = {'R','G','B'} - {si, sj}
        nk = 2 * j - i
        if not (2 * j - i) < n:
            continue
        if s[nk] in c_set:
            count -= 1

print("{}".format(count))

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

Submission status: Time Limit Exceeded

Solution code:
def main():
 
  import sys
  
  def input():
    return(sys.stdin.readline()[:-1])
  
  import collections
 
  N = int(input())
  S = list(input())
 
  CNT_S = collections.Counter(S)
 
  cnt_r = CNT_S['R']
  cnt_b = CNT_S['B']
  cnt_g = CNT_S['G']
 
  total = cnt_r * cnt_g * cnt_b
  subdu = 0
 
 
  for pos in range(N):
      for i in range(1, N):
 
          index_r = pos
          index_m = pos + i
          index_l = pos + 2 * i
 
          if (index_l < N and index_m < N and index_r < N):
              if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                  subdu += 1
 
 
  ans = total - subdu
 
  print(ans)
  
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r, g, b = 0, 0, 0

for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        if si == sj:
            continue
        c_set = {'R','G','B'}
        c_set = c_set - {si,sj}
        nk = 2 * j - i
        if not nk < n:
            continue
        if not nk > j:
            continue
        if s[nk] in c_set:
            count -= 1

print("{}".format(count))

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s_list= list(input())
count=0
rr,bb,gg=0,0,0
for i in range(0,n):
    if s_list[i]=="R":
        rr=rr+1
    elif s_list[i]=="G":
        gg=gg+1
    else:
        bb=bb+1
sum=rr*gg*bb

for p in range(0,n-2):
    for q in range(p+1,n-1):
        if s_list[p] == s_list[q]:
            continue
        else:
            for r in range(q+1,n):
                if s_list[r]==s_list[p] or s_list[r]==s_list[q] :
                    continue
                elif r-q==q-p:
                    count = count+1
print(sum-count)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r, g, b = 0, 0, 0

for k,i in enumerate(s):
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        if si == sj:
            continue
        c_set = {'R','G','B'}
        c_set = c_set - {si,sj}
        nk = 2 * j - i
        if not nk < n:
            continue
        if s[nk] in c_set:
            count -= 1

print("{}".format(count))

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

n=int(input().strip())
s=input().strip()

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

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s_list= list(input())
count=0
rr,bb,gg=0,0,0
for i in range(0,n):
    if s_list[i]=="R":
        rr=rr+1
    elif s_list[i]=="G":
        gg=gg+1
    else:
        bb=bb+1
sum=rr*gg*bb

for p in range(0,n-2):
    for q in range(p+1,n-1):
        for r in range(q+1,n):
            if s_list[r]!=s_list[p] and s_list[r]!=s_list[q] and s_list[p]!=s_list[q] and r-q==q-p:
                count = count+1


print(sum-count)

Submission status: Time Limit Exceeded

Solution code:
number = int(input())
character = input()
count = 0
for i in range(number - 3):
    first_character = character[i]
    for j in range(i + 1, number -1):
        second_character = character[j]
        if first_character == second_character:
            continue
        for k in range(j + 1, number):
            if j  - i == k - j:
                continue
            third_character = character[k]
            if first_character == third_character or second_character == third_character:
                continue
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r_list = []
g_list = []
b_list = []

for k,i in enumerate(s):
    if i == 'R':
        r_list.append(k)
    elif i == 'G':
        g_list.append(k)
    elif i == 'B':
        b_list.append(k)

count = len(r_list) * len(g_list) * len(b_list)

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        c_set = {'R','G','B'}
        c_set = c_set - {si,sj}
        nk = 2 * j - i
        if c_set == {'R'} and nk in r_list:
            count -= 1
        elif c_set == {'G'} and nk in g_list:
            count -= 1
        elif c_set == {'B'} and nk in b_list:
            count -= 1        

print("{}".format(count))

Submission status: Time Limit Exceeded

Solution code:
def main():
 
  import sys
  input = sys.stdin.readline
  
  import collections
 
  N = int(input())
  S = list(input())
 
  CNT_S = collections.Counter(S)
 
  cnt_r = CNT_S['R']
  cnt_b = CNT_S['B']
  cnt_g = CNT_S['G']
 
  total = cnt_r * cnt_g * cnt_b
  subdu = 0
 
 
  for pos in range(N):
      for i in range(1, N):
 
          index_r = pos
          index_m = pos + i
          index_l = pos + 2 * i
 
          if (index_l < N and index_m < N and index_r < N):
              if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                  subdu += 1
 
 
  ans = total - subdu
 
  print(ans)
  
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():

  import collections

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

  CNT_S = collections.Counter(S)

  cnt_r = CNT_S['R']
  cnt_b = CNT_S['B']
  cnt_g = CNT_S['G']

  total = cnt_r * cnt_g * cnt_b
  subdu = 0


  for pos in range(N):
      for i in range(1, N):

          index_r = pos
          index_m = pos + i
          index_l = pos + 2 * i

          if (index_l < N and index_m < N and index_r < N):
              if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                  subdu += 1


  ans = total - subdu

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product as p

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

def f(n, s):
  dic = {'R': [], 'G':[], 'B':[]}
  res = 0
  
  for i in range(N):
    dic[S[i]].append(i)
    
  for tpl in p(*dic.values()):
    l = sorted(tpl)
    if (l[2] + l[0])/2 != l[1]:
      res += 1
      
  return res

print(f(N, S))

Submission status: Time Limit Exceeded

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

R_lis = []
G_lis = []
B_lis = []

num = 0


for word in S:
    num += 1
    if word == 'R':
        R_lis.append(num)
    elif word == 'G':
        G_lis.append(num)
    elif word == 'B':
        B_lis.append(num)

if len(R_lis) >= len(G_lis) and len(R_lis) >= len(B_lis):
    x,y,z =  G_lis,B_lis,R_lis

if len(G_lis) >= len(B_lis) and len(G_lis) >= len(R_lis):
    x,y,z =  R_lis,B_lis,G_lis

if len(B_lis) >= len(G_lis) and len(B_lis) >= len(R_lis):
    x,y,z =  G_lis,R_lis,B_lis


irr = 0


for i in x:
    for j in y:
        if max(i,j) + abs(i-j) in z:
            irr += 1
        if min(i,j) - abs(i-j) in z:
            irr += 1
        if (i + j)/2 in z:
            irr += 1

ans = len(R_lis)*len(G_lis)*len(B_lis) - irr

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
r_list = []
g_list = []
b_list = []
l_set = {'R','G','B'}

for k,i in enumerate(s):
    if i == 'R':
        r_list.append(k)
    elif i == 'G':
        g_list.append(k)
    elif i == 'B':
        b_list.append(k)

count = len(r_list) * len(g_list) * len(b_list)

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        c_set = l_set.copy()
        c_set = c_set - {si} - {sj}
        nk = 2 * j - i
        if c_set == {'R'} and nk in r_list:
            count -= 1
        elif c_set == {'G'} and nk in g_list:
            count -= 1
        elif c_set == {'B'} and nk in b_list:
            count -= 1        

print("{}".format(count))

Submission status: Time Limit Exceeded

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

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


print(num)

Submission status: Time Limit Exceeded

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

arr = []
R = len(list(filter(lambda x:x == 'R', s)))
G = len(list(filter(lambda x:x == 'G', s)))
B = len(list(filter(lambda x:x == 'B', s)))
tot = R*G*B

for i in range(n):
  for j in range(i+1, n):
    k = 2*j-i
    if k >= n:
      continue
    if (s[i] != s[j]) & (s[i] != s[k]) & (s[j] != s[k]):
      tot -= 1

print(tot)

Submission status: Time Limit Exceeded

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

arr = []
R = len(list(filter(lambda x:x == 'R', s)))
G = len(list(filter(lambda x:x == 'G', s)))
B = len(list(filter(lambda x:x == 'B', s)))
tot = R*G*B

for i in range(n):
  for j in range(i+1, n):
    k = 2*j-i
    if k < n:
      if (s[i] != s[j]) & (s[i] != s[k]) & (s[j] != s[k]):
        tot -= 1

print(tot)

Submission status: Time Limit Exceeded

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

rcs = [0] * n
gcs = [0] * n
bcs = [0] * n

for i in range(n):
   if s[i] == 'R':
      rcs[i] = 1
   elif s[i] == 'G':
      gcs[i] = 1
   elif s[i] == 'B':
      bcs[i] = 1

for i in range(1, n):
   rcs[i] += rcs[i-1]
   gcs[i] += gcs[i-1]
   bcs[i] += bcs[i-1]

result = 0
for i in range(n):
   if s[i] == 'R':
      for j in range(i+1, n):
         if s[j] == 'R':
            continue

         elif s[j] == 'G':
            result += bcs[n-1] - bcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'B':
               result -= 1

         elif s[j] == 'B':
            result += gcs[n-1] - gcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'G':
               result -= 1

   elif s[i] == 'G':
      for j in range(i+1, n):
         if s[j] == 'G':
            continue

         elif s[j] == 'R':
            result += bcs[n-1] - bcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'B':
               result -= 1

         elif s[j] == 'B':
            result += rcs[n-1] - rcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'R':
               result -= 1

   elif s[i] == 'B':
      for j in range(i+1, n):
         if s[j] == 'B':
            continue

         elif s[j] == 'R':
            result += gcs[n-1] - gcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'G':
               result -= 1

         elif s[j] == 'G':
            result += rcs[n-1] - rcs[j]
            t = 2 * j - i
            if t < n and s[t] == 'R':
               result -= 1

print(result)

Submission status: Time Limit Exceeded

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

arr = []
R = len(list(filter(lambda x:x == 'R', s)))
G = len(list(filter(lambda x:x == 'G', s)))
B = len(list(filter(lambda x:x == 'B', s)))
tot = R*G*B

for i in range(n):
  for j in range(i+1, n):
    k = 2*j-i
    if k < n:
      if (s[i] != s[j]) & (s[i] != s[k]) & (s[j] != s[k]):
        tot -= 1

print(tot)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
point = 0
letter_set = {'R','G','B'}

def last_point(c):
    num = None
    for index,letter in enumerate(s):
        if letter == c:
            num = index
    return index

r_num = last_point('R')
g_num = last_point('G')
b_num = last_point('B')

for i in range(point,n-2):
    si = s[i]
    for j in range(i+1,n-1):
        if s[j] != si:
            sj = s[j]
        else:
            continue
        for k in range(j+1,n):
            if s[k] != si and s[k] != sj:
                sk = s[k]
            else:
                continue
            
            num = 0
            l_set = letter_set.copy()
            l_set = l_set - {si} - {sj}
            
            if l_set == {'R'}:
                num = r_num
            elif l_set == {'G'}:
                num = g_num
            elif l_set == {'B'}:
                num = b_num

            if k > num:
                break
            
            if (j - i) != (k - j):
                count += 1

print("{}".format(count))

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(input())
count = 0
br = ["R",'G','B']
for i in range(a-2):
    for j in range(i+1,a-1):
        if ar[i] != ar[j]:
            br.remove(ar[i])
            br.remove(ar[j])
            cr = ar[j+1:a]
            count += cr.count(br[0])
            if (j -i) + j < a:
                if ar[(j-i) + j] == br[0]:
                    count -= 1
            br = ['R','G','B']
print(count)

Submission status: Time Limit Exceeded

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

R_lis = []
G_lis = []
B_lis = []

num = 0


for word in S:
    num += 1
    if word == 'R':
        R_lis.append(num)
    elif word == 'G':
        G_lis.append(num)
    elif word == 'B':
        B_lis.append(num)


irr = 0


for i in R_lis:
    for j in G_lis:
        if max(i,j) + abs(i-j) in B_lis:
            irr += 1
        if min(i,j) - abs(i-j) in B_lis:
            irr += 1
        if (i + j)/2 in B_lis:
            irr += 1

ans = len(R_lis)*len(G_lis)*len(B_lis) - irr

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

count = 0
point = 0
for i in range(point,n-2):
    si = s[i]
    for j in range(i+1,n-1):
        if s[j] != si:
            sj = s[j]
        else:
            continue
        for k in range(j+1,n):
            if s[k] != si and s[k] != sj:
                sk = s[k]
            else:
                continue
            
            if (j - i) != (k - j):
                count += 1

print("{}".format(count))

Submission status: Time Limit Exceeded

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

dic = {'R':[], 'G':[], 'B':[]}

num = 1


for word in S:
    dic[word].append(num)
    num += 1

irr = 0

for i in dic['R']:
    for j in dic['G']:
        if max(i,j) + abs(i-j) in dic['B']:
            irr += 1
        if min(i,j) - abs(i-j) in dic['B']:
            irr += 1
        if (i + j)/2 in dic['B']:
            irr += 1

ans = len(dic['R'])*len(dic['G'])*len(dic['B']) - irr

print(ans)

Submission status: Time Limit Exceeded

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

dic = {'R':[], 'G':[], 'B':[]}

num = 1


for word in S:
    dic[word].append(num)
    num += 1

irr = 0

for i in dic['R']:
    for j in dic['G']:
        t = 0
        for k in dic['B']:
            lis = [i,j,k]
            lis1 = lis.sort()
            if lis[2]-lis[1] == lis[1]-lis[0]:
                irr += 1
                t += 1
            if t == 3:
                break

ans = len(dic['R'])*len(dic['G'])*len(dic['B']) - irr

print(ans)

Submission status: Time Limit Exceeded

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

dic = {'R':[], 'G':[], 'B':[]}

num = 1


for word in S:
    dic[word].append(num)
    num += 1

irr = 0

for i in dic['R']:
    for j in dic['G']:
        for k in dic['B']:
            lis = [i,j,k]
            lis1 = lis.sort()
            if lis[2]-lis[1] == lis[1]-lis[0]:
                irr += 1

ans = len(dic['R'])*len(dic['G'])*len(dic['B']) - irr

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
nsum = 0
for s in S:
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    else:
        B += 1
count = 0
for i in range(N):
    for k in range(i+1,N):
        if not S[i] == S[k]:
            if 2*k - i <= N - 1:
                if S[i] != S[2*k - i] and S[k] != S[2*k - i]:
                    count += 1
print(R*G*B - count)

Submission status: Time Limit Exceeded

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

all = S.count("R") * S.count("G") * S.count("B")
count = 0
for i in range(N):
    for j in range(1,N):
        k = 2*j - i
        if k < N and i < j:
            if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]):
                count += 1
print(all-count)

Submission status: Time Limit Exceeded

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

arr = []
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]) & (s[i] != s[k]) & (s[j] != s[k]):
        arr.append((i,j,k))

sum = 0
for l in range(len(arr)):
  if abs(arr[l][1] - arr[l][0]) != abs(arr[l][2] - arr[l][1]):
    sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

r=S.count('R')
g=S.count('G')
b=S.count('B')

ans=r*g*b
dib=0


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

Submission status: Time Limit Exceeded

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

r=S.count('R')
g=S.count('G')
b=S.count('B')


ans=r*g*b
dib=0

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

Submission status: Time Limit Exceeded

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

R=[0]*N
G=[0]*N
B=[0]*N

for i in range(N):
    if S[i]=='R':
        R[i]=1
    elif S[i]=='G':
        G[i]=1
    elif S[i]=='B':
        B[i]=1
r=sum(R)
g=sum(G)
b=sum(B)

ans=r*g*b
dib=0

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}
L = {
    'R': tuple(i for i, v in enumerate(S) if v == 'R'),
    'G': tuple(i for i, v in enumerate(S) if v == 'G'),
    'B': tuple(i for i, v in enumerate(S) if v == 'B')
}

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] != S[j]:
            other = list({'R', 'G', 'B'} - {S[i], S[j]})[0]
            tmp = j + j - i

            ans += len([k for k in L[other] if (k > j) and (tmp != k)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}
L = {
    'R': [i for i, v in enumerate(S) if v == 'R'],
    'G': [i for i, v in enumerate(S) if v == 'G'],
    'B': [i for i, v in enumerate(S) if v == 'B']
}

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] != S[j]:
            other = list({'R', 'G', 'B'} - {S[i], S[j]})[0]
            ans += len([k for k in L[other] if (k > j) and (j - i != k - j)])
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):
        for k in range(j):
            if s[i] != s[j] and 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()
S = list(s)
a = 0
for k in range(n):
        for j in range(k):
            if S[j] != S[k]:
                for i in range(j):
                    if   S[i] != S[j] and S[k] != S[i] and 2*j != i + k:
                        # print(i, j, k)
                        a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
S = list(s)
a = 0
for k in range(n):
        for j in range(k):
            for i in range(j):
                if  S[i] != S[j] and S[j] != S[k] and S[k] != S[i] and 2*j != i + k:
                    # print(i, j, k)
                    a += 1
print(a)

Submission status: Time Limit Exceeded

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
input_num = int(input())
input_list = list(input())
total = 0

for i in range(input_num):
    for j in range(input_num):
        for k in range(input_num):
            # index restriction
            if i < j < k and j - i != k - j:
                # String restriction
                if input_list[i] != input_list[j] and input_list[i] != input_list[k] and input_list[j] != input_list[k]:
                    total += 1
                else:
                    pass
            else:
                pass
print(total)

Submission status: Time Limit Exceeded

Solution code:
input_num = int(input())
input_list = list(input())
total = 0

for i in range(input_num):
    for j in range(input_num):
        for k in range(input_num):
            # index restriction
            if j - i != k - j and i < j < k:
                # String restriction
                if input_list[i] != input_list[j] and input_list[i] != input_list[k] and input_list[j] != input_list[k]:
                    total += 1
print(total)

Submission status: Time Limit Exceeded

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

cnt = Counter(S)

ans = cnt['R'] * cnt['G'] * cnt['B']

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

print(ans)

Submission status: Time Limit Exceeded

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

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(input())
R =[]
G =[]
B =[]
for k in range(N):
    s = a[k]
    if s == 'R':
        R.append(k)
    elif s == 'G':
        G.append(k)
    else:
        B.append(k)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

rgb = S.count('R')*S.count('G')*S.count('B')

ans = 0
for comb in list(itertools.combinations(range(N), 2)):
  i, j = comb
  
  if (j*2 -i) < N:
    if (S[i] != S[j]) & (S[j] != S[j*2 -i]) & (S[j*2 -i] != S[i]) :
      ans += 1

print(rgb - ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ans = 0
for comb in list(itertools.combinations(range(N), 3)):
  i, j, k = comb
  
  if (S[i] != S[j]) & (S[j] != S[k]) & (S[k] != S[i]) :
    if (j + j - i - k) != 0 :
      ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

# R,G,Bのそれぞれのインデックスをリストで取得する
r_index = []
g_index = []
b_index = []
for i in range(len(S)):
  if S[i]=='R':
    r_index.append(i)
  elif S[i]=='G':
    g_index.append(i)
  else:
    b_index.append(i)

#　全組み合わせ(制約1)
rgb_num = len(r_index) * len(g_index) * len(b_index)

# 組み合わせを全探索して、制約2に当てはまるものをカウントする(i, j, k)
ck = 0
for r in r_index:
  for g in g_index:

    # x < y
    if r < g :
      x, y = r, g
    else:
      x,y = g, r
    
    # (i, j, k) = (x, y, b) or (x, b, y) or (b, x, y)
    # (x, y, b)
    i = x
    j = y
    if (j*2 -i) in b_index:
      ck += 1

    # (x, b, y)
    i = x
    k = y
    if (i + k) / 2 in b_index:
      ck += 1

    # (b, x, y)
    j = x
    k = y
    if (j*2 -k) in b_index:
      ck += 1

print(rgb_num - ck)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
s = input()
res = 0
# for i in range(0, n):
#     for j in range(i+1,n):
#         if s[i] != s[j]:
#             for k in range(j+1,n):
#                 if j-i!=k-j and s[i] != s[k] and s[j] != s[k]:
#                     res += 1
#
# print(res)
num = collections.Counter(s)
# print(num)
num_r = num["R"]
num_g = num["G"]
num_b = n - num_r - num_g
count_2 = 0
for x in range(1,int(n/2)+1):
    for i in range(n-2):
        # print(x)
        # print(i)
        # print()
        if (i + 2 * x < n and s[i] != s[i + x] and s[i + x] != s[i + 2 * x] and s[i] != s[i + 2 * x] ):
            count_2 += 1
            # print(x)
            # print(i)
            # print()

res = num_r * num_g * num_b - count_2
# print(num_r)
# print(num_g)
# print(num_b)
print(res)