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:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	S = input()
	S = S[:-1]

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

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

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

	tmp = 0
	for i in R:
		toto = bisect_right(G, i)
		if toto < len(G):
			for jj in range(toto,len(G)):
				j = G[jj]
				toto2 = bisect_right(B, j)
				if toto2 < len(B):
					for kk in range(toto2,len(B)):
						k = B[kk]
						if k == j * 2 - i:
							tmp += 1

	for i in R:
		toto = bisect_right(B, i)
		if toto < len(B):
			for jj in range(toto,len(B)):
				j = B[jj]
				toto2 = bisect_right(G, j)
				if toto2 < len(G):
					for kk in range(toto2,len(G)):
						k = G[kk]
						if k == j * 2 - i:
							tmp += 1

	for i in G:
		toto = bisect_right(R, i)
		if toto < len(R):
			for jj in range(toto,len(R)):
				j = R[jj]
				toto2 = bisect_right(B, j)
				if toto2 < len(B):
					for kk in range(toto2,len(B)):
						k = B[kk]
						if k == j * 2 - i:
							tmp += 1

	for i in G:
		toto = bisect_right(B, i)
		if toto < len(B):
			for jj in range(toto,len(B)):
				j = B[jj]
				toto2 = bisect_right(R, j)
				if toto2 < len(R):
					for kk in range(toto2,len(R)):
						k = R[kk]
						if k == j * 2 - i:
							tmp += 1

	for i in B:
		toto = bisect_right(G, i)
		if toto < len(G):
			for jj in range(toto,len(G)):
				j = G[jj]
				toto2 = bisect_right(R, j)
				if toto2 < len(R):
					for kk in range(toto2,len(R)):
						k = R[kk]
						if k == j * 2 - i:
							tmp += 1

	for i in B:
		toto = bisect_right(R, i)
		if toto < len(R):
			for jj in range(toto,len(R)):
				j = R[jj]
				toto2 = bisect_right(G, j)
				if toto2 < len(G):
					for kk in range(toto2,len(G)):
						k = G[kk]
						if k == j * 2 - i:
							tmp += 1

	print(maxs-tmp)
	#print(tmp)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	S = input()
	S = S[:-1]

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

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

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

	tmp = 0
	for i in R:
		for j in G:
			for k in B:
				tmptmp = []
				tmptmp.append(i)
				tmptmp.append(j)
				tmptmp.append(k)
				tmptmp.sort()

				if tmptmp[2] == tmptmp[1] * 2 - tmptmp[0]:
					tmp += 1
				#print(tmptmp)

	print(maxs-tmp)
	#print(tmp)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
array = ["RGB", "RBG", "BGR", "BRG", "GRB", "GBR"]
count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

R = 0
for i in range(len(S)):
    for j in range(i+1,len(S)):
        if S[i] != S[j]:
            SD = S[j+1:2*j-i]
            try: SD += S[2*j-i+1:]
            except: pass
            R += len(SD.replace(S[i],"").replace(S[j],""))
print(R)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
A = (S.count("R"))*(S.count("B"))*(S.count("G"))
for i in range(1,N):
    for j in range(i+1,N+1):
        if j < 2*j - i <= N:
            if S[i-1] != S[j-1] and S[i-1] != S[2*j - i - 1] and S[j-1] != S[2*j - i - 1]:
                A -= 1
