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:
N = int(input())
S = input()

s = 0
for i in range(N):
    s1 = S[i]
    for j in range(i+1,N):
        if s1!=S[j]:
            s2 = S[j]
            for k in range(j+1,N):
                s3 = S[k]
                #print(s1,s2,s3)
                if j-i == k-j:
                    continue
                elif s1 != s2 and s1 != s3 and s2 != s3:
                    s += 1

print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
count=0
for i in range (N):
    for j in range (i+1,N):
        for k in range (j+1,N):
            if j-i ==k-j:
                pass
            else:
                R=S[i]+S[j]+S[k]
                if ("R" in R and "G" in R and "B" in R ):
                    count=count+1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
count_r = s.count("R")
count_g = s.count("G")
count_b = s.count("B")
sum = count_r * count_g * count_b
minus = 0

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

print(sum - minus)

Submission status: Time Limit Exceeded

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

R = [0]*(n)
G = [0]*(n)
B = [0]*(n)

R_count = [0]*n
G_count = [0]*n
B_count = [0]*n

Rc = 0
Gc = 0
Bc = 0

for i in range(1,n+1):
    if s[-i] == "R":
        R[-i] = 1
        Rc += 1
    elif s[-i] == "G":
        G[-i] = 1
        Gc += 1
    else:
        B[-i] = 1
        Bc += 1
        
    R_count[-i] = Rc
    G_count[-i] = Gc
    B_count[-i] = Bc

ans = 0

for i in range(n):
    if s[i] == "R":
        for j in range(i+1,n-1):
            if  s[j] == "G":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if B[j+j-i] == 1:
                    ans -= 1
            if s[j] == "B":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if G[j+j-i] == 1:
                    ans -= 1               

    if s[i] == "B":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += G_count[j+1]
                if j+j-i+1 > n:
                    continue
                if G[j+j-i] == 1:
                    ans -= 1
            if s[j] == "G":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if R[j+j-i] == 1:
                    ans -= 1

    if s[i] == "G":
        for j in range(i+1,n-1):
            if  s[j] == "R":
                ans += B_count[j+1]
                if j+j-i+1 > n:
                    continue
                if B[j+j-i] == 1:
                    ans -= 1
            if s[j] == "B":
                ans += R_count[j+1]
                if j+j-i+1 > n:
                    continue
                if R[j+j-i] == 1:
                    ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
for i in range(1, len(S)+1):
    for j in range(i + 1, len(S)+1):
        for k in range(j + 1, len(S)+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:
N = int(input())
S = input()
ans = 0
for i in range(0, N):
    for j in range(i, N):
        for k in range(j, N):
            if i == j or j == k:
                continue
            elif j-i != k-j and S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def get_result(data):
    # N = int(data[0][0])
    S = data[1][0]
    ans = 0
    r_index = [i for i in range(len(S)) if S[i] == 'R']
    g_index = [i for i in range(len(S)) if S[i] == 'G']
    b_index = [i for i in range(len(S)) if S[i] == 'B']
    for i in r_index:
        for j in g_index:
            for k in b_index:
                s_i, s_j, s_k = sorted([i, j, k])
                if (s_j-s_i) != (s_k-s_j):
                    ans += 1
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n=int(input())
    s=input()
    r=[0]*n
    g = [0] * n
    b = [0] * n
    for i in range(n):
        if s[i]=="R":
            r[i]=1
        elif s[i]=="G":
            g[i]=1
        else:
            b[i]=1
    ans=0
    a=[r,g,b]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    a=[r,b,g]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    a=[g,r,b]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    a=[g,b,r]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    a=[b,r,g]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    a=[b,g,r]
    for i in range(n):
            if a[0][i]==1:
                for j in range(i+1,n):
                    if a[1][j]==1:
                        ans+=sum(a[2][j+1:n])
                        if 2*j-i<=n-1:
                            if a[2][2*j-i]==1:
                               ans-=1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

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

print(d)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

i = 0
while i < 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)
    i += 1

cnt = 0
for r in R:
    for g in G:
        for b in B:
            i, j, k = sorted([r, g, b])
            if j - i != k - j:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

