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:
#!/usr/bin/python3.6

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

ans = 0
for i in range(N-2):
    r,g = 0,0
    if S[i] == "R":
        R,G = "GB"
    elif S[i] == "G":
        R,G = "RB"
    else:
        R,G = "RG"
    for j in range(i+1,N):
        if S[j] == R:
            r += 1
            ans += g
        elif S[j] == G:
            g += 1
            ans += r

for i in range(N-2):
    for k in range(i+1,N):
        if (k-i)%2 == 0:
            j = (k+i)//2
            if 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:
#!python3.8
# -*- coding: utf-8 -*-
# abc162/abc162_d
import sys
from itertools import product

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    S = i2s()
    dic = {'R': [], 'G': [], 'B': []}
    for i, s in enumerate(S):
        dic[s].append(i)
    r = 0
    for t in product(*dic.values()):
        (i, j, k) = sorted(t)
        if j - i != k - j:
            r += 1
    print(r)
    return

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r_idx = set()
g_idx = set()
b_idx = set()

for i, s in enumerate(S):
    if s == "R":
        r_idx.add(i)
    elif s == "G":
        g_idx.add(i)
    elif s == "B":
        b_idx.add(i)

count = 0
if r_idx != set() and g_idx != set() and b_idx != set():
    for r in r_idx:
        for g in g_idx:
            for b in b_idx:
                if abs(g-r) != abs(g-b) and abs(r-b) != abs(g-b) and abs(r-g) != abs(r-b):
                    count += 1
                    
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()

if len(set(S)) == 1 or N < 3:
    print(0)
    sys.exit()

