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:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

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

    R = set([])
    G = set([])
    B = set([])

    for i in range(N):
        temp = S[i]
        if temp == 'R':
            R.add(i)
        elif temp == 'G':
            G.add(i)
        else:
            B.add(i)

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

    count = 0

    for r in R:
        for g in G:
            for b in B:
                l = [r,g,b]
                l.sort()
                if l[1]-l[0] == l[2]-l[1]:
                    count += 1

    res -= count
    print(res)




main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_list =[]
g_list =[]
b_list =[]

for i in range(1,n+1):
    if s[i-1] == 'R':
        r_list.append(i)
    elif s[i-1] == 'G':
        g_list.append(i)
    elif s[i-1] == 'B':
        b_list.append(i)

#print(r_list, g_list, b_list)

i=0
for r_el in r_list:
    for g_el in g_list:
        for b_el in b_list:
            [e1, e2, e3] = sorted([r_el, g_el, b_el])
            if (e2 - e1) != (e3 - e2):
                i += 1
print(i)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
word_len = int(input())
word = str(input())
R_list = []
G_list = []
B_list = []
R_array = np.array(R_list)
G_array = np.array(G_list)
B_array = np.array(B_list)

for i in range(word_len):
    if word[i] == "R":
        R_array = np.append(R_array, i)
    if word[i] == "G":
        G_array = np.append(G_array, i)
    if word[i] == "B":
        B_array = np.append(B_array, i)
counter = 0
for r in R_array:
    for g in G_array:
        for b in B_array:
            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:
import copy

def main():
    N=int(input())
    S=input()
    s="A"*4001
    S=S+s
    L=["R","G","B"]
    ans=0

    for i in range(N-2):
        l = copy.deepcopy(L)

        l.remove(S[i])
        for j in range(i+1,N-1):
            l2 = copy.deepcopy(l)
            if S[j] in l2:
                l2.remove(S[j])

                cnt = S[j+1:].count(l2[0])
                ans+=cnt

                c=j+j-i
                #if c>N-1:continue
                if S[c] == l2[0]:
                    ans-=1
          
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        set_ij = set([S[i], S[j]])
        if len(set_ij) == 2:
            sk = ({"R", "G", "B"} - set_ij).pop()
            if 2*j - i < N and S[2*j - i] == sk:
                ans += S[j+1:].count(sk) - 1
            else:
                ans += S[j+1:].count(sk)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s.replace("R","1")
s = s.replace("G","2")
s = s.replace("B","9")
s2 = list(map((lambda x:int(x)), list(s)))

