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 sys
#import copy
import numpy as np
#import itertools
#import collections
#from collections import deque
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
#read = sys.stdin.buffer.read

def main():
    # input
    N = int(readline())
    S = np.array(list(readline()))

    R = np.sum(S == 'R')
    G = np.sum(S == 'G')
    B = np.sum(S == 'B')

    ans = R*G*B

    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2 * j - i
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1
    
    print(ans)
    #print('end')


if __name__ == "__main__":
    main()

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]:
            if S[i] != "R" and S[j] != "R":
                cnt += S[j+1:].count("R")
                try:
                    if S[j+j-i] == "R":
                        cnt -= 1
                except IndexError:
                    pass
            elif S[i] != "G" and S[j] != "G":
                cnt += S[j+1:].count("G")
                try:
                    if S[j+j-i] == "G":
                        cnt -= 1
                except IndexError:
                    pass
            else:
                cnt += S[j+1:].count("B")
                try:
                    if S[j+j-i] == "B":
                        cnt -= 1
                except IndexError:
                    pass
                
print(cnt)

Submission status: Time Limit Exceeded

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

rgb = [[], [], []]

for i, c in enumerate(s):
    rgb[(c > 'G') + (c > 'B')].append(i)

comb = len(rgb[0]) * len(rgb[1]) * len(rgb[2])

exclude = 0
for i in rgb[0]:
    for j in rgb[1]:
        ii = min(i, j)
        jj = max(i, j)
        d = jj - ii
        if ii - d >= 0 and ii - d in rgb[2]:
            exclude += 1
        if jj + d < n and jj + d in rgb[2]:
            exclude += 1
        if not d % 2 and ii + d // 2 in rgb[2]:
            exclude += 1

summ = len(rgb[0]) * len(rgb[1]) * len(rgb[2])
print(summ - exclude)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
i_c = j_c = k_c =1
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k_c>j_c and j_c>i_c): 
                if(j_c-i_c==k_c-j_c):
                    pass
                else:
                    ijk.append([i,j,k])
            k_c+=1
        j_c+=1
        k_c = 1
    i_c+=1
    j_c = 1



for i in range(len(ijk)):
    if(S[ijk[i][0]]!= S[ijk[i][1]] and
       S[ijk[i][0]]!= S[ijk[i][2]] and
       S[ijk[i][1]]!= S[ijk[i][2]]):
       f+=1
print(f)

Submission status: Time Limit Exceeded

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

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    except_num = 0
    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2

            if minmin in max_arr:
                except_num += 1
            if maxmax in max_arr:
                except_num += 1
            if exists_central == 0:
                central = (max_i + min_i) / 2
                if central in max_arr:
                    except_num += 1

    ans = len(r_arr) * len(g_arr) * len(b_arr) - except_num
    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

l_r = []
l_g = []
l_b = []
for i in range(N):
  if S[i]=='R':
    l_r.append(i)
  if S[i]=='G':
    l_g.append(i)
  if S[i]=='B':
    l_b.append(i)

ans = len(l_r)*len(l_g)*len(l_b)

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
rgb = set(['R', 'G', 'B'])
ans = c['R']*c['G']*c['B']
for i in range(n-2):
    for j in range(i+1, n-1):
        if n <= 2*j-i:
            continue
        if {s[i], s[j], s[2*j-i]} == rgb:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
S = input()
R, G, B = S.count("R"), S.count("G"), S.count("B")
ans = R * G * B
for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
if __name__ == "__main__":
    N = int(input())
    S = input()

    R_idx = [i for i in range(len(S)) if S[i] == "R"]
    G_idx = [i for i in range(len(S)) if S[i] == "G"]
    B_idx = [i for i in range(len(S)) if S[i] == "B"]
    #print(R_idx)
    #print(G_idx)
    #print(B_idx)
    ans = len(R_idx) * len(G_idx) * len(B_idx)
    for i in R_idx:
        for j in G_idx:
            m = min(i,j)
            M = max(i,j)
            d = M - m
            if m - d in B_idx:
                #print("({},{}), {}".format(i, j, m-d))
                ans -= 1
            if M + d in B_idx:
                #print("({},{}), {}".format(i, j, M+d))
                ans -= 1
            if d %  2 == 0:
                if m + d/2 in B_idx:
                    ans -= 1
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
S = input()
result = 0

