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 collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

s = s.replace("R", "0").replace("G", "1").replace("B", "2")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

count = 0
for i in range(n):
  for j in range(i+1, n):
    if 2*j - i >= n:
      break;
    else:
      a = int(s[i])
      b = int(s[j])
      c = int(s[2*j-i])
      if noteq(a, b, c):
        count += 1
    
ans = s_r * s_g * s_b - count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

s = s.replace("R", "0").replace("G", "1").replace("B", "2")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

count = 0
for i in range(n):
  for j in range(i+1, n):
    if 2*j - i <= n-1:
      a = int(s[i])
      b = int(s[j])
      c = int(s[2*j-i])
      if noteq(a, b, c):
        count += 1
    
ans = s_r * s_g * s_b - count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# ABC162D - RGB Triplets

import numpy as np

def main():
    n=int(input())
    s=np.zeros(n,dtype=np.int64)
    a=input()
    for i in range(n):
        s[i]=ord(a[i])%3
    ans=0
    d=np.zeros((3,n),dtype=np.int64)
    m=np.zeros((3,n),dtype=np.int64)
    l=np.array([[0,2,1],[2,1,0],[1,0,2]])
    #print(s.dtype,d.dtype,m.dtype)

    for i in range(n):
        d[s[i],i]=1
        m[s[i],i]=1

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

    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[s[i]][s[j]],j+1]
            if (j*2-i)<n:
                ans-=d[l[s[i]][s[j]],j*2-i]
    
    print(ans)
        
main()

Submission status: Time Limit Exceeded

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

s_r = s.count("R")
s_g = s.count("G")
s_b = s.count("B")

s = s.replace("R", "0").replace("G", "1").replace("B", "2")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

count = 0
for i in range(n):
  for j in range(i+1, n):
    if 2*j - i <= n-1:
      a = int(s[i])
      b = int(s[j])
      c = int(s[2*j-i])
      if noteq(a, b, c):
        count += 1
    
ans = s_r * s_g * s_b - count
print(ans)

Submission status: Time Limit Exceeded

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

s_r = s.count("R")
s_g = s.count("G")
s_b = s.count("B")

s = s.replace("R", "0")
s = s.replace("G", "1")
s = s.replace("B", "2")

def conditions(a, b):
  c = [-1, -1, -1]
  if (a+b) % 2 == 0:
    c[0] = (a+b) / 2
    c[1] = 2*b - a
    c[2] = 2*a - b
  else:
    c[1] = 2*b - a
    c[2] = 2*a - b
  list = []
  for i in range(3):
    if c[i] >= 0 and c[i] <= n-1:
      list.append(c[i])
  return list

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

count = 0
for i in range(n):
  for j in range(i+1, n):
    if 2*j - i <= n-1:
      a = int(s[i])
      b = int(s[j])
      c = int(s[2*j-i])
      if noteq(a, b, c):
        count += 1
    
ans = s_r * s_g * s_b - count
print(ans)

Submission status: Time Limit Exceeded

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

s_r = s.count("R")
s_g = s.count("G")
s_b = s.count("B")

s = s.replace("R", "0")
s = s.replace("G", "1")
s = s.replace("B", "2")

def conditions(a, b):
  c = [-1, -1, -1]
  if (a+b) % 2 == 0:
    c[0] = (a+b) / 2
    c[1] = 2*b - a
    c[2] = 2*a - b
  else:
    c[1] = 2*b - a
    c[2] = 2*a - b
  list = []
  for i in range(3):
    if c[i] >= 0 and c[i] <= n-1:
      list.append(c[i])
  return list

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

count = 0
for i in range(n):
  for j in range(i+1, n):
    for k in range(j+1, n):
      a = int(s[i])
      b = int(s[j])
      c = int(s[k])
      if k in conditions(i, j) and noteq(a, b, c):
        count += 1
    
ans = s_r * s_g * s_b - count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
import numpy as np