cnt = 0
for i in range(0, n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if(s2[i]+s2[j]+s2[k] == 12):
                if not (j-i) == (k-j):
                    cnt += 1
print(cnt)

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

Submission status: Time Limit Exceeded

Solution code:
import collections

def other(a, b):
    if(a!="R" and b!="R"):
        return "R"
    if(a!="G" and b!="G"):
        return "G"
    if(a!="B" and b!="B"):
        return "B"


N = int(input())
S = str(input())
counterList = []

for i in range(N):
    counterList.append(collections.Counter(S[i:]))

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

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,m in enumerate(s):
    for j,M in enumerate(s[:i]):
        if m!=M:
            k=3-"RGB".index(m)-"RGB".index(s[j])
            a+=c[-1][k]-c[i][k]
            if 2*i-j<=n-1:
                if s[2*i-j]=="RGB"[k]:
                    a-=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys 
try:
	sys.stdin = open('input.txt', 'r') 
	sys.stdout = open('output.txt', 'w')
except:
	pass
n = int(input())
s = input()
red = []
blue = []
green = []
for i in range(n):
	if s[i] == 'R':
		red.append(i)
	elif s[i] == "G":
		green.append(i)
	else:
		blue.append(i)
temp = [(len(red),'R'), (len(blue),'B'), (len(green),'G')]
temp.sort()

if temp[0][1] == 'R':
	first = list(red)
elif temp[0][1] == 'G':
	first = list(green)
else:
	first = list(blue)

if temp[1][1] == 'R':
	second = list(red)
elif temp[1][1] == 'G':
	second = list(green)
else:
	second = list(blue)

if temp[2][1] == 'R':
	third = list(red)
elif temp[2][1] == 'G':
	third = list(green)
else:
	third = list(blue)

final = []
for i in first:
	for j in second:
		gg = max(i,j)
		ss = min(i,j)
		diff = gg-ss
		check = []
		if (gg+ss)%2==0:
			check.append((gg+ss)//2)
		if gg+diff < n:
			check.append(gg+diff)
		if ss-diff >= 0:
			check.append(ss-diff)
		final.append(check)
# print(final)
# print(first,second,third)
ll = len(third)
d_third = {i:1 for i in third}
		
ans = 0

for item in final:
	sub = 0
	for i in item:
		if i in d_third:
			sub += 1
	ans += ll - sub
print(ans)

Submission status: Time Limit Exceeded

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

diff_dic = {}
diff_dic[('R', 'G')] = []
diff_dic[('R', 'B')] = []
diff_dic[('G', 'R')] = []
diff_dic[('G', 'B')] = []
diff_dic[('B', 'G')] = []
diff_dic[('B', 'R')] = []

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0
for i in range(N):
    if S[i] == 'R':
        r_cnt += 1
    elif S[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i + 1, N):
        if S[i] == 'R':
            if S[j] != 'R':
                diff_dic[('R', S[j])].append((j, j - i))

        elif S[i] == 'G':
            if S[j] != 'G':
                diff_dic[('G', S[j])].append((j, j - i))

        elif S[i] == 'B':
            if S[j] != 'B':
                diff_dic[('B', S[j])].append((j, j - i))


cnt = 0

for t in [('R', 'G'), ('G', 'R')]:
    for j, d in diff_dic[t]:
        if j < N - 1:
            if b_cnt - B_lst[j] > 0:
                cnt += b_cnt - B_lst[j]
                if j + d < N and S[j + d] == 'B':
                    cnt -= 1

for t in [('G', 'B'), ('B', 'G')]:
    for j, d in diff_dic[t]:
        if j < N - 1:
            if r_cnt - R_lst[j] > 0:
                cnt += r_cnt - R_lst[j]
                if j + d < N and S[j + d] == 'R':
                    cnt -= 1

for t in [('B', 'R'), ('R', 'B')]:
    for j, d in diff_dic[t]:
        if j < N - 1:
            if g_cnt - G_lst[j] > 0:
                cnt += g_cnt - G_lst[j]
                if j + d < N and S[j + d] == 'G':
                    cnt -= 1

print(cnt)

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+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
x = 0
for i in r:
    for j in g:
        for k in b:
            if abs(j-i) != abs(k-j) and abs(i-k) != abs(j-i) and abs(k-i) != abs(j-k):
                x += 1
print(x)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = list()
g = list()
b = list()
for i in range(0, len(s)):
    v = s[i]
    if v == "R":
        r.append(i)
    elif v == "G":
        g.append(i)
    else:
        b.append(i)
c = 0
for rr in r:
    for gg in g: 
        for bb in b:
            i, j, k = tuple(sorted((rr, gg, bb)))
            if j - i != k - j:
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=0
if n<=2:
    print(0)
    quit()
import itertools           
for i,j,k in itertools.combinations_with_replacement(range(0,n), 3):
    if j-i!=k-j and s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def input():
    return sys.stdin.readline()


def resolve():
    n = int(input())
    s = input()

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

    print(count)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(0, N-3):
    for j in range(i+1, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            r = color[0]
            ans += S[j+1:].count(r)
            try:
                if S[j+(j-i)] == r:
                    ans -= 1
            except IndexError:
                pass
print(ans)

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+1)
    elif S[i] == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
ans = 0
count = 0
for k in R:
    for j in G:
        for i in B:
            fr = 0
            ab = [k,j,i]
            ab.sort()
            if ab[2] - ab[1] == ab[1] - ab[0]:
                fr += 1

            if fr == 0:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)

for r in R_index:
    for g in G_index:
        for b in B_index:
            l = [r,g,b]
            l.sort()
            if l[1] - l[0] == l[2] -l[1]:
                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(1, n - 1):
        if i >= j: continue
        for k in range(2, n):
            if j >= k: continue
            if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:continue  
            if j - i != k - j: cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n= int(input())
    slis = list(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]

    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB

    for i in lis_R:
        for j in lis_G:
            for k in lis_B:
                if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                    total -= 1
    print(total)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i in range(N):
  if(S[i]=="R"):
    R.append(i)
  elif(S[i]=="G"):
    G.append(i)
  else:
    B.append(i)
ans = 0
if(N<=3):
  print(0)
else:
  for i in range(N):
    if(len(R)==0 or len(G)==0 or len(B)==0):
      break
    else:
      if(S[i]=="R"):
        R.pop(0)
        for j in range(len(G)):
          for k in range(len(B)):
            if(G[j]<B[k]):
              if(G[j]-i != B[k]-G[j]):
                ans += 1
            elif(B[k]<G[j]):
              if(B[k]-i != G[j]-B[k]):
                ans += 1
      elif(S[i]=="G"):
        G.pop(0)
        for j in range(len(R)):
          for k in range(len(B)):
            if(R[j]<B[k]):
              if(R[j]-i != B[k]-R[j]):
                ans += 1
            elif(B[k]<R[j]):
              if(B[k]-i != R[j]-B[k]):
                ans += 1
      else:
        B.pop(0)
        for j in range(len(R)):
          for k in range(len(G)):
            if(R[j]<G[k]):
              if(R[j]-i != G[k]-R[j]):
                ans += 1
            elif(G[k]<R[j]):
              if(G[k]-i != R[j]-G[k]):
                ans += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
n = int(input())
s = input()
s_l = list(s)
cnt = 0
for i in range(n-2):
    for j in range(i,n-1):
        if s[i] != s[j]:
            for k in range(j,n):
                if s[i] != s[k] and s[j] != s[k]:
                    if j-i != k-j:
                        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-2):
    for j in range(2, N):
        if j <= i:
            continue
        for k in range(3, N+1):
            if k <= j:
                continue
            if not(j-i == k-j or S[i-1] == S[j-1] or S[i-1] == S[k-1] or S[k-1] == S[j-1]):
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