resdict = collections.defaultdict(dict)

target = set('RGB')
for i in range(N):
    # print(resdict)
    for j in range(i + 1, N):
        if S[i] != S[j]:
            tmp = (target - set(S[i]+S[j])).pop()
            if j in resdict[tmp]:
                # print('used')
                result += resdict[tmp][j]
            else:
                res = S.count(tmp, j + 1, N)
                resdict[tmp][j] = res
                result += res
            if 2*j-i < N and S[2 * j - i] == tmp:
                result -= 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
rgb = set(['R', 'G', 'B'])
ans = c['R']*c['G']*c['B']
for i in range(n-2):
    for j in range(i+1, n-1):
        x, y = s[i], s[j]
        if x == y:
            continue
        if n <= 2*j-i:
            continue
        z = s[2*j-i]
        if {x, y, z} == rgb:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
	N=int(input())
	S=input()
	count=0
	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)
	for x in r:
		for y in g:
			for z in b:
				L=[x,y,z]
				#print(L)
				L=sorted(L)
				if (L[1]-L[0])!=(L[2]-L[1]):
					count+=1
	print(count)


if __name__=='__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = 0
    r_cnt = s.count('R')
    g_cnt = s.count('G')
    g_cnt = s.count('G')

    for i in range(n):
        for j in range(1, n - i, 1):
            if s[i] != s[i+j]:
                for x in ('R', 'G', 'B'):
                    if x not in (s[i], s[i+j]):
                        tmp = x
                if i+2*j < n and s[i+2*j] == tmp:
                    ans += s[i+j+1:].count(tmp) - 1
                else:
                    ans += s[i+j+1:].count(tmp)

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import deque
N = int(input())
S = input()
Sd = deque(S)
ans = 0
R, G, B = 0, 0, 0
for i in range(N):
    p = Sd.popleft()
    if p == "R":
        R += 1
    elif p == "G":
        G += 1
    else:
        B += 1
ans = R * G * B
for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    except_num = 0
    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2
            central = (max_i + min_i) / 2

            if minmin in max_arr:
                except_num += 1
            if maxmax in max_arr:
                except_num += 1
            if exists_central == 0:
                if central in max_arr:
                    except_num += 1

    ans = len(r_arr) * len(g_arr) * len(b_arr) - except_num
    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(i<j<k):
                if(S[i]==S[j]):
                    pass
                elif(S[i]==S[k]):
                    pass
                elif(S[j]==S[k]):
                    pass
                else:
                    ijk.append([i+1,j+1,k+1])
# print(ijk)
for i in range(len(ijk)):
    if(ijk[i][1]-ijk[i][0] == ijk[i][2]-ijk[i][1]):
        pass
    else:
        f += 1
print(f)

Submission status: Time Limit Exceeded

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

R = [None] * S.count("R")
G = [None] * S.count("G")
B = [None] * S.count("B")

ri = 0
gi = 0
bi = 0
for i in range(N):
    if S[i] == "R":
        R[ri] = i+1
        ri+=1
    elif S[i] == "G":
        G[gi] = i+1
        gi += 1
    else:
        B[bi] = i+1
        bi += 1

c = 0
for r in R:
    for g in G:
        for d in [(r + g) / 2, 2 * r - g, 2 * g - r]:
            if d in B:
                c += 1

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    # min_len={len(r):r,len(g):g,len(b):b}
    # tmp=list(min_len.keys()).sort()
    for i in (r):
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

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):
        if s[i] == s[j]:
            continue
        for k in range(j + 1, n):
            if len(set([s[i], s[j], s[k]])) == 3 and abs(i-j) != abs(j-k):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Rs = 0