t=0
s_arr=np.array(list(s))
p=np.where(s_arr=='R')
R_ind=p[0]
p=np.where(s_arr=='G')
G_ind=p[0]
p=np.where(s_arr=='B')
B_ind=p[0]
k=len(B_ind)
len(R_ind)*len(G_ind)*len(B_ind)
for i in range(0,len(R_ind)):
    for j in range(0,len(G_ind)):
        r=R_ind[i]
        g=G_ind[j]
        m=abs(r-g)
        rb_diff=abs(B_ind-r)
        gb_diff=abs(B_ind-g)
        t=t+k-sum((rb_diff==m)*1+(gb_diff==m)*1+(rb_diff==gb_diff)*1)
print(t)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
strs = list(input())

index_s = collections.defaultdict(list)

for s in strs:
    index = strs.index(s)
    index_s[s].append(strs.index(s))
    strs[index] = "0"

# print(index_s)


r = index_s["R"]
b = index_s["B"]
g = index_s["G"]

sum = 0

for i in r:
    for j in b:
        for k in g:
            arr = sorted([i,j,k])
            if( arr[1]-arr[0]!=arr[2]-arr[1]):
                sum+=1
                

print(sum)

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:
n = int(input())
s = list(input())
r_pos = []
g_pos = []
b_pos = []

for i in range(n):
    st = s[i]
    if st == "R":
        r_pos.append(i)

    if st == "G":
        g_pos.append(i)

    if st == "B":
        b_pos.append(i)

colors = [r_pos, g_pos, b_pos]
if len(colors[0]) > len(colors[1]):
    colors[0], colors[1] = colors[1], colors[0]
if len(colors[0]) > len(colors[2]):
    colors[0], colors[2] = colors[2], colors[0]
if len(colors[1]) > len(colors[2]):
    colors[1], colors[2] = colors[2], colors[1]

x_num, y_num, z_num = len(colors[0]), len(colors[1]), len(colors[2])

ans = x_num * y_num * z_num
for a in colors[0]:
    for b in colors[1]:
        x, y = min(a, b), max(a, b)

        dif = y - x
        left_pos = x - dif
        right_pos = y + dif
        mid_pos = 4000
        if dif % 2 == 0:
            mid_pos = x + dif // 2

        left = 0
        right = 0
        mid = 0
        if left_pos in colors[2]:
            left = 1
        if right_pos in colors[2]:
            right = 1
        if mid_pos in colors[2]:
            mid = 1

        ans -= left + right + mid

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
bc = n*[0]
gc = n*[0]
rc = n*[0]
for i in range(1, n+1):
    mi = n - i
    if s[n - i] == 'B':
        if i == 1:
            bc[mi] = 1
        else:
            bc[mi] = bc[mi+1] + 1
            rc[mi] = rc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'R':
        if i == 1:
            rc[mi] = 1
        else:
            rc[mi] = rc[mi+1] + 1
            bc[mi] = bc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'G':
        if i ==1:
            gc[mi] = 1
        else:
            gc[mi] = gc[mi+1] + 1
            rc[mi] = rc[mi+1]
            bc[mi] = bc[mi+1]
si = 0
for i in range(0, n - 1):
    
    for j in range(i+1, n-1):
        if s[j] != s[i]:
            d = j-i 
            if j+d < n:
                nc = s[j+d]
                if nc in [s[i], s[j]]:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]
                    elif l[0] == 'G':
                        si += gc[j+1] 
                    else:
                        si += bc[j+1]
                else:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]-1
                    elif l[0] == 'G':
                        si += gc[j+1] -1
                    else:
                        si += bc[j+1] - 1
            else:
                l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                if l[0] == 'R':
                    si+= rc[j+1]
                elif l[0] == 'G':
                    si += gc[j+1] 
                else:
                    si += bc[j+1]
print(si)

Submission status: Time Limit Exceeded

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

# 各文字がどこまでにいくつ含まれるかを累積和で管理
R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
    dic["R"][i]=rp
  elif S[i]=="G":
    gp+=1
    dic["G"][i]=gp
  elif S[i]=="B":
    bp+=1
    dic["B"][i]=bp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    # 最初の2つの文字を固定する
    if S[i] == S[j]:
      continue
    # 探すべき文字を特定
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

RR = []
GG = []
BB = []
for i in range(N):
  if(S[i] == 'R'):
    RR.append(i)
  elif(S[i] == 'G'):
    GG.append(i)
  else:
    BB.append(i)

count = 0