print(c)

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-1):
        if s[i]!=s[j]:
            buf=j-i
            for k in range(j+1,n):
                 if (k-j)!=buf and (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 = str(input())
R = []
G = []
B = []
answer = 0
for i in range(N):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            X = [r, g, b]
            X.sort()
            if int(X[1]) - int(X[0]) != int(X[2]) - int(X[1]):
                answer += 1
print(answer)

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)
  elif s[i] == "G":
    g.append(i)
  else:
    b.append(i)
c = len(r)*len(g)*len(b)
for ri in r:
  for gi in g:
    for bi in b:
      if 3*(max(ri,gi,bi)+min(ri,gi,bi)) == 2*sum([ri,gi,bi]):
        c -= 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
d = {}
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] != s[j]:
            d[(i, j)] = s[i] + s[j]

for k, v in d.items():
    for i in range(k[1]+1, n):
        if s[k[0]] == s[i]:
            continue
        if s[k[1]] == s[i]:
            continue
        if k[1] - k[0] != i - k[1]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            continue
        for k in range(j+1, N):
            if S[i]==S[k] or S[k]==S[j]:
                continue
            if 2*j != k+i:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()
Ans = 0
for i in range(1, 1 + N):
    for j in range(1, 1 + N):
        for k in range(1, 1 + N):
            if i < j and j < k:
                if S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[i-1] != S[k-1]:
                    if (j-i) != (k-j):
                        Ans += 1
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
#from operator import itemgetter
#from collections import Counter
#import heapq
N = int(input())
S = input()
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
#A = [int(input()) for i in range(N)]
#C = [list(map(int, input().split())) for i in range()]
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)
answer = len(R)*len(G)*len(B)
hiku = 0
for r in R:
    for g in G:
        for b in B:
            if b > max(r,g)+abs(r-g):
                break
            if abs(r-g) == abs(g-b) or abs(r-b) == abs(b-g) or abs(b-r) == abs(r-g):
                hiku += 1
answer -= hiku
print(answer)

Submission status: Time Limit Exceeded

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

R_inds = [0] * n
G_inds = [0] * n
B_inds = [0] * n

for i in range(n):
    if s[i] == 'R':
        R_inds[i] = i
    elif s[i] == 'G':
        G_inds[i] = i
    else:
        B_inds[i] = i
R_inds = list(set(R_inds))
G_inds = list(set(G_inds))
B_inds = list(set(B_inds))

if s[0] == 'R':
    G_inds.remove(0)
    B_inds.remove(0)
elif s[0] == 'G':
    R_inds.remove(0)
    B_inds.remove(0)
else:
    R_inds.remove(0)
    G_inds.remove(0)
    
ans = 0
for r in R_inds:
    for g in G_inds:
        big = max(r,g)
        small = min(r,g)
        bigger = big + (big - small)
        smaller = small - (big - small)
        if (big - small) % 2 == 0:
            middle = (big + small) // 2
        else:
            middle = -1
        
        subans = 0
        subans += len(B_inds)
        if smaller in B_inds:
            subans -= 1
        if bigger in B_inds:
            subans -= 1
        if middle in B_inds:
            subans -= 1
        if subans > 0:
            ans += subans

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if (j - i) != (k - j):
                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
r = []
g = []
b = []
for i in range(N):
    if(s[i] == "R"):
        r.append(i)
    elif(s[i] == "G"):
        g.append(i)
    elif(s[i] == "B"):
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            if(2*b[k] == r[i] + g[j] or 2*r[i] == b[k] + g[j] or 2*g[j] == r[i] + b[k]):
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from numba import njit

# @njit
# def regular_intervals(r,g,b):
#     i,j,k = sorted([r,b,g])
#     if k-j == j-i:
#         return True
#     else:
#         return False


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

Rids = []
Gids = []
Bids = []

for i,c in enumerate(S):
    if c == 'R':
        Rids.append(i)
    elif c == 'G':
        Gids.append(i)
    elif c == 'B':
        Bids.append(i)

# print(sum(not regular_intervals(r,g,b) for r in Rids
#               for g in Gids
#               for b in Bids))

# print(sum(abs(r-b) != abs(b-g) and abs(b-g) != abs(g-r) and abs(g-r) != abs(r-b)
#           for r in Rids
#           for g in Gids
#           for b in Bids))