Gs = 0
Bs = 0
for i, il in enumerate(S):
    if il == 'R':
        Rs += 1
    if il == 'G':
        Gs += 1
    if il == 'B':
        Bs += 1
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(Rs*Gs*Bs - num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(i<j<k):
                if(S[i]==S[j]):
                    pass
                elif(S[i]==S[k]):
                    pass
                elif(S[j]==S[k]):
                    pass
                else:
                    ijk.append([i+1,j+1,k+1])
# print(ijk)
for i in range(len(ijk)):
    if(ijk[i][1]-ijk[i][0] != ijk[i][2]-ijk[i][1]):
        f+=1
print(f)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = s.count('R') * s.count('G') * s.count('B')

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

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
S = input()
result = 0

resdict = collections.defaultdict(dict)

target = set('RGB')
for i in range(N):
    #print(result)
    for j in range(i + 1, N):
        if S[i] != S[j]:
            # print(resdict)
            if (target - set(S[i]+S[j])).pop() in resdict[S[j]]:
                result += resdict[S[j]][j]
            else:
                tmp = (target - set(S[i]+S[j])).pop()
                res = S.count(tmp, j + 1, N)
                resdict[tmp][j] = res
                result += res
            if 2*j-i < N and S[2 * j - i] == tmp:
                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)]
if n<3:
    result = 0
else:
    for i in range(1, n+1):
        if s[-i]=="R":
            c[i][0] += 1
        elif s[-i]=="G":
            c[i][1] += 1
        else:
            c[i][2] += 1
        for j in range(3):
            c[i][j] += c[i-1][j]
    c = c[::-1]
    s = s+"0"*(n+1)
    result = 0
    for i in range(n-1):
        if s[i]=="R":
            for j in range(i+1, n):
                if s[j]=="G":
                    result += c[j][2] - ("B"==s[2*j-i])
                elif s[j]=="B":
                    result += c[j][1] - ("G"==s[2*j-i])
        if s[i]=="G":
            for j in range(i+1, n):
                if s[j]=="R":
                    result += c[j][2] - ("B"==s[2*j-i])
                elif s[j]=="B":
                    result += c[j][0] - ("R"==s[2*j-i])
        if s[i]=="B":
            for j in range(i+1, n):
                if s[j]=="G":
                    result += c[j][0] - ("R"==s[2*j-i])
                elif s[j]=="R":
                    result += c[j][1] - ("G"==s[2*j-i])
print(result)

Submission status: Time Limit Exceeded

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

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    ans = 0
    max_num = len(max_arr)
    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2
            central = (max_i + min_i) / 2

            ans += max_num

            if minmin in max_arr:
                ans -= 1
            if maxmax in max_arr:
                ans -= 1
            if exists_central == 0:
                if central in max_arr:
                    ans -= 1

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
ans = c['R']*c['G']*c['B']

for i in range(n):
    for j in range(i+1, n):
        x, y = s[i], s[j]
        if x == y:
            continue
        if n <= 2*j-i:
            continue
        z = s[2*j-i]
        if x != z and y != z:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
ans=0
how_r=[0 for i in range(n+1)]
how_g=[0 for i in range(n+1)]
how_b=[0 for i in range(n+1)]
for i in range(n):
    string=s[i]
    if string=="R":
        how_r[i+1]+=1
    elif string=="G":
        how_g[i+1]+=1
    elif string=="B":
        how_b[i+1]+=1
for i in range(n):
    how_r[i+1]+=how_r[i]
    how_g[i+1]+=how_g[i]
    how_b[i+1]+=how_b[i]