ans = []
for i in range(N - 2):
    a = S[i]
    for j in range(i + 1, N - 1):
        b = S[j]
        if a == b:
            continue
        jtoi = j - i
        for k in range(j + 1, N):
            c = S[k]
            if a == c or b == c:
                continue
            ktoj = k - j
            if jtoi != ktoj:
                ans.append(None)

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
z = 0
lnr = [i for i, x in enumerate(s) if x == 'R']
lng = [i for i, x in enumerate(s) if x == 'G']
lnb = [i for i in range(n)]
lnb = list(set(lnb) - set(lng) - set(lnr))
for i in lnr:
    for j in lnb:
        a = max(i, j)
        b = min(i, j)
        if a + a - b in lng:
            z += 1
        if (a - b) % 2 == 0:
            if b + ((a - b)//2) in lng:
                z += 1
        if b * 2 - a >=0:
            if b - (a - b) in lng:
                z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Time Limit Exceeded

Solution code:
import bisect


def main():
    N = int(input())
    S = input()
    dict = {"R": [], "G": [], "B": []}
    ans = 0
    for i in range(N):
        dict[S[i]].append(i)
    l = {"R": len(dict["R"]), "G": len(dict["G"]), "B": len(dict["B"])}
    name = [["R", "G", "B"], ["R", "B", "G"], ["G", "R", "B"], ["G", "B", "R"], ["B", "R", "G"], ["B", "G", "R"]]
    for i in name:
        a, b, c = i
        for i in dict[a]:
            p = bisect.bisect_left(dict[b], i + 1)
            if p >= l[b]:
                continue
            for j in dict[b][p:]:
                q = bisect.bisect_left(dict[c], j + 1)
                for k in range(q,l[c],1):
                    if dict[c][k] > j and (j - i) != (dict[c][k] - j):
                        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_li = list(s)
count = 0
for i in range(len(s_li)):
  for j in range(len(s_li)):
    for k in range(len(s_li)):
      if i < j < k <= n-1:
        if s_li[i] != s_li[j] and s_li[i] != s_li[k] and s_li[j] != s_li[k]:
          if j - i != k -j:
            count += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

N = int(input())
S = [ch for ch in input()]
counter = collections.Counter(S)
if len(counter.keys()) != 3:
    print(0)
    sys.exit()
total = 1
for k, v in counter.items():
    total *= v

for i in range(len(S)):
    l, r = i - 1, i + 1
    while 0 <= l and r < len(S):
        if len(set([S[i], S[l], S[r]])) == 3:
            total -= 1
        l -= 1
        r += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            if S[i]!=S[j]:
                k1=S[j+1:].count(S[i])
                k2=S[j+1:].count(S[j])
                ans=ans+len(S[j+1:])-k1-k2
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

al = 0
for i in range(N):
    c1 = S[i]
    for j in range(i+1,N):
        c2 = S[j]
        if c1 == c2:
            continue
        if c1!='R' and c2!='R':
            c3 = 'R'
        elif c1!='B' and c2!='B':
            c3 = 'B'
        else :
            c3 = 'G'
        al += S[j+1:].count(c3)
        if N-1<2*j-i:
            continue
        tmp = S[2*j-i]
        if tmp != c1 and tmp != c2:
            al -= 1

ans = al
print(ans)

Submission status: Time Limit Exceeded

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

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

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

    if lenB <= lenG <= lenR:
        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

    elif lenB <= lenR <= lenG:
        for i in lis_G:
            for j in lis_R:
                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

    elif lenG <= lenB <= lenR:
        for i in lis_R:
            for j in lis_B:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenG <= lenR <= lenB:
        for i in lis_B:
            for j in lis_R:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenB <= lenG:
        for i in lis_G:
            for j in lis_B:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenG <= lenB:
        for i in lis_B:
            for j in lis_G:
                for k in lis_R:
                    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:
def main():
    n=int(input())
    s=input()
    r = [0]*n
    g = [0]*n
    b = [0]*n
    ans = 0

    dict = {"R":r, "G":g, "B":b}

    for i in range(n):
        dict[s[i]][i] = 1
 
    def rgb_sum(t,i,j):
        for s3 in "RGB":
            if s3 not in t:
                if 2*j-i < n:
                    return sum(dict[s3][j+1:]) - dict[s3][2*j-i]
                else:
                    return sum(dict[s3][j+1:])
                break

    for i in range(n):
        for j in range(i,n):
            if dict[s[i]][j] == 0:                
                s1 = s[i]
                s2 = s[j]
                t = (s1,s2)
                ans += rgb_sum(t,i,j)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(0, N-3):
        si = S[i]
        for a in retA(si):
            idx1 = S.find(a, i)
            while idx1 != -1:
                b = retB(a, si)
                cnt += S[idx1+1:].count(b)
                if 2*idx1 - i < N:
                    if S[2*idx1 - i] == b:
                        cnt -= 1
                idx1 = S.find(a, idx1+1)

    print(cnt)

def retA(a):
    if a == 'R':
        A = ('G', 'B')
    elif a == 'G':
        A = ('R', 'B')
    elif a == 'B':
        A = ('R', 'G')
    return A

def retB(a, x):
    if (a == 'R' or x == 'R') and (a == 'G' or x == 'G'):
        b = 'B'
    elif (a == 'B' or x == 'B') and (a == 'G' or x == 'G'):
        b = 'R'
    elif (a == 'B' or x == 'B') and (a == 'R' or x == 'R'):
        b = 'G'
    return b

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
ans=0
N=int(sys.stdin.readline())
S=sys.stdin.readline()

for i in itertools.combinations(range(N),3):
  if (i[0]+i[2])/2 ==i[1]:
    continue
  L=(S[i[0]],S[i[1]],S[i[2]])
  if len(set(L))!=3:
    continue
  ans+=1
  
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    R = []
    G = []
    B = []
    for i, s in enumerate(S):
        if s == "R":
            R.append(i)
        elif s == "G":
            G.append(i)
        else:
            B.append(i)
    s = 0
    for i in R:
        for j in G:
            for k in B:
                if abs(i-j) != abs(j-k) and abs(i-j) != abs(k-i) and abs(j-k) != abs(k-i):
                    s += 1
    print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
SS = list(S)
n = 0
for i in range(N):
  for j in range(N):
    for k in range(N):
      kk = []
      kk.append(i)
      kk.append(j)
      kk.append(k)
      kk.sort()
      if SS[int(kk[0])] != SS[int(kk[1])] and SS[int(kk[1])] != SS[int(kk[2])] and SS[int(kk[2])] != SS[int(kk[0])]:
        if int(kk[1]) - int(kk[0]) != int(kk[2]) - int(kk[1]):
            n+=1
ans = int(n / 6)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
rgb = input()
ans = 0
for i in range(n):
  for j in range(i + 1, n):
    if rgb[i] == rgb[j]:
      continue
    else:
      for k in range(j + 1, 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:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
s = input()

count = 0

for i in range(0, n-3):
    for j in range(i+1, n-1):
        j_i = j - i

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

print(count)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(N):
    for j in range(N):
        for k in range(N):

            
            if(i<=j and j<=k):
                
                if(S[i] != S[j]):
                    if(S[j] != S[k]):
                       if(S[i] != S[k]):
                           if((j-i) != (k-j)):
                         
                              count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
def solve(n, s):
    rlist, glist, blist = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
    rcount, gcount, bcount = 0, 0, 0
    for i in range(n-1):
        if s[n-1-i] == 'R':
            rcount += 1
        if s[n-1-i] == 'G':
            gcount += 1
        if s[n-1-i] == 'B':
            bcount += 1
        rlist[n-2-i], glist[n-2-i], blist[n-2-i] = rcount, gcount, bcount
        
    ans = 0

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                if s[i] == 'R':
                    if s[j] == 'G':
                        ans += blist[j] - ( 1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    elif s[j] == 'B':
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
                elif s[i] == 'G':
                    if s[j] == 'R':
                        ans += blist[j] - (1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    elif s[j] == 'B':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                elif s[i] == 'B':
                    if s[j] == 'G':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                    elif s[j] == 'R':
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
    
    return ans

def main():
    n = int(input())
    s = input()
    print(solve(n, s))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    # su = 0
    # for i, si in enumerate(S):
    #     for j, sj in enumerate(S[i+1:]):
    #         if si == sj:
    #             continue
    #         for k, sk in enumerate(S[i+j+2:]):
    #             # print(si, sj, sk)
    #             # print(i+1, j+i+2, k+j+i+2)
    #             if j == k:
    #                 continue
    #             elif sj != sk and si != sk:
    #                 su += 1
    #                 # print('gets')
    # print(su)

    su = 0
    for i, si in enumerate(S):
        for j, sj in enumerate(S[i+1:]):
            if si == sj:
                continue
            tmps = S[i+j+2:]
            # print(si, sj, tmps)
            if len(tmps) <= j:
                pass
            else:
                if tmps[j] in [sj, si]:
                    pass
                else:
                    su -= 1

                    # try:
                    #     if tmps[j] in [si, sj]:
                    #         continue
                    # except:
                    #     continue
            tmps = [s for s in tmps if s not in [si, sj]]
            su += len(tmps)

            # for k, sk in enumerate(tmps):
            #     # print(si, sj, sk)
            #     # print(i+1, j+i+2, k+j+i+2)
            #     if j == k:
            #         continue
            #     elif sj != sk and si != sk:
            #         su += 1
            #         # print('gets')
    print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
 
R = []
G = []
B = []
for i in range(n):
  if s[i] == "R":
    R.append(i+1)
  elif s[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)
 
  
ans = 0
 
for i in range(len(R)):
  for j in range(len(G)):
    temp = 0
    if 2*R[i] - G[j] > 0 and 2*R[i] - G[j] <= n:
      if 2*R[i] - G[j] in B:
        temp += 1
    if 2*G[j] - R[i] > 0 and 2*G[j] - R[i] <= n:
      if 2*G[j] - R[i] in B:
        temp += 1
    if (R[i]+G[j])%2 == 0:
      if (R[i]+G[j])/2 in B:
        temp += 1
    ans += len(B)- temp
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
s = input()
ans = 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[j] != s[k] and s[k] != s[i] and j - i != k - j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
sn = len(s)

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)

for i in range(1, sn):
    if s[i] == "R":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)

for i in range(1, sn):
    if s[i] == "G":
        g.append(g[-1] + 1)
    else:
        g.append(g[-1])

b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

for i in range(1, sn):
    if s[i] == "B":
        b.append(b[-1] + 1)
    else:
        b.append(b[-1])


for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if s[i] != s[j]:
            tmp = dict()
            tmp[s[i]] = 0
            tmp[s[j]] = 0
            target_idx = j + (j - i)

            if tmp.get("B", 1) == 1:
                ans += b[-1] - b[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "B":
                    ans -= 1
            elif tmp.get("G", 1) == 1:
                ans += g[-1] - g[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += r[-1] - r[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "R":
                    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+1,n):
        for k in range(j+1,n+1):
            if 2*j-k-i==0:
                continue
            else:
                if((s[i-1]!=s[j-1])&(s[j-1]!=s[k-1])&(s[i-1]!=s[k-1])):
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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


def f(p):
    _p = [p[0], p[1], p[2]]
    _p.sort()
    return _p[1] - _p[0] != _p[2] - _p[1]


def run():
    n = int(input())
    s = input()[:n]

    r_list = [i for i, x in enumerate(s, start=1) if x == 'R']
    g_list = [i for i, x in enumerate(s, start=1) if x == 'G']
    b_list = [i for i, x in enumerate(s, start=1) if x == 'B']

    v = list(filter(f, itertools.product(r_list, g_list, b_list)))
    print(len(v))


if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from itertools import combinations

N = int(input())
S = np.array(list(input()))

i=0
for c in combinations(range(N),3):
    if len(set(np.diff(sorted(c)))) == 1:
        continue
    if len(set([S[c[0]],S[c[1]],S[c[2]]])) != 3:
        continue
    i+=1
print(i)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
S = 'D' + input()

rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1

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


dict={'R':0, 'G':1, 'B':2}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = 0
for i in range(2,N):
    search = dic[S[i]]
    ret += (rgb_count[1][search[0]] - rgb_count[i][search[0]]) * rgb_count[i+1][search[1]]
    ret += (rgb_count[1][search[1]] - rgb_count[i][search[1]]) * rgb_count[i+1][search[0]]
    ran = min([i-1, N-i])

    if ran < 1: continue
    for k in range(1, ran+1):
        if set([S[i-k], S[i], S[i+k]]) == set(['G', 'B', 'R']):
            ret -= 1

print(ret)

Submission status: Time Limit Exceeded

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

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

    Rnum = [0 for _ in range(N)]  # Rnum[i] := i以降の右側に何個Rがあるか？（i含む）
    Gnum = [0 for _ in range(N)]
    Bnum = [0 for _ in range(N)]

    if S[N-1] == "R":
        Rnum[N-1] = 1
    elif S[N-1] == "G":
        Gnum[N-1] = 1
    else:
        Bnum[N-1] = 1
    for i in range(N-2, -1, -1):
        if S[i] == "R":
            Rnum[i] = Rnum[i+1] + 1
            Gnum[i] = Gnum[i+1]
            Bnum[i] = Bnum[i+1]
        elif S[i] == "G":
            Rnum[i] = Rnum[i+1]
            Gnum[i] = Gnum[i+1] + 1
            Bnum[i] = Bnum[i+1]
        else:
            Rnum[i] = Rnum[i+1]
            Gnum[i] = Gnum[i+1]
            Bnum[i] = Bnum[i+1] + 1

    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i]=="R" and S[j]=="G":
                ans += Bnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "B":
                        ans -= 1
            elif S[i]=="R" and S[j]=="B":
                ans += Gnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "G":
                        ans -= 1
            elif S[i]=="G" and S[j]=="R":
                ans += Bnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "B":
                        ans -= 1
            elif S[i]=="G" and S[j]=="B":
                ans += Rnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "R":
                        ans -= 1
            elif S[i]=="B" and S[j]=="R":
                ans += Gnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "G":
                        ans -= 1
            elif S[i]=="B" and S[j]=="G":
                ans += Rnum[j+1]
                if j+(j-i)<N:
                    if S[j+(j-i)] == "R":
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
num=[[i,j,k] for i,j,k in itertools.combinations(range(n),3) if (j-i)!=(k-j)]

s=input()
ans=0
for i,j,k in num:
    if 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()

ans = 0
rgb_idx = {
    'R': [],
    'G': [],
    'B': [],
}
for i in range(n):
    if s[i] == 'R':
        rgb_idx['R'].append(i)
    elif s[i] == 'G':
        rgb_idx['G'].append(i)
    else:
        rgb_idx['B'].append(i)

for i in range(len(rgb_idx['R'])):
    for j in range(len(rgb_idx['G'])):
        ans += len(rgb_idx['B'])
        rnum = rgb_idx['R'][i]
        gnum = rgb_idx['G'][j]
        if gnum-rnum>0:
            g = gnum
            l = rnum
        else:
            g = rnum
            l = gnum
        if g+abs(gnum-rnum) in rgb_idx['B']:
            ans -= 1
        if l-abs(gnum-rnum) in rgb_idx['B']:
            ans -= 1
        if (g+l)/2 in rgb_idx['B']:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

if __name__ == "__main__":
	N = int(input())
	S = tuple(input())

	sum = 0

	for i,j,k in tuple(combinations(range(N),3)):
		if (j - i) != (k - j):
			S_i = S[i]; S_j = S[j]; S_k = S[k]
			if (S_i != S_j) and (S_i != S_k) and (S_j != S_k):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c
co = 0
r = range(1, int(input())+1)
s = "0" + input()
for i, j, k in c(r, 3):
  if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] and j-i != k-j:
    co += 1
print(co)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

i = [i for i in range(n)]

summ = 0
for i, j, k in itertools.combinations(i, 3):
    if all([s[i] != s[j], s[i] != s[k], s[j] != s[k], j - i != k - j]):
        summ += 1

print(summ)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = []
    g = []
    b = []
    for ind, c in enumerate(s):
        if c == 'R':
            r.append(ind)
        elif c == 'G':
            g.append(ind)
        else:
            b.append(ind)
    ans = 0

    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
n = int(input())
s = input()

r_acc = list(accumulate([s[i] == 'R' for i in range(n)], initial=0))
g_acc = list(accumulate([s[i] == 'G' for i in range(n)], initial=0))
b_acc = list(accumulate([s[i] == 'B' for i in range(n)], initial=0))

ans = 0
all = {'R', 'G', 'B'}
for i in range(n):
    for j in range(i + 1, n):
        si = s[i]
        sj = s[j]
        if si == sj:
            continue
        if (si == 'R' and sj == 'G') or (si == 'G' and sj == 'R'):
            ans += b_acc[n] - b_acc[j]
            if n >= j + j - i + 1:
                ans -= b_acc[j + j - i + 1] - b_acc[j + j - i]
        elif (si == 'R' and sj == 'B') or (si == 'B' and sj == 'R'):
            ans += g_acc[n] - g_acc[j]
            if n >= j + j - i + 1:
                ans -= g_acc[j + j - i + 1] - g_acc[j + j - i]
        else:
            ans += r_acc[n] - r_acc[j]
            if n >= j + j - i + 1:
                ans -= r_acc[j + j - i + 1] - r_acc[j + j - i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for v in itertools.combinations(range(N), 3):
    if (v[1] - v[0]) != (v[2] - v[1]):
        if S[v[0]] == S[v[1]] or S[v[1]] == S[v[2]] or S[v[2]] == S[v[0]]:
            continue
        else:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
sum=0
if a>3:
    for r in range(a-2):
        for g in range(r,a-1):
            for d in range(g,a):
                keyw=b[r]+b[g]+b[d]
                if ((g-r)==(d-g))or(r>g)or(g>d):
                    pass
                elif ('R' in keyw)and('G' in keyw)and('B' in keyw):
                    sum+=1
print(sum)