print(sum(len(set((abs(r-b), abs(b-g), abs(g-r)))) == 3
          for r in Rids
          for g in Gids
          for b in Bids))

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()
ss=list(range(n))
cnt=0
from itertools import combinations
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 j-i==k-j :
                continue
            if i<j and j<k:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
S = str(input())
r=0
g=0
b=0
error=0
n=0

if N%2==0:
    n=(N//2)-1
else:
    n=N//2

for i in range(N):
    if S[i]=='R':
        r+=1
    if S[i]=='G':
        g+=1
    if S[i]=='B':
        b+=1

for p in range(n+1):
    for i in range(N-(p*2)):
        if S[i]=='R' and S[i+p]=='G' and S[i+2*p]=='B':
            error+=1
        if S[i]=='R' and S[i+p]=='B' and S[i+2*p]=='G':
            error+=1
        if S[i]=='B' and S[i+p]=='R' and S[i+2*p]=='G':
            error+=1
        if S[i]=='B' and S[i+p]=='G' and S[i+2*p]=='R':
            error+=1
        if S[i]=='G' and S[i+p]=='R' and S[i+2*p]=='B':
            error+=1
        if S[i]=='G' and S[i+p]=='B' and S[i+2*p]=='R':
            error+=1
d=r*g*b

print(d-error)

Submission status: Time Limit Exceeded

Solution code:
def RGBto123(c):
    if c == 'R':
        return 1
    elif c == 'G':
        return 2
    return 4


N = int(input())
S = list(map(RGBto123, input()))

rel = [1 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]+S[k]==7 if i+k!=2*j ]



print(len(rel))

Submission status: Time Limit Exceeded

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

r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

answer = r_count*g_count*b_count


for k in range(1, n):
    for i in range(k):
        extracted = s[i::k]
        if len(extracted) >= 3:
            for l in range(len(extracted)-2):
                temp = extracted[l:l+3]
                if temp.count('R') == 1 and temp.count('G')==1 and temp.count('B'):
                    answer -= 1

print(answer)

Submission status: Time Limit Exceeded

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

r_ind = []
g_ind = []
b_ind = []
base_set = set(["R", "G", "B"])
for i, v in enumerate(s):
  if v == "R":
    r_ind.append(i)
  elif v == "G":
    g_ind.append(i)
  else:
    b_ind.append(i)
char_dict = {
  "R": np.array(r_ind),
  "G": np.array(g_ind),
  "B": np.array(b_ind)
}
res = 0
list_s = list(s)
for i, v in enumerate(list_s):
  rest_s = list_s[i+1:]
  if i + 1 > (len(list_s)-2):
    break
  for j, v2 in enumerate(rest_s):
    cur_index = j + i + 1
    diff = j + 1
    ban_index = i + diff * 2
    if cur_index + 1 > (len(list_s)-1):
      break
    if v == v2:
      continue
    c_set = set([v, v2])
    rest_char = list(base_set - c_set)[0]
    use_ind = char_dict[rest_char]
    flg = (use_ind > cur_index) & (use_ind != ban_index)
    res += flg.sum()
print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import itertools

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

RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
RGB.sort(key=lambda x:len(x))
len_B=len(RGB[2])
max_B=max(RGB[2])
min_B=min(RGB[2])
#for r in RGB[0]:
#    for g in RGB[1]:
for rg in itertools.product(RGB[0],RGB[1]):
    r=rg[0]
    g=rg[1]
    dif = abs(r - g)

    up = max(r,g)+dif
    down = min(r,g)-dif
    if max_B >=up:
        if RGB[2][bisect.bisect_left(RGB[2],up)]==up:
            total-=1
    if min_B <= down and max_B >= down:
        if RGB[2][bisect.bisect_left(RGB[2],down)]==down:
            total-=1
    if dif % 2 == 0:
        center = (r+g)/2
        center_posi=bisect.bisect_left(RGB[2], center)
        if center_posi < len_B:
            if RGB[2][center_posi]==center:
                total-=1