for r in RR:
  for g in GG:
    b1 = 2*r - g
    b2 = 2*g - r
    if((r+g)%2 == 0):
      b3 = int((r+g)/2)
    else:
      b3 = -1

    blen = len(BB)
    if(b1 in BB):
      blen -=1
    if(b2 in BB):
      blen -=1
    if(b3 in BB):
      blen -=1

    count += blen

print(count)

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

Submission status: Time Limit Exceeded

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

_ = int(input())
RGB= list(input())

count = 0
sub = 0

r_list = []
g_list = []
b_list = []
for val in range(0,len(RGB)):
    if "R" == RGB[val]:
        r_list.append(val)
    elif "G" == RGB[val]:
        g_list.append(val)
    elif "B" == RGB[val]:
        b_list.append(val)
rc = 0
bc = 0 
gc = 0
for index, a in enumerate(RGB):
    if a == "R":
        for g in g_list:
            if g > index:
                for b in b_list:
                    if b > index:
                        if b-index == g-b or g-index == b-g:
                            sub +=1
                    else:
                        bc += 1
            else:
                gc += 1
        if len(g_list) != 0:
            bc = int(bc / len(g_list))
        if not gc == 0:
            del g_list[:gc]
        if not bc == 0:
            del b_list[:bc]
        bc = 0
        rc = 0
        gc = 0
        count = count + len(g_list)*len(b_list)
                    
    elif a == "G":
        for b in b_list:
            if b > index:
                for r in r_list:
                    if r > index:
                        if r-index == b-r or b-index == r-b :
                            sub +=1
                    else:
                        rc += 1
            else: 
                bc += 1
        if len(b_list) != 0:
            rc = int(rc / len(b_list))
        if not rc == 0:
            del r_list[:rc]
        if not bc == 0:
            del b_list[:bc]
        bc = 0
        rc = 0
        gc = 0
        count = count + len(b_list)*len(r_list)
        
    elif a == "B":
        for r in r_list:
            if r > index:
                for g in g_list:
                    if g > index:
                        if g-index == r-g or r-index == g-r :
                            sub +=1
                    else:
                        gc += 1
            else:
                rc += 1
        if len(r_list) != 0:
            gc = int(gc / len(r_list))
        if not rc == 0:
            del r_list[:rc]
        if not gc == 0:    
            del g_list[:gc]
        bc = 0
        rc = 0
        gc = 0
        count = count + len(r_list)*len(g_list)

print(count-sub)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate, dropwhile
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    r_idx, g_idx, b_idx = [[i for i, c in enumerate(S) if c == x] for x in 'RGB']
    idxs = {'R': sorted(g_idx + b_idx), 'G': sorted(r_idx + b_idx), 'B': sorted(r_idx + g_idx)}

    ans = 0
    for i in range(N - 2):
        idxs[S[i]] = list(dropwhile(lambda x: i > x, idxs[S[i]]))
        for j in idxs[S[i]]:
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=[0,0,0]
for i in range(n):
    if s[i]=='R':
        cnt[0]+=1
    elif s[i]=='G':
        cnt[1]+=1
    elif s[i]=='B':
        cnt[2]+=1
ans=cnt[0]*cnt[1]*cnt[2]
for i in range(n):
    for j in range(i+1,n):
        if 2*j-i>=n:
          continue
        if len(set([s[i],s[j],s[2*j-i]]))==3:
          ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re

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

rn = s.count('R')
gn = s.count('G')
bn = s.count('B')

count = 0


def r_list_len(r_str):
    i = 0
    m = ''
    l = []
    while True:
        m = re.search(r_str, s[i:])
        if m is None:
            break
        l.append(m.group())
        i += m.start() + 1
    return len(l)