sum = 0
R_list = []
G_list = []
B_list = []
for i in range(N):
	if S[i]=='R':
		R_list.append(i)
	elif S[i]=='G':
		G_list.append(i)
	else:
		B_list.append(i)
for x in R_list:
	for y in G_list:
		if (x-y)%2==0:
			tmp_set = {x+(x-y),x-(x-y),y+(x-y),y-(x-y),int((x+y)/2)}
		else:
			tmp_set = {x+(x-y),x-(x-y),y+(x-y),y-(x-y)}
		sum+=len(B_list)-len(set(B_list) & tmp_set)
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
jyogai = 0

for i in range(N-1):
    nS = S[i+1:]
    kouho = []
    for w in ["R", "G", "B"]:
        if S[i] != w:
            kouho.append(nS.count(w))
    temp = i
    while True:
        temp += 1
        temp2 = temp - i
        if i + 2*temp2 > N-1:
            break
        b = [S[i], S[i + temp2], S[i + 2*temp2]]
        if len(set(b)) == 3:
            jyogai += 1

    ans += kouho[0] * kouho[1]

print(ans - jyogai)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            temp = [r[i], g[j], b[k]]
            temp.sort()

            if temp[1]-temp[0] != temp[2]-temp[1]:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Rcnt = 0
Gcnt = 0
Bcnt = 0

excep = 0

for i in S:
    if i == 'R':
        Rcnt += 1
    elif i == 'G':
        Gcnt += 1
    elif i == 'B':
        Bcnt += 1

for i in range(len(S)):
    for j in range(1, len(S)):
        if i+j+j < len(S):
            if S[i] != S[i+j] and S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                # print(S[i], S[i+1], S[i+2])
                excep += 1

RGBcalc = Rcnt * Gcnt * Bcnt

print(RGBcalc - excep)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N=int(input())
K=list(input())

ans = 0
while len(K)>3:
    a = K.pop()
    for i, b in enumerate(K):
        if b != a:
            k = K[i+1:]
    
            for ii,c in enumerate(k):
                    if (c != b) and (c != a) and (ii != (len(k)-1)/2):
                        ans +=1
                 