print(total)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(1,n+1):
    for j in range(i+1,n+1):
        if s[i-1] != s[j-1]:
            for k in range(j+1,n+1):
                if s[i-1] != s[k-1]:
                    if s[j-1] != s[k-1]:
                        if k != 2*j - i:
                            cnt += 1
                            # print(i,j,k)

print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0

for x in range(n):
  for y in range(n):
    for z in range(n):
      if not x < y < z:
        # print("SKIP: ", x, y, z)
        continue

      if "".join( sorted([s[x], s[y], s[z]]) ) != "BGR":
        # print("SKIP: ", x, y, z)
        continue

      if y-x == z-y:
        # print("SKIP: ", x, y, z)
        continue

      ans += 1
      # print(x, y, z)
      # print(s[x], s[y], s[z])

print(ans)

Submission status: Time Limit Exceeded

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

def bool2(a,b,c):
    if a-b != b-c:
        return True
    else:
        return False

def main():
    n = int(input())
    s = input()
    ans = 0

    for i in range(n):
        c1 = s[i]
        for j in range(i):
            c2 = s[j]
            for k in range(j):
                if bool3(c1,c2,s[k]) and bool2(i,j,k):
                    ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c = 0
l = []
for k in range(n):
  for j in range(n-1):
    if j < k and s[k] != s[j]:
      l.append((k, j))
for k,j in l:
  for i in range(n-2):
    if i < j and s[j] != s[i] and s[i] != s[k]:
      if j - i != k - j:
        c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
S=list(ns())
Rlist=[]
Glist=[]
Blist=[]
ans=0
for i in range(N):  
    if(S[i]=='R'):
        Rlist.append(i)
    elif(S[i]=='G'):
        Glist.append(i)
    else:
        Blist.append(i)


for i in Rlist:
    for j in Glist:
        for k in Blist:
                tmp=[i,j,k]
                tmp.sort()
                if((tmp[1]-tmp[0])!=(tmp[2]-tmp[1])):
                    ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
dic = {'R':0,'G':1,'B':2}

lst = []
t_last = (0,0,0)
for i in s[::-1]:
    if i=='R':
        t = (t_last[0]+1, t_last[1], t_last[2])
        lst.append(t)
        t_last = t
    elif i=='G':
        t = (t_last[0], t_last[1]+1, t_last[2])
        lst.append(t)
        t_last = t
    elif i=='B':
        t = (t_last[0], t_last[1], t_last[2]+1)
        lst.append(t)
        t_last = t
lst.reverse()

res = 0
for ni,i in enumerate(s[:-2]):
    lst3 = ['R','G','B']
    lst3.remove(i)
    for nnj,j in enumerate(s[ni+1:-1]):
        if i!=j:
            nj = nnj + ni + 1
            target = [x for x in lst3 if x != j][0]
            res += lst[nj][dic[target]]
            index = nj + (nj-ni)
            if index <= n-1 and s[index] == target:
                res -= 1
print(res)

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)
res = 0
if max(len(r),len(g),len(b)) == len(r):
  for i in g:
    for j in b:
      res += len(r) - r.count((i + j) / 2) - r.count( 2 * i - j) - r.count(2 * j - i)
else:
  for i in b:
    for j in r:
      res += len(g) - g.count((i + j) / 2) - g.count( 2 * i - j) - g.count(2 * j - i)
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L_r = []
L_g = []
L_b = []
ans = 0
for i in range(N):
    if S[i] == "R":
        L_r.append(i)
    elif S[i] == "G":
        L_g.append(i)
    else:
        L_b.append(i)
m = max(len(L_r),len(L_g),len(L_b))
if len(L_r) == m:
    for x in L_g:
        for y in L_b:
            ans += m
            if 2*x - y in L_r:
                ans -= 1
            if 2*y - x in L_r:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_r:
                ans -= 1
elif len(L_g) == m:
    for x in L_r:
        for y in L_b:
            ans += m
            if 2*x - y in L_g:
                ans -= 1
            if 2*y - x in L_g:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_g:
                ans -= 1
else:
    for x in L_r:
        for y in L_g:
            ans += m
            if 2*x - y in L_b:
                ans -= 1
            if 2*y - x in L_b:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_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[k] != S[i] and (j-i) != (k-j):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=input()