print(A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R,G,B = [],[],[]
for i, char in enumerate(S):
    if char == 'R':
        R.append(i+1)
    if char == 'G':
        G.append(i+1)
    if char == 'B':
        B.append(i+1)
        
ans = len(R)*len(G)*len(B)

for j in R:
    for i in range(1,j):
        k = j*2 - i
        if i in G and k in B:
            ans -= 1
        elif i in B and k in G:
            ans -= 1

for j in G:
    for i in range(1,j):
        k = j*2 - i
        if i in R and k in B:
            ans -= 1
        elif i in B and k in R:
            ans -= 1

for j in B:
    for i in range(1,j):
        k = j*2 - i
        if i in G and k in R:
            ans -= 1
        elif i in R and k in G:
            ans -= 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R,G,B = [],[],[]
for i, char in enumerate(S):
    if char == 'R':
        R.append(i+1)
    if char == 'G':
        G.append(i+1)
    if char == 'B':
        B.append(i+1)
        
ans = len(R)*len(G)*len(B)
for i in R:
    for j in G:
        for k in B:
            L = sorted([i,j,k])
            if L[0]+L[2]==2*L[1]:
                ans -= 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()
r = []
b = []
g = []
for i, si in enumerate(s):
    if si == 'R':
        r.append(i + 1)
    elif si == 'G':
        g.append(i + 1)
    elif si == 'B':
        b.append(i + 1)
count = 0
gb = sorted(g + b)
br = sorted(b + r)
rg = sorted(r + g)
for i, si in enumerate(s):
    tmp = None
    if si == 'R':
        tmp = gb
    elif si == 'G':
        tmp = br
    else:
        tmp = rg
    for j in tmp:
        if j < i + 1:
            continue
        tmp2 = None
        # print(si, s[j-1])
        if (si == 'R' and s[j-1] == 'G') or (si == 'G' and s[j-1] == 'R'):
            tmp2 = b
        elif (si == 'R' and s[j-1] == 'B') or (si == 'B' and s[j-1] == 'R'):
            tmp2 = g
        elif (si == 'G' and s[j-1] == 'B') or (si == 'B' and s[j-1] == 'G'):
            tmp2 = r
        for k in tmp2:
            if k < j:
                continue
            if j - (i + 1) != k - j:
                # print(i + 1, j, k)
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

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

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

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import collections
n = int(input())
s = list(input())

c = collections.Counter(s)
total = c['R'] * c['G'] * c['B']
tmp = 0
#print(total)
for i in range(1, n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if i <= k <= n:
            #print(i, j, k)
            if s[i-1] != s[j-1] and s[i-1] != s[k-1] and s[j-1] != s[k-1]:
                tmp += 1
print(total - tmp)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r*g*b
for i in range(1,(n+1)//2):
    j = 0
    while j + 2*i <= n-1:
        if len(set(s[j]+s[j+i]+s[j+2*i])) == 3:
            ans -= 1
        j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r  = s.count("R")
g = s.count("G")
b = s.count("B")

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
counter = {'R':0, 'G':0, 'B':0}
for s in S:
    counter[s] += 1

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

Submission status: Time Limit Exceeded

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

r1=[]
g1=[]
b1=[]
d=0
for i in range(n):
    if s[i]=="R":
        r1.append(i) 
    elif s[i]=="G":
        g1.append(i)
    else:
        b1.append(i)
#print(r1)
#print(g1)
#print(b1)
for i in r1:
    for j in g1:
        a=abs(i-j)
        if max(i,j)+a in b1:
            d=d+1 
        if min(i,j)-a in b1:
            d=d+1
        if a%2==0 and (min(i,j)+a/2) in b1:
#        for k in b1:
#            print(i,j,k)
#            if abs(j-i)!=abs(k-j) and abs(i-j)!=abs(k-i) and abs(i-k)!=abs(j-k):
            d=d+1
b=len(r1)*len(g1)*len(b1)-d


print(b)

Submission status: Time Limit Exceeded

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

ans = 0
for k in range(2,n):
    for j in range(1,k):
        for i in range(0,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()

r1=[]
g1=[]
b1=[]
d=0
for i in range(n):
    if s[i]=="R":
        r1.append(i) 
    elif s[i]=="G":
        g1.append(i)
    else:
        b1.append(i)
#print(r1)
#print(g1)
#print(b1)
for i in r1:
    for j in g1:
        for k in b1:
#            print(i,j,k)
            if abs(j-i)!=abs(k-j) and abs(i-j)!=abs(k-i) and abs(i-k)!=abs(j-k):
                d=d+1

print(d)

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):
  if s[i] =="R":
    s[i]=1
  elif s[i] =="G":
    s[i]=2
  elif s[i] =="B":
    s[i]=4
for i in range(n):
  for j in range(i+1,n):

    k=2*j-i
    if k>= n:
      continue
    if int(s[i])+int(s[j])+int(s[k])==7:
      ans -=1
print (ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
def right(r,g):
    return (max(r,g)+(abs(r-g)))
def left(r,g):
    return (min(r,g)-(abs(r-g)))
def half(r,g):
    return ((r+g)//2)
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
ans = len(r)*len(g)*(n-(len(r)+len(g)))
for i in range(len(r)):
    for j in range(len(g)):
        t = left(r[i],g[j])
        f = right(r[i],g[j])
        h = half(r[i],g[j])
        if f < n:
            if s[f] == "B":
                ans -= 1
        if 0 <= t:
            if s[t] == "B":
                ans -= 1
        if abs(r[i]-g[j])%2 == 0:
            if s[h] == "B":
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
ans = len(r)*len(g)*(n-(len(r)+len(g)))
for i in range(len(r)):
    for j in range(len(g)):
        if max(r[i],g[j])+(abs(r[i]-g[j])) < n:
            if s[max(r[i],g[j])+(abs(r[i]-g[j]))] == "B":
                ans -= 1
        if 0 <= min(r[i],g[j])-(abs(r[i]-g[j])):
            if s[min(r[i],g[j])-(abs(r[i]-g[j]))] == "B":
                ans -= 1
        if abs(r[i]-g[j])%2 == 0:
            if s[(r[i]+g[j])//2] == "B":
                ans -= 1
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)
    if s[i] == "G":
        g.append(i)
    if s[i] == "B":
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in range(len(r)):
    for j in range(len(g)):
        if max(r[i],g[j])+(abs(r[i]-g[j])) < n:
            if s[max(r[i],g[j])+(abs(r[i]-g[j]))] == "B":
                ans -= 1
        if 0 <= min(r[i],g[j])-(abs(r[i]-g[j])):
            if s[min(r[i],g[j])-(abs(r[i]-g[j]))] == "B":
                ans -= 1
        if abs(r[i]-g[j])%2 == 0:
            if s[(r[i]+g[j])//2] == "B":
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
b = []
ans = 0
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    if s[i] == "G":
        g.append(i+1)
    if s[i] == "B":
        b.append(i+1)
for i in range(len(r)):
    for j in range(len(g)):
        ans += len(b)
        if max(r[i],g[j])+(abs(r[i]-g[j])) in b:
            ans -= 1
        if min(r[i],g[j])-(abs(r[i]-g[j])) in b:
            ans -= 1
        if abs(r[i]-g[j])%2 == 0 and (r[i]+g[j])//2 in b:
            ans -= 1
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)
    if s[i] == "G":
        g.append(i+1)
    if s[i] == "B":
        b.append(i+1)
ans = len(r)*len(g)*len(b)
for i in range(len(r)):
    for j in range(len(g)):
        if max(r[i],g[j])+(abs(r[i]-g[j])) in b:
            ans -= 1
        if min(r[i],g[j])-(abs(r[i]-g[j])) in b:
            ans -= 1
        if abs(r[i]-g[j])%2 == 0 and (r[i]+g[j])//2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
b = []
ans = 0
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    if s[i] == "G":
        g.append(i+1)
    if s[i] == "B":
        b.append(i+1)
for i in range(len(r)):
    for j in range(len(g)):
        ans += len(b)
        if max(r[i],g[j])+(abs(r[i]-g[j])) in b:
            ans -= 1
        if min(r[i],g[j])-(abs(r[i]-g[j])) in b:
            ans -= 1
        if abs(r[i]-g[j])%2 == 0 and (r[i]+g[j])//2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R = []
G = []
B = []
Rsize =0
Gsize =0
Bsize =0
for i in range(0,N,1):
    if S[i] == "R":
        R.append(i)
        Rsize+=1
    elif S[i] == "G":
        G.append(i)
        Gsize+=1
    elif S[i] == "B":
        B.append(i)
        Bsize+=1
counter = 0
for i in range(0,Rsize,1):
    for j in range(0,Gsize,1):
        for k in range(0, Bsize, 1):
            tmp = sorted([R[i],G[j],B[k]])
            if tmp[1]-tmp[0] != tmp[2] - tmp[1]:
                counter+=1

print(counter)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    elif s[i] == "B":
        b.append(i) 

cnt = 0
for i in r:
    for j in g:
        for k in b:
            if (i+j)/2==k or (j+k)/2==i or (k+i)/2==j:
                cnt += 1

print( len(r) * len(g) * len(b) - cnt )

Submission status: Time Limit Exceeded

Solution code:
import collections

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

lenR=S.count("R")
lenG=S.count("G")
lenB=S.count("B")

count=0
for i in range(N-2):
    for j in range(i,N-1):

        if (S[i] != S[j]) and (2*j-i<N):
                if (S[i]!=S[2*j-i]) and (S[j]!=S[2*j-i]):
                    count+=1
    
print(lenR*lenG*lenB-count)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

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

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0
r = range(N)
kumis = []

for i in r:
    for j in r:
        if j < i: continue
        dji = 2*j -i
        for k in r:
            if k < j: continue
            if dji == k: continue
            kumis.append(tuple([i, j, k]))

for kumi in kumis:
    i, j, k = kumi
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        ans += 1

# for i in r:
#     for j in r:
#         if j < i: continue
#         for k in r:
#             if k < j: continue
#             if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
#                 kumis.append(tuple([i,j,k]))

# for kumi in kumis:
#     i,j,k = kumi
#     if j-i != k-j:
#         ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0
r = range(N)
kumis = []
for i in r:
    for j in r:
        if j < i: continue
        for k in r:
            if k < j: continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                kumis.append(tuple([i,j,k]))

for kumi in kumis:
    i,j,k = kumi
    if j-i != k-j:
        ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0
r = range(N)
for i in r:
    for j in r:
        if j < i: continue
        if S[i] == S[j]: continue
        dji = j - i
        for k in r:
            if k < i: continue
            if k < j: continue
            if S[i] == S[k]: continue
            if S[j] == S[k]: continue
            dkj = k - j
            if dji == dkj: continue
            ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0
r = range(N)
for i in r:
    for j in r:
        if j == i: break
        if S[i] == S[j]: continue
        dji = j - i
        for k in r:
            if k == i: break
            if k == j: break
            if S[i] == S[k]: continue
            if S[j] == S[k]: continue
            dkj = k - j
            if dji == dkj: continue
            ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
P = 0
JI = 0
RGB = 'R'
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i] != S[j]:
            JI = j-i
            if S[i] != 'R' and S[j] != 'R':
                RGB = 'R'
            elif S[i] != 'G' and S[j] != 'G':
                RGB = 'G'
            else:
                RGB = 'B'
            for k in range(j+1,N):
                if JI != k-j:
                    if S[k] == RGB:
                        P += 1
print(P)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
P = 0
JI = 0
RGB = 'R'
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i] != S[j]:
            JI = j-i
            if S[i] != 'R' and S[j] != 'R':
                RGB = 'R'
            if S[i] != 'G' and S[j] != 'G':
                RGB = 'G'
            if S[i] != 'B' and S[j] != 'B':
                RGB = 'B'
            for k in range(j+1,N):
                if S[k] == RGB and JI != k-j:
                    P += 1
print(P)

Submission status: Time Limit Exceeded

Solution code:
def sort(a,b,c):
    l=[a,b,c]
    l.sort()
    return l

n=int(input())
s=input()
S=list(enumerate(s))
r=[n for n,v in S if v=='R']
g=[n for n,v in S if v=='G']
b=[n for n,v in S if v=='B']
sum=0
for x in g:
    for y in r:
        for z in b:
            i,j,k=sort(x,y,z)
            if k-j!=j-i:
                sum+=1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets
def main():
    _ = int(input())
    S = input().rstrip()
    cnt = 0
    for j, b in enumerate(S[1:-1], 1):
        for i, a in enumerate(S[:j]):
            for k, c in enumerate(S[j + 1 :], j + 1):
                if a == b or a == c or b == c:
                    continue
                elif j - i == k - j:
                    continue
                cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
g=[]
b=[]
r=[]
for i in range(n):
  if s[i] == 'G':
    g.append(i)
  elif s[i]=='B':
    b.append(i)
  else:
    r.append(i)
for x in g:
  for y in b:
    for z in r:
      test=sorted([x,y,z])
      if test[1]-test[0]==test[2]-test[1]:
        continue
      else:
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
s2=[]
for i in s:
  if i == 'G':
    s2.append(2)
  elif i=='B':
    s2.append(3)
  else:
    s2.append(5)
for k in range(2,n):
  for j in range(1,k):
    for i in range(j):
      if j-i == k-j:
        continue
      elif s2[i]+s2[j]+s2[k]==10:
        count+=1
print(count)

Submission status: Time Limit Exceeded

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