print(ans)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            for k in range(j + 1, N):
                if (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:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')
 
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
 
N = iin()
S = input()

R = []
G = []
B = []
R_count = [0 for _ in range(N)]
G_count = [0 for _ in range(N)]
B_count = [0 for _ in range(N)]
for si in range(len(S)):
    if si > 0:
        R_count[si] = R_count[si-1]
        G_count[si] = G_count[si-1]
        B_count[si] = B_count[si-1]
    if S[si] == 'R': 
        R.append(si)
        R_count[si] += 1
    elif S[si] == 'G': 
        G.append(si)
        G_count[si] += 1
    else: 
        B.append(si)
        B_count[si] += 1

# OK判定
def isOK(index, key):
    # ここにOKとなる条件を記載
    if index > key: return True
    else: return False

# めぐる式二分探索
def binary_search(key):
    ng = -1
    ok = N

    while abs(ok - ng) > 1:
        mid = (ok+ng) // 2
        if isOK(mid, key): ok = mid
        else: ng = mid
    return right

first = ''
second = ''
first_index = -1
second_index = -1
ans = 0
for i in range(len(S)):
    first = S[i]
    for j in range(i+1, len(S)):
        if first != S[j]:
            second = S[j]
            diff = j - i
            if not(first == 'R' or second == 'R'):
                ans += R_count[N-1] - R_count[j]
                if j+diff < len(S) and S[j+diff] == 'R':
                    ans -= 1
            elif not(first == 'G' or second == 'G'):
                ans += G_count[N-1] - G_count[j]
                if j+diff < len(S) and S[j+diff] == 'G':
                    ans -= 1
            else:
                ans += B_count[N-1] - B_count[j]
                if j+diff < len(S) and S[j+diff] == 'B':
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

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


print(res)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=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"]
a1 = [R,G,B]
l1=list(itertools.product(*a1))
a2 = [R,B,G]
l2=list(itertools.product(*a2))
a3 = [G,R,B]
l3=list(itertools.product(*a3))
a4 = [G,B,R]
l4=list(itertools.product(*a4))
a5 = [B,R,G]
l5=list(itertools.product(*a5))
a6 = [B,G,R]
l6=list(itertools.product(*a6))
ans=[]
for l in [l1,l2,l3,l4,l5,l6]:
  ans += list(filter(lambda i:i[1]-i[0]!=i[2]-i[1] and i[0]<i[1]<i[2],l))
print (len(ans))

Submission status: Time Limit Exceeded

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

print(sum((j-i)!=(k-j) and s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k] for i in range(n) for j in range(i,n) for k in range(j,n)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
S = input()
count = Counter(S)

# print(count)
ans = 0
R = count["R"]
G = count["G"]
B = count["B"]
ans = R * G * B

# for i in range(N - 2):
#     if S[i] == "R":
#         for j in range(i + 1, N - 1):
#             if S[j] == "G":
#                 b_count = Counter(S[j:])["B"]
#                 ans += b_count

# print(R, G, B, ans)

for i in range(N):
    for j in range(N):
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
            ans -= 1
        if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
            ans -= 1
        if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
ans = 0
for i in range(len(S)):    
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
# r, g, b = 0, 0, 0
# while (r < len(R) and g < len(G) and b < len(B)):
#     target = min(R[r], g, b)
#     if (abs(R[r] - g) != abs(g - b) and abs(R[r] - g) != abs(R[r] - b) and abs(g - b) != abs(R[r] - b)):
#         ans += 1
#     if target == R[r]:
#         r += 1
#     if target == g:
#         g += 1
#     if target == b:
#         b += 1
for r in R:
    for g in G:
        for b in B:
            if (abs(r - g) != abs(g - b) and abs(r - g) != abs(r - b) and abs(g - b) != abs(r - b)):
                ans += 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

def main():
    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:
        sj = bisect_left(G_index, i)
        for ij in range(sj, len(G_index)):
            j = G_index[ij]
            sk = bisect_left(B_index, j)
            for ik in range(sk,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(B_index, i)
        for ij in range(sj,len(B_index)):
            j = B_index[ij]
            sj = bisect_left(G_index, j)
            for ik in range(sj,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in G_index:
        sj = bisect_left(B_index, i)
        for ij in range(sj, len(B_index)):
            j = B_index[ij]
            sk = bisect_left(R_index, j)
            for ik in range(sk,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(R_index, i)
        for ij in range(sj,len(R_index)):
            j = R_index[ij]
            sj = bisect_left(B_index, j)
            for ik in range(sj,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in B_index:
        sj = bisect_left(R_index, i)
        for ij in range(sj, len(R_index)):
            j = R_index[ij]
            sk = bisect_left(G_index, j)
            for ik in range(sk,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(G_index, i)
        for ij in range(sj,len(G_index)):
            j = G_index[ij]
            sj = bisect_left(R_index, j)
            for ik in range(sj,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
x = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    if s[i]==s[j]:
      continue
    else:
      if 2*j-i<= n-1 and len({s[i], s[j], s[2*j-i]})==3:
        x += 1
print(a*b*c-x)

Submission status: Time Limit Exceeded

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

d = 0
for k in range(2,N):
    sk = S[k]
    for j in range(k):
        sj = S[j]
        if sk != sj:
            for i in range(j):
                si = S[i]
                if (si != sj) & (si != sk) & (j-i != k-j):
    
                    d += 1

print(d)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
rgb = input()
ans = 0
for i in range(n):
  for j in range(i, n):
    for k in range(j, n):
      if rgb[i] != rgb[j] and rgb[i] != rgb[k] and rgb[j] != rgb[k] and j - i != k - j:
        ans = 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):
            x,y,z=s[i],s[j],s[k]
            if x!=y and y!=z and z!=x and j-i!=k-j:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
S = input()
counter_R = [0]
counter_G = [0]
counter_B = [0]
for s in S:
    counter_R.append(counter_R[-1] + (s == 'R'))
    counter_G.append(counter_G[-1] + (s == 'G'))
    counter_B.append(counter_B[-1] + (s == 'B'))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = [S[i], S[j]]
    if 'R' not in temp: cnt = counter_R
    elif 'G' not in temp: cnt = counter_G
    else: cnt = counter_B
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
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=0
for i in range(len(R)):
	for j in range(len(G)):
		for k in range(len(B)):
			tmp=[R[i],G[j],B[k]]
			tmp=sorted(tmp)
			if (tmp[2]-tmp[1])!=(tmp[1]-tmp[0]):
				count+=1
print(count)