for i in range((n-3)//2+1):
    r1 = 'R{}G{}B'.format('.'*i, '.'*i) 
    r2 = 'R{}B{}G'.format('.'*i, '.'*i) 
    r3 = 'G{}R{}B'.format('.'*i, '.'*i) 
    r4 = 'G{}B{}R'.format('.'*i, '.'*i) 
    r5 = 'B{}R{}G'.format('.'*i, '.'*i) 
    r6 = 'B{}G{}R'.format('.'*i, '.'*i) 

    count += r_list_len(r1)
    count += r_list_len(r2)
    count += r_list_len(r3)
    count += r_list_len(r4)
    count += r_list_len(r5)
    count += r_list_len(r6)

print(rn*gn*bn - count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
S = list(input())

R = S.count("R")
G = S.count("G")
B = S.count("B")

ans = R * G * B
count = 0

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

Submission status: Time Limit Exceeded

Solution code:
# ABC162D - RGB Triplets

def main():
    n=int(input())
    s=input()
    ans=0
    d=[[0]*n for _ in range(3)]
    m=[[0]*n for _ in range(3)]
    l=[[0,2,1],[2,1,0],[1,0,2]]

    for i in range(n):
        d[ord(s[i])%3][i]=1
        m[ord(s[i])%3][i]=1

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

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[ord(s[i])%3][ord(s[j])%3]][j+1]
            if j*2-i<n:
                ans-=d[l[ord(s[i])%3][ord(s[j])%3]][j*2-i]

    print(ans)
        
main()

Submission status: Time Limit Exceeded

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
S = input()
A = [0] * N
for i, s in enumerate(S):
    if s == 'G':
        A[i] = 1
    elif s == 'B':
        A[i] = 2

acc = list2d(3, N, 0)
for i, a in enumerate(A):
    acc[a][i] = 1
for i in range(3):
    acc[i] = list(accumulate(acc[i][::-1]))[::-1]

ans = 0
for i, a1 in enumerate(A):
    for j in range(i+1, N):
        a2 = A[j]
        if a1 == a2:
            continue
        a3 = 3 - a1 - a2
        cnt = acc[a3][j]
        dist = j - i
        if j + dist < N and A[j+dist] == a3:
            cnt -= 1
        ans += cnt
print(ans)

Submission status: Time Limit Exceeded

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

s_r = []
s_g = []
s_b = []

for i in range(n):
  if s[i] == "R":
    s_r.append(i)
  elif s[i] == "G":
    s_g.append(i)
  else:
    s_b.append(i)

def conditions(a, b):
  if (a+b) % 2 == 0:
    x = (a+b) / 2
    y = 2*b - a
    z = 2*a - b
  else:
    x = -1
    y = 2*b - a
    z = 2*a - b
  return (x, y, z)
  
count = 0
for i in range(len(s_r)):
  for j in range(len(s_g)):
    for l in conditions(s_r[i], s_g[j]):
      if l in s_b:
        count += 1
    
ans = len(s_r) * len(s_g) * len(s_b) - count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

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

print(count)

Submission status: Time Limit Exceeded

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

s_r = []
s_g = []
s_b = []

for i in range(n):
  if s[i] == "R":
    s_r.append(i)
  elif s[i] == "G":
    s_g.append(i)
  else:
    s_b.append(i)

def conditions(a, b):
  if (a+b) % 2 == 0:
    x = (a+b) / 2
    y = 2*b - a
    z = 2*a - b
  else:
    x = -1
    y = 2*b - a
    z = 2*a - b
  return (x, y, z)
  
count = 0
for item in s_r:
  for item2 in s_g:
    for i in conditions(item, item2):
      if i in s_b:
        count += 1
    
ans = len(s_r) * len(s_g) * len(s_b) - count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
list_R=[]
list_G=[]
list_B=[]
count=0
for i in range(N):
    if S[i]=="R":
        list_R.append(i)
    if S[i]=="G":
        list_G.append(i)
    if S[i]=="B":
        list_B.append(i)
for i in range(N):
    for j in range(N):
        k=2*j-i
        if i<j and j<k and k<N:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                count=count+1

print(len(list_B)*len(list_G)*len(list_R)-count)

Submission status: Time Limit Exceeded

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

answer = 0

r = 0
g = 0
b = 0
count = 0

for i in S:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  else:
    b += 1
    
for i in range(N-2):
  for j in range(N-i-2):
    j = j + i + 1
    k = j + (j - i)
    if k < N:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        count += 1
else:
  answer = (r * g * b) - count
  print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
r = S.count('R')
g = S.count('G')
b = S.count('B')
target = set('RGB')
result = r*g*b
for i in range(N):
    for j in range(i + 1, N):
        if S[i] != S[j]:
            tmp = (target - set(S[i]+S[j])).pop()
            if 2*j-i < N and S[2 * j - i] == tmp:
                result -= 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
nR = S.count('R')
nG = S.count('G')
nB = S.count('B')
cnt = 0
for i in range(N-1):
  for j in range(i+1, N):
    k = j - i + j
    if k < N:
      L = [S[i], S[j], S[k]]
      if 'R' in L and 'G' in L and 'B' in L:
        cnt += 1
print(nR*nG*nB-cnt)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            k = j + (j - i)
            ans += sums[other][-1] - sums[other][j]
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
 
# 標準入力 <int>
N = int(input())
 
# 標準入力<str>
S = input()
 
ans = 0
 
for i in range(N - 2):
  S2 = []
  for j in range(N - i - 2):
    if S[i] == S[i + j + 1]:
      continue
    if i + ((j + 1) * 2) < N:
      S2 = S[i + j + 2:i + (2 * j) + 2] + S[i + (2 * j) + 3:]
    else:
      S2 = S[i + j + 2:]
    S2 = S2.replace(S[i], "")
    S2 = S2.replace(S[i + j + 1], "")
    ans += len(S2)
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

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

ans = 0

for i in range(N - 2):
  S2 = []
  for j in range(N - i - 2):
    if S[i] == S[i + j + 1]:
      continue
    if i + ((j + 1) * 2) < N:
      S2 = S[i + j + 2:i + (2 * j) + 2] + S[i + (2 * j) + 3:]
    else:
      S2 = S[i + j + 2:]
    S2 = S2.replace(S[i], "")
    S2 = S2.replace(S[i + j + 1], "")
    ans += len(S2)

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

C = [0]*3
for i in range(N):
    c = "RGB".index(S[i])
    C[c]+=1

K = 0
for i in range(N-2):
    for j in range(i,N):
        k = 2*j-i
        if k>=N:break
        if len(set([S[i],S[j],S[k]]))==3:
            K+=1
print(C[0]*C[1]*C[2]-K)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[]
G=[]
B=[]
for n in range(N):
    if S[n]=="R":
        R.append(n)
    elif S[n]=="G":
        G.append(n)
    else:
        B.append(n)

x=0
for r in R:
    for g in G:
        for b in B:
            l=[r,g,b]
            if sum(l)/3 in l:
                pass
            else:
                x+=1

print(x)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

count = collections.Counter(S)

ans = count['R'] * count['G'] * count['B']

for i in range(N):
    for j in range(1, (N - 1) // 2 + 1):
        if i + 2 * j < N:
            if len({S[i], S[i + j], S[i + 2 * j]}) == 3:
                ans -= 1
    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import collections

def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
           continue
       if Tr[i]=="G":
           G.append(i)
           continue
       if Tr[i]=="B":
           B.append(i)
           continue
    cnt=len(R)*len(G)*len(B)
    for i in R:
        for j in G:
            if max(i,j)+abs(i-j) in B:
                cnt-=1
            if min(i,j)-abs(i-j) in B:
                cnt-=1
            if (i+j)%2==0 and (i+j)//2 in B:
                cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
rec = []
ans = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i] != s[j]:
      rec.append([i,j])
for m in range(len(rec)):
  x = rec[m][0]
  y = rec[m][1]
  for k in range(y+1,n):
    if s[x] != s[k] and s[y] != s[k] and y - x != k - y:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
S=[0]*5000
S=list(input())

S_count=Counter(S)
r=S_count["R"]
g=S_count["G"]
b=S_count["B"]

ans=0

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

print(r*g*b-ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
ans = 0
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)

for x in r:
    for y in g:
        for z in b:
            l = [x, y, z]
            l.sort()
            if l[1] - l[0] != l[2] - l[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
from itertools import permutations
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)

ans = len(r)*len(g)*len(b)

for A, B, C in permutations([r,g,b]):
    for i in A:
        for j in B:
            k = 2 * j - i
            if k < N and i < j < k and 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 bisect
N=int(input())
S=list(input())
ans=0
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=len(B)*len(R)*len(G)
B.append(10000)
lenb=len(B)-1
flag=0
for r in R:
    for g in G:
        left=min(r,g)-abs(r-g)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for i in (left,right,middle):
            if i>0:
                a=bisect.bisect_left(B,i)
                if B[a]==i:
                    flag=flag+1
ans=ans-flag
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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