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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
from sys import stdin
import sys
sys.setrecursionlimit(10000)

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


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

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

#print(mapR, mapG, mapB)

sum = 0
for i in range(len(s)):
    if s[i] == "R":
        map1 = mapG
        map2 = mapB
    elif s[i] == "G":
        map1 = mapR
        map2 = mapB
    else:
        map1 = mapR
        map2 = mapG

    for j in range(len(map1)):
        if map1[j] < i:
            continue
        for k in range(len(map2)):
            if map2[k] < i:
                continue
            jj,kk = sorted((map1[j],map2[k]))
            if (i-jj) != (jj-kk):
#                print("hit",i,map1[j],map2[k], i-map1[j], i-map2[k])
                sum+= 1 
print(sum)

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

print(sum)
'''

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
result = 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 (k - j) != (j - i):
                    result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for a in range(n-2):
    for b in range(1,n-1):
        for c in range(2,n):
            se=set([a,b,c])
            if c>b:
                if b>a:
                    if len(se)==3:
                        if b-a!=c-b:
                            if s[a]!=s[b] and s[b]!=s[c] and s[a]!=s[c]:
                                ans+=1
                                
print(ans)

Submission status: Time Limit Exceeded

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

count = 0

for k in range(N):
    for j in range(k):
        if S[k] == S[j]:
            continue
        for i in range(j):
            if S[i] == S[j] or S[i] == S[k]:
                continue
            if j-i == k-j:
                continue
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = [S[i] for i in range(len(S))]
R_list = []
G_list = []
B_list = []
for i in range(len(s)):
    if s[i] == "R":
        R_list.append(i+1)
    elif s[i] == "G":
        G_list.append(i+1)
    elif s[i] == "B":
        B_list.append(i+1)
ans = 0
for i in range(len(R_list)):
    for j in range(len(G_list)):
        for k in range(len(B_list)):
            list = [R_list[i],G_list[j],B_list[k]]
            list.sort()
            if list[1] - list[0] != list[2] - list[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(1,n+1):
    for j in range(i,n+1):
        for k in range(j,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]:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = II()
S = input()

ans = 0
for i in range(1, N+1):
    for j in range(2, N+1):
        if j < i:
            continue
        for k in range(3, N+1):
            if k < j:
                continue
            if j-i != k-j and S[i-1] != S[j-1] and S[i-1] != S[k-1] and S[k-1] != S[j-1]:
                    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):
            rgb = s[i] + s[j]+ s[k]
            if "R" in rgb and "G" in rgb and "B" in rgb :
                if abs(j-i) != abs(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)
	if s[i] == "G":
		g.append(i)
	if s[i] == "B":
		b.append(i)
count = len(r)*len(g)*len(b)
for i in r:
	for j in g:
		if (max(i,j)+abs(i-j)) in b:
			count -= 1
		if (min(i,j)-abs(i-j)) in b:
			count -= 1
		if abs(i-j)%2 == 0:
			if (i+j)//2 in b:
				count -= 1
print(count)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []

for x in range(n):
  if s[x] == 'R':
    r.append(x)
  elif s[x] == 'G':
    g.append(x)
  elif s[x] == 'B':
    b.append(x)
    
ans = 0
tmp = []

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
r_list = []
g_list = []
b_list = []
r_append = r_list.append
g_append = g_list.append
b_append = b_list.append
cnt = 0
for i, j in enumerate(s):
    if j == 'R':
        r_append(i)
    if j == 'G':
        g_append(i)
    if j == 'B':
        b_append(i)
for r in r_list:
    for g in g_list:
        for b in b_list:
            if not(r * 2 == g + b or g * 2 == r + b or b * 2 == r + g):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_re = S[::-1]

C = [[0, 0, 0] for _ in range(N+1)]

ans = 0

for i in range(N):
    if S_re[i] == 'R':
        C[i+1][0] = C[i][0]+1
        C[i+1][1] = C[i][1]
        C[i+1][2] = C[i][2]
    elif S_re[i] == 'G':
        C[i+1][0] = C[i][0]
        C[i+1][1] = C[i][1]+1
        C[i+1][2] = C[i][2]
    else:
        C[i+1][0] = C[i][0]
        C[i+1][1] = C[i][1]
        C[i+1][2] = C[i][2]+1

C = C[::-1][:-1]

for i in range(N):
    if S[i] == 'R':
        C[i][0] -= 1
    elif S[i] == 'G':
        C[i][1] -= 1
    else:
        C[i][2] -= 1


for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] != S[j]:
            if 'R' not in [S[i], S[j]]:
                ans += C[j][0]
                if j+(j-i) < N and S[j+(j-i)]=='R':
                    ans -= 1
            if 'G' not in [S[i], S[j]]:
                ans += C[j][1]
                if j+(j-i) < N and S[j+(j-i)]=='G':
                    ans -= 1
            if 'B' not in [S[i], S[j]]:
                ans += C[j][2]
                if j+(j-i) < N and S[j+(j-i)]=='B':
                    ans -= 1

        #print(S[i], S[j], ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 整数を一つ入力
N = int(input())

# 文字列を一つ入力
S = input()

res = 0

R = []
G = []
B = []

for i in range(N):
    if S[i] == "R":
        R.append(i)
        continue
    if S[i] == "G":
        G.append(i)
        continue
    B.append(i)

for i in R:
    for j in G:
        for k in B:
            if abs(j-i) == abs(k-j) or abs(k-i) == abs(k-j) or abs(k-i) == abs(i-j):
                continue
            res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = input("")
S = input("")
N = int(N)
X = 0

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

Submission status: Time Limit Exceeded

Solution code:
import re

def main():
    NumString = int(input())
    String = input()
    
    total_ans = 0
    for i in range(NumString):
        RGB = 'RGB'
        for j in range(i+1, NumString):
            if String[i] == String[j]:
                continue

            k_RGB = RGB.replace(String[i], '').replace(String[j], '')
            total_ans += String.count(k_RGB, j+1)
            if (j + (j-i)) < NumString and String[j + (j-i)] == k_RGB:
                total_ans -= 1
    
    print(total_ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
if N <= 3:
    print('0')
else:
    Rlist = []
    Glist = []
    Blist = []
    for i in range(N):
        if S[i] == 'R':
            Rlist.append(i)
        elif S[i] == 'G':
            Glist.append(i)
        else:
            Blist.append(i)
    for i in Rlist:
        for j in Glist:
            for k in Blist:
                listijk = sorted([i,j,k])
                if listijk[1] - listijk[0] != listijk[2] -listijk[1]:
                    count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s= input()
flag = 0
x = range(n)
for k in x:
    for j in range(k):
        for i in range(j):
            if j - i == k-j:
                continue
            if not (s[i] == s[j] or s[i] == s[k] or s[j] == s[k]):
                flag += 1
print(flag)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
ans = 0
for s in S:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

for i in range(len(S)-2):
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    for j in range(len(S)):
        j += 1
        if i + j >= len(S) or i + j + j >= len(S):
            break
        if S[i] == "R":
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

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


rgb_list = ['R', 'G', 'B']
counter = 0

for p in range(n):
    for q in range(n - p):
        a = s[p]
        b = s[p + q]
        if a == b:
            continue
        current_list = [a, b]
        diff_list = list(set(rgb_list) - set(current_list))
        c = diff_list[0]
        nokori = s[p + q:]
        counter += nokori.count(c)
        if q < len(nokori) and nokori[q] == c:
            counter -= 1

print(counter)

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
ans = 0

for i, x in enumerate(S):
  if x == 'R':
    R.append(i)
  elif x == 'G':
    G.append(i)
  else:
    B.append(i)

def calc(a, b, c):
  lis = sorted([a, b, c])
  if lis[1] * 2 != lis[0] + lis[2]:
    return 1
  else:
    return 0
        
lis = [calc(r, g, b) for r in R for g in G for b in B]
print(sum(lis))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

R_index = []
G_index = []
B_index = []

for i, s in enumerate(S):
    if s == 'R':
        R_index.append(i)
    elif s == 'G':
        G_index.append(i)
    elif s == 'B':
        B_index.append(i)

ans = 0
for i in R_index:
    for j in G_index:
        for k in B_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in B_index:
        for k in G_index:
            if i < j < k and j - i != k - j:
                ans += 1

for i in G_index:
    for j in R_index:
        for k in B_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in B_index:
        for k in R_index:
            if i < j < k and j - i != k - j:
                ans += 1

for i in B_index:
    for j in G_index:
        for k in R_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in R_index:
        for k in G_index:
            if i < j < k and j - i != k - j:
                ans += 1

print(ans)

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)
for r in R:
    for g in G:
        for b in B:
            a=r+g+b
            e=max(r,g,b)
            d=min(r,g,b)
            c=a-e-d
            if e-c!=c-d:
                ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np


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

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

tail = np.zeros((n, 3), dtype=int)
cnt_rgb = np.zeros(3, dtype=int)

for i, c in enumerate(reversed(s)):
    j = n - i - 1
    tail[j] = cnt_rgb
    k = rgbi[c]
    cnt_rgb[k] += 1

cnt = 0
rgb = set('RGB')
for i in range(n-2):
    c1 = s[i]
    for j in range(i+1, n):
        if s[j] == c1:
            continue
        
        ck = (rgb - set([c1, s[j]])).pop()
        cki = rgbi[ck]
        
        ck_cnt = tail[j, cki]
        if 2 * j - i < n:
            ck_cnt -= 1 if s[j + j - i] == ck else 0
        cnt += ck_cnt

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def calc(N, S):
    id = {'R': 0, 'G': 1, 'B': 2}
    rgb = [[0, 0, 0] for _ in range(N)]
    rgb[0][id[S[0]]] = 1
    for i in range(1, N):
        for j in [0, 1, 2]:
            rgb[i][j] = rgb[i - 1][j]
            if j == id[S[i]]:
                rgb[i][j] += 1
    result = 0
    for i in range(N - 1, -1, -1):
        X = S[i]
        YZ = (['G', 'B'] if X == 'R' else ['R', 'B'] if X == 'G' else ['R', 'G'])
        j = i - 1
        while j > -1:
            if S[j] in YZ:
                result += rgb[j][3 - id[X] - id[S[j]]]
                if 2 * j - i > -1 and not S[2 * j - i] in [X, S[j]]:
                    result -=1
            j -= 1
    return result


N = int(input())
S = input()
print(calc(N, S))

Submission status: Time Limit Exceeded

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [0 if i=="R" else 1 if i=="G" else 2 for i in input()]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for i in rs:
        for j in reversed(gs):
            if not (i < j):
                break
            for k in reversed(bs):
                if not (i < k):
                    break
                if j < k:
                    if (j - i) != k - j:
                        res += 1
                else:
                    if (k - i) != j - k:
                        res += 1

    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


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

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=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)
        
"""
print(r)
print(g)
print(b)
"""

ans=0
for i in r:
    for j in g:
        for k in b:
            l=[i,j,k]
            x=max(l)
            y=min(l)
            l.remove(x)
            l.remove(y)
            if x-l[0]!=l[0]-y:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(t)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
count_r = s.count("R")
count_g = s.count("G")
count_b = s.count("B")
sum = count_r * count_g * count_b
minus = 0

for i in range(n):
    for j in range(1,(n-i)//2 + 1):
        if i+2*j <= n-1:
            tem = [s[i], s[i+j], s[i+2*j]]
            tem.sort()
            if tem[0] != tem[1] and tem[1] != tem[2]:
                minus += 1
            

print(sum - minus)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [e for e in input()]
cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

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

c = 0

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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 not b-a == c-b:
                    if RGB[b-1] != RGB[c-1] and RGB[c-1] != RGB[a-1]:
                        count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = 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']
sum = 0

for i in r:
  for j in g:
    for k in b:
      d, e, f = sorted([i, j, k])
      if e-d == f-e:
        continue
      sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

    ans = 0
    for ix_i, i in enumerate(S):
        for j in range(ix_i+1, N):
            if i == S[j]:
                continue
            else:
                if (i == "R") and (S[j] == "G"):
                    ans += S[j + 1:].count("B")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "B":
                            ans -= 1
                elif (i == "R") and (S[j] == "B"):
                    ans += S[j + 1:].count("G")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "G":
                            ans -= 1
                elif (i == "G") and (S[j] == "B"):
                    ans += S[j + 1:].count("R")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "R":
                            ans -= 1
                elif (i == "G") and (S[j] == "R"):
                    ans += S[j + 1:].count("B")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "B":
                            ans -= 1
                elif (i == "B") and (S[j] == "R"):
                    ans += S[j + 1:].count("G")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "G":
                            ans -= 1
                elif (i == "B") and (S[j] == "G"):
                    ans += S[j + 1:].count("R")
                    if j+(j-ix_i) < N:
                        if S[j + (j - ix_i)] == "R":
                            ans -= 1
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 標準入力 <int>
N = int(input())

# 標準入力<str>
S = input()

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  s = input()
  r = []
  g = []
  b = []
  for i in range(n):
    if s[i]=="R":
      r.append(i)
    if s[i]=="G":
      g.append(i)
    if s[i]=="B":
      b.append(i)
  ans = 0
  for i in r:
    for j in g:
      for k in b:
        l = sorted([i,j,k])
        if l[1]-l[0] != l[2]-l[1]:
          ans += 1
  print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
str = input()
sum = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      if str[i] == str[j]:
        break
      elif str[i] != str[k] and str[j] != str[k] and j - i != k - j:
        sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
c=[[0,0,0] for _ in range(n+1)]
R=G=B=0
for j,i in enumerate(s):
    if i=="R":
        R+=1
    elif i=="G":
        G+=1
    else:B+=1
    c[j+1]=[R,G,B]
a=0
for i in range(1,n-1):
    for j in range(i):
        k=3-"RGB".index(s[i])-"RGB".index(s[j])
        if s[i]!=s[j]:
            a+=c[-1][k]-c[i][k]
            if 2*i-j<=n-1:
                if s[2*i-j]!=s[i] and s[2*i-j]!=s[j]:
                    a-=1
print(a)

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 k==2*j-i:
                    continue             
                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:
from collections import defaultdict

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 = str(input())
count = 0
i = 0
j = 0
k = 0
for i in range(0,n):
    if i > j  :
        break
    for j in range(0,n):
        if j > k :
            break
        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:
word_len = int(input())
word = str(input())
R_list = []
G_list = []
B_list = []
for i in range(word_len):
    if word[i] == "R":
        R_list.append(i)
    if word[i] == "G":
        G_list.append(i)
    if word[i] == "B":
        B_list.append(i)
counter = 0
for r in R_list:
    for g in G_list:
        for b in B_list:
            v =  [r,g,b]
            v.sort()
            if (v[1]-v[0]) != (v[2]-v[1]):
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

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


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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect
n = int(input())
s = input()
listR = []
listG = []
listB = []
counter = 0
for i in range(len(s)):
    if s[i] == "R":
        listR.append(i)
    elif s[i] == "G":
        listG.append(i)
    elif s[i] == "B":
        listB.append(i)

for bits in itertools.product(listR, listG):
    diff = max(bits) - min(bits)
    remover = 0
    if sum(bits) % 2 == 0:
        mid = int(sum(bits) / 2)
    else:
        mid = 1000000
    if (max(bits) + diff) in listB:
        remover += 1
    if (min(bits) - diff) in listB:
        remover += 1
    if mid in listB:
        remover += 1
    counter += (len(listB) - remover)
print(counter)

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()

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
        ans += s[j + 1:j + di].count(ds) + s[j + di + 1:].count(ds)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
def loop():
    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)

loop()

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

rlist, glist, blist = [], [], []
for i in range(N):
    if S[i] == "R":
        rlist.append(i)
    elif S[i] == "G":
        glist.append(i)
    else:
        blist.append(i)

rnumber = len(rlist)
gnumber = len(glist)
bnumber = N - rnumber - gnumber
total = rnumber * gnumber * bnumber

overcount = 0
for r in rlist:
    for g in glist:
        if (2*r-g) in blist: 
            overcount +=1
        if (2*g-r) in blist:
            overcount +=1
        if (r+g)/2 in blist:
            overcount +=1            
print(total - overcount)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
num = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i] ==S[j]: continue
        SS = S[j:j+(j-i)] + S[j+(j-i)+1:]
        SS = SS.replace(S[i],"").replace(S[j],"")
        num += len(SS)
print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
import itertools
l = list(range(1,n+1))
counter = 0
for v in itertools.combinations(l, 3):
    i = v[0]
    j = v[1]
    k = v[2]
    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]:
            counter+=1
print(counter)