for  i in range(n):
    for j in range(i+1,n):
        x=s[i]
        y=s[j]
        if x!=y:
            if "R" not in [x,y]:
                ans+=how_r[j+1]-how_r[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="R":
                        ans-=1
                
            
            elif "G" not in [x,y]:
                ans+=how_g[j+1]-how_g[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="G":
                        ans-=1
                
            elif "B" not in [x,y]:
                ans+=how_b[j+1]-how_b[i]
                if (i+j)%2==0:
                    if s[(i+j)//2]=="B":
                        ans-=1  
print(ans)

Submission status: Time Limit Exceeded

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

Rs = []
Gs = []
Bs = []
for i, il in enumerate(S):
    if il == 'R':
        Rs.append(i)
    if il == 'G':
        Gs.append(i)
    if il == 'B':
        Bs.append(i)
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(len(Rs)*len(Gs)*len(Bs) - num)

Submission status: Time Limit Exceeded

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

rgb = [[], [], []]

for i, c in enumerate(s):
    if c == 'R':
        rgb[0].append(i)
    elif c == 'G':
        rgb[1].append(i)
    else:
        rgb[2].append(i)

comb = len(rgb[0]) * len(rgb[1]) * len(rgb[2])

summ = 0
for i in rgb[0]:
    for j in rgb[1]:
        ii = min(i, j)
        jj = max(i, j)
        d = jj - ii
        if ii - d >= 0 and ii - d in rgb[2]:
            summ += 1
        if jj + d < n and jj + d in rgb[2]:
            summ += 1
        if not d % 2 and ii + d // 2 in rgb[2]:
            summ += 1

print(comb - summ)

Submission status: Time Limit Exceeded

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

cnt=0
r = []
g = []
b = []
for i, x in enumerate(s):
    if x == "R":
        r.append(i)
    elif x == "G":
        g.append(i)
    else:
        b.append(i)

cnt = len(r)*len(g)*len(b)
for d in range(1,n+1):
    for m in range(n):
        if m-d<0 or m+d>=n:
            continue
        a = m-d
        c = m+d
        if s[a]!=s[m] and s[m]!=s[c] and s[a]!=s[c]:
            cnt -= 1
print(cnt)

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 len(set([s[i], s[j], s[k]])) == 3 and abs(i-j) != abs(j-k):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    r_ind = []
    g_ind = []
    b_ind = []
    # print(list(S))
    for i,s in enumerate(list(S)):
        # print(s)
        if s == "R":
            r_ind.append(i)
        elif s == "G":
            g_ind.append(i)
        else:
            b_ind.append(i)
    d = len(r_ind)*len(g_ind)*len(b_ind)
    # print(d)
    temp = 0
    for i in r_ind:
        for j in g_ind:
            for k in b_ind:
                maxid = max(i,j,k)
                minid = min(i,j,k)
                if i+j+k == (maxid+minid)*3/2:
                    temp +=1
    # print(temp)
    ans = d-temp
    print(ans)

main()

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(i, N):
        for k in range(j, N):
            if j - i == k - j:
                continue
            elif len(set([S[i], S[j], S[k]])) == 3:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
ijk = [[i+1,j+1,k+1] for i in range(N) for j in range(N) for k in range(N) 
        if i<j<k 
        if S[i]!=S[j] and
           S[i]!=S[k] and
           S[j]!=S[k]]
# print(ijk)
for i in range(len(ijk)):
    if(ijk[i][1]-ijk[i][0] != ijk[i][2]-ijk[i][1]):
        f+=1
print(f)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R_sum = [0]*N
G_sum = [0]*N
B_sum = [0]*N
rcnt = 0
gcnt = 0
bcnt = 0
cnt = 0
for c in S:
    if c == 'R':
        rcnt += 1
    elif c == 'G':
        gcnt += 1
    elif c == 'B':
        bcnt += 1
    R_sum[cnt] = rcnt
    G_sum[cnt] = gcnt
    B_sum[cnt] = bcnt
    cnt += 1

anssum = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]: continue
        clist = [S[i], S[j]]
        diff_idx = j - i

        if not ('R' in clist):
            anssum += (R_sum[N-1] - R_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'R':
                    anssum -= 1
        elif not ('G' in clist):
            anssum += (G_sum[N-1] - G_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'G':
                    anssum -= 1
        elif not ('B' in clist):
            anssum += (B_sum[N-1] - B_sum[j-1])
            if diff_idx + j < N:
                if S[diff_idx + j] == 'B':
                    anssum -= 1
print(anssum)

Submission status: Time Limit Exceeded

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

point = 0
rList = []
gList = []
bList = []
for value in s:
    point += 1
    if value == "R":
        rList.append(point)
    elif value == "G":
        gList.append(point)
    elif value == "B":
        bList.append(point)

count = 0
for iIndex, iItem in enumerate(rList):
    for jIndex, jItem in enumerate(gList):
        for kIndex, kItem in enumerate(bList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
for iIndex, iItem in enumerate(rList):
    for jIndex, jItem in enumerate(bList):
        for kIndex, kItem in enumerate(gList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
for iIndex, iItem in enumerate(gList):
    for jIndex, jItem in enumerate(bList):
        for kIndex, kItem in enumerate(rList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
for iIndex, iItem in enumerate(gList):
    for jIndex, jItem in enumerate(rList):
        for kIndex, kItem in enumerate(bList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
for iIndex, iItem in enumerate(bList):
    for jIndex, jItem in enumerate(rList):
        for kIndex, kItem in enumerate(gList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
for iIndex, iItem in enumerate(bList):
    for jIndex, jItem in enumerate(gList):
        for kIndex, kItem in enumerate(rList):
            if (jItem - iItem != kItem - jItem) and iItem < jItem < kItem:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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


R = []
B = []
G = []
p = 0
for i in S:
    if(i == "R"):
        R.append(p)
    elif(i == "B"):
        B.append(p)
    elif(i == "G"):
        G.append(p)
    p += 1
sum = len(R) * len(B) * len(G)
for r in R:
    for b in B:
        up = max(r, b)
        dw = min(r, b)
        d = up-dw
        if up + d in G:
            sum -= 1
        if dw - d in G:
            sum -= 1
        if (r+b)/2 in G:
            sum -= 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = [0] * n
g = [0] * n
b = [0] * n
R = 0
G = 0
B = 0
for i in range(n-1, -1, -1):
  r[i] = R
  g[i] = G
  b[i] = B
  if s[i] == 'R':R+=1
  if s[i] == 'G':G+=1
  if s[i] == 'B':B+=1

ans = 0
for i in range(n-2):
  dum = s[i]
  for j in range(i+1, n-1):
    if (dum == 'R' and s[j] == 'G') or (dum == 'G' and s[j] =='R'):
      ans += b[j]
      if 2*j - i < n:
        if s[2*j-i] == 'B': ans -= 1
    elif (dum == 'G' and s[j] == 'B') or (dum == 'B' and s[j] =='G'):
      ans += r[j]
      if 2*j - i < n:
        if s[2*j-i] == 'R': ans -= 1
    elif (dum == 'B' and s[j] == 'R') or (dum== 'R' and s[j] =='B'):
      ans += g[j]
      if 2*j -i < n:
        if s[2*j-i] == 'G': ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            if 2*j-i <= n-1:
                if s[2*j-i] not in(s[i],s[j]):
                    cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = [0] * n
g = [0] * n
b = [0] * n
R = 0
G = 0
B = 0
for i in range(n-1, -1, -1):
  r[i] = R
  g[i] = G
  b[i] = B
  if s[i] == 'R':R+=1
  if s[i] == 'G':G+=1
  if s[i] == 'B':B+=1

ans = 0
for i in range(n-2):
  dum = s[i]
  for j in range(i+1, n-1):
    if (dum == 'R' and s[j] == 'G') or (dum == 'G' and s[j] =='R'):
      ans += b[j]
      if 2*j - i < n:
        if s[2*j-i] == 'B': ans -= 1
    if (dum == 'G' and s[j] == 'B') or (dum == 'B' and s[j] =='G'):
      ans += r[j]
      if 2*j - i < n:
        if s[2*j-i] == 'R': ans -= 1
    if (dum == 'B' and s[j] == 'R') or (dum== 'R' and s[j] =='B'):
      ans += g[j]
      if 2*j -i < n:
        if s[2*j-i] == 'G': ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())  #数字
S = list(input()) #スペース抜きの複数行,１文字ずつ

cou = 0
for i in range(N):
    for j in range(i, N):
        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:
                        cou +=1
print(cou)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
ans = s.count('R') * s.count('G') * s.count('B')

for i in range(n - 1):
    for j in range(i + 1, n):
        k = j + j - i

        if k >= n:
            continue

        if (s[i] != s[j]) & (s[i] != s[k]) & (s[j] != s[k]):
            ans -= 1
            
print(ans)