s=input()

R = [i for i, x in enumerate(s) if x == "R"]
G = [i for i, x in enumerate(s) if x == "G"]
B = [i for i, x in enumerate(s) if x == "B"]
a1 = [R,G,B]
l1=list(itertools.product(*a1))
a2 = [R,B,G]
l2=list(itertools.product(*a2))
a3 = [G,R,B]
l3=list(itertools.product(*a3))
a4 = [G,B,R]
l4=list(itertools.product(*a4))
a5 = [B,R,G]
l5=list(itertools.product(*a5))
a6 = [B,G,R]
l6=list(itertools.product(*a6))

l=l1+l2+l3+l4+l5+l6
ans = [i for i in l if i[1]-i[0]!=i[2]-i[1] and i[0]<i[1]<i[2]]
print (len(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter, deque

N = int(input())
S = list(input())
s_c = Counter(S)

a = s_c["R"] * s_c["B"] * s_c["G"]

for i in range(N):
    for j in range(i + 1, N):
        for _ in range(j + 1, N):
            if (S[i] != S[j] and S[i] != S[_] and S[j] != S[_]) and \
                not (j - i != _ - j):
                a -= 1
print(a)

Submission status: Time Limit Exceeded

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

S_R = S.count('R')
S_B = S.count('B')
S_G = S.count('G')

con = S_R * S_B * S_G

for i in range(N):
    for j in range(i+1, N):
        if S[i] != S[j] and j + (j - i) < len(S):
            if S[i] != S[j + (j - i)] and S[j] != S[j + (j - i)]:
                con -= 1

print(con)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
tot = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] != s[j]:
            li = ['R', 'G', 'B']
            li.remove(s[i])
            li.remove(s[j])
            tot += s[j + 1:].count(li[0])
            if n > 2 * j - i and s[2 * j - i] == li[0]:
                tot -= 1

print(tot)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
r=[]
g=[]
b=[]
for i in range (n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='G':
        g.append(i)
    if s[i]=='B':
        b.append(i)
lr=len(r)
lg=len(g)
lb=len(b)
d=0
for x in range (lr):
    for y in range (lg):
        for z in range (lb):
            s=[r[x],g[y],b[z]]
            c=sorted(s)
            if c[2]-c[1]!=c[1]-c[0]:
                d+=1
print(d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

from itertools import permutations, product

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

R_indices = [i for i in range(N) if S[i] == "R"]
G_indices = [i for i in range(N) if S[i] == "G"]
B_indices = [i for i in range(N) if S[i] == "B"]

count = 0
for indices_1, indices_2, indices_3 in permutations([R_indices, G_indices, B_indices]):
    j_idx_prev = -1
    for i in indices_1:
        j_idx = len(indices_2)
        ng = j_idx_prev
        while j_idx - ng > 1:
            m = (j_idx + ng) // 2
            if i < indices_2[m]:
                j_idx = m
            else:
                ng = m

        j_idx_prev = j_idx - 1
        k_idx_prev = -1
        for j in indices_2[j_idx:]:
            k_idx = len(indices_3)
            ng = k_idx_prev
            while k_idx - ng > 1:
                m = (k_idx + ng) // 2
                if j < indices_3[m]:
                    k_idx = m
                else:
                    ng = m

            k_idx_prev = k_idx - 1
            for k in indices_3[k_idx:]:
                if j - i != k - j:
                    count += 1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
s = str(input())
res = 0
nl = list(range(n))

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

for x in list(itertools.combinations(nl,3)):
    i = x[0]
    j = x[1]
    k = x[2]
    if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and j - i != k - j:
        res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = tuple(input())
if n <= 3:
    sum = 0
else:
    sum = 0
    for i in range(n-3):
        if s[i] == "R":
            j = i + 1
            while j <= n-2:
                if s[j] == "G":
                    sum += s[j:n].count("B")
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += s[j:n].count("G")
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                j += 1
        if s[i] == "G":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += s[j:n].count("B")
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += s[j:n].count("R")
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
        if s[i] == "B":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += s[j:n].count("G")
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                if s[j] == "G":
                    sum += s[j:n].count("R")
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
print(sum)