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()
total = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1
        elif S[i] == 'G':
            if S[j] == 'G':
                continue
            elif S[j] == 'R':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
        if S[i] == 'B':
            if S[j] == 'B':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
            elif S[j] == 'R':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
input()
R, G, B = list(), list(), list()
for i, j in enumerate(input()):
  if j == 'R':
    R.append(i)
  elif j == 'G':
    G.append(i)
  else:
    B.append(i)

ans = len(R)*len(G)*len(B)
for i in R:
  for j in G:
    temp = 0
    for k in B:
      if k < temp:
        continue
      if (i+j+k)/3 in {i, j, k}:
        ans -= 1
        if k < i and k < j:
          temp = min(i, j)
        elif (k-i)*(j-k) > 0:
          temp = max(i, j)
        else:
          break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
input()
R, G, B = list(), list(), list()
for i, j in enumerate(input()):
  if j == 'R':
    R.append(i)
  elif j == 'G':
    G.append(i)
  else:
    B.append(i)

ans = len(R)*len(G)*len(B)
for i in R:
  for j in G:
    for k in B:
      if (i+j+k)/3 in {i, j, k}:
        ans -= 1
        if k > i and k > j:
          break
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
input()
R, G, B = list(), list(), list()
for i, j in enumerate(input()):
  if j == 'R':
    R.append(i)
  elif j == 'G':
    G.append(i)
  else:
    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 (R[i]+G[j]+B[k])/3 in {R[i], G[j], B[k]}:
        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
input()
R, G, B = list(), list(), list()
for i, j in enumerate(input()):
  if j == 'R':
    R.append(i)
  elif j == 'G':
    G.append(i)
  else:
    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 (R[i]+G[j])/2 == B[k] or (G[j]+B[k])/2 == R[i] or (R[i]+B[k])/2 == G[j]:
        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
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)

count = 0
# for r in R:
#     for g in G:
#         for b in B:
#             l = sorted([r, b, g])
#             if (l[0] + l[2])/2 != l[1]:
#                 count += 1

for r, g, b in list(itertools.product(R, G, B)):
    l = sorted([r, b, g])
    if (l[0] + l[2])/2 != l[1]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
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)

count = 0
for r in R:
    for g in G:
        for b in B:
            l = sorted([r, b, g])
            if (l[0] + l[2])/2 != l[1]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

def judge(numlist):
    if numlist[2] + numlist[0] == 2 * numlist[1]:
        return False
    else:
        return True

N = int(input())
S = list(input())
R = []
G = []
B = []

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

RG = product(R,G)
RGcombs = []
for i in RG:
    RGcombs.append(sorted(list(i)))

ans = 0
for comb in RGcombs:
    for k in B:
        if k < comb[0]:
            if judge([k]+list(comb)):
                ans += 1
        elif k > comb[1]:
            if judge(list(comb)+[k]):
                ans += 1
        else:
            c = list(comb)
            if judge([c[0],k,c[1]]):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

sum = 0
for i in range(N-2):
    for j in range(i+1,N):
        for k in range(j+1, N):
#            print(i, j, k)
            if S[i] == S[j] or S[j] == S[k] or S[i] == S[k]:
                continue
            elif j - i == k - j:
                continue
            else:
                sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def judge(numlist):
    if numlist[2] + numlist[0] == 2 * numlist[1]:
        return False
    else:
        return True

N = int(input())
S = list(input())
R = []
G = []
B = []

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

RGcombs = []

for i in R:
    for j in G:
        if i < j:
            RGcombs.append([i,j])
        else:
            RGcombs.append([j,i])

ans = 0
for comb in RGcombs:
    for k in B:
        if k < comb[0]:
            if judge([k]+comb):
                ans += 1
        elif k > comb[1]:
            if judge(comb+[k]):
                ans += 1
        else:
            if judge([comb[0],k,comb[1]]):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

from math import gcd
sum = 0
for i in range(N-2):
    for j in range(i+1,N):
        for k in range(j+1, N):
#            print(i, j, k)
            if S[i] == S[j] or S[j] == S[k] or S[i] == S[k]:
                continue
            elif j - i == k - j:
                continue
            else:
                sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def judge(numlist):
    if numlist[2] + numlist[0] == 2 * numlist[1]:
        return False
    else:
        return True

N = int(input())
S = list(input())
R = []
G = []
B = []

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

RGcombs = []

for i in R:
    for j in G:
        if i < j:
            RGcombs.append([i,j])
        else:
            RGcombs.append([j,i])

RGBcombs = []

for comb in RGcombs:
    for k in B:
        if k < comb[0]:
            RGBcombs.append([k]+comb)
        elif k > comb[1]:
            RGBcombs.append(comb+[k])
        else:
            RGBcombs.append([comb[0],k,comb[1]])

ans = 0
for satcomb in RGBcombs:
    if judge(satcomb):
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby
from statistics import median
from time import time
from sys import exit
import numpy as np

N = int(input())

line = ([(index, v) for index, v in enumerate(input())])


def types(line): return line[1]
def index(line): return line[0]


gp = groupby(sorted(line, key=types), types)

d = {}

for k, v in gp:
    indexes = np.array([i for i, value in v])
    d[k] = indexes

if 'R' not in d or 'G' not in d or 'B' not in d:
    print(0)
    exit()

delete = 0

for differ in range(1, N):
    for start in range(N):
        if start + differ*2 > N - 1:
            break
        i = line[start]
        j = line[start + differ]
        k = line[start + differ*2]

        if len({i[1], j[1], k[1]}) == 3:
            upper = max(i[0], j[0], k[0])
            lower = min(i[0], j[0], k[0])
            median_num = median([i[0], j[0], k[0]])
            if upper - median_num == median_num - lower:
                delete += 1

print(d['R'].size * d['G'].size * d['B'].size - delete)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())

line = input()
target_itr = (list(combinations(range(N), 3)))


def ok_list(target_itr):
    for target in target_itr:
        if (target[1] - target[0]) != (target[2] - target[1]):
            yield target[0], target[1], target[2]


def itr_line(line, target_itr):
    result = 0
    for combination in target_itr:
        i = line[combination[0]]
        j = line[combination[1]]
        k = line[combination[2]]

        if i != j and i != k and j != k:
            result += 1
    print(result)


itr_line(line, ok_list(target_itr))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())

line = input()
target_itr = (list(combinations(range(N), 3)))


def ok_list(target_itr):
    for target in target_itr:
        if (target[1] - target[0]) != (target[2] - target[1]):
            yield target[0], target[1], target[2]


def itr_line(line, target_itr):
    for combination in target_itr:
        i = line[combination[0]]
        j = line[combination[1]]
        k = line[combination[2]]

        if i != j and i != k and j != k:
            yield i, j, k


result = list(itr_line(line, ok_list(target_itr)))
print(len(result))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count_R = 0
count_G = 0
count_B = 0
count_2 = 0
R = []
G = []
B = []


for i in range(N):
    if S[i] == 'R':
        count_R = count_R+1
        R.append(i)
    elif S[i] == 'G':
        count_G = count_G+1
        G.append(i)
    else:
        count_B = count_B+1
        B.append(i)
count = count_R*count_G*count_B

for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            if R[i]+B[k] == 2*G[j]:
                count_2 = count_2+1
            elif R[i]+G[j] == 2*B[k]:
                count_2 = count_2+1
            elif G[j]+B[k] == 2*R[i]:
                count_2 = count_2+1
                

ans = count-count_2
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(N):
        for k in range(N):
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i] and i+k != 2*j and i<j<k:
                count = count+1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
#%%
cum = [[0]*(N+1) for _ in range(3)]
for i in range(N):
    cum[0][i+1] = cum[0][i] + (S[i]=='R')
    cum[1][i+1] = cum[1][i] + (S[i]=='G')
    cum[2][i+1] = cum[2][i] + (S[i]=='B')
    
def color(i,j):
    RGB = [0]*3
    if S[i]=='R' or S[j]=='R':
        RGB[0] = 1
    if S[i]=='G' or S[j]=='G':
        RGB[1] = 1
    if S[i]=='B' or S[j]=='B':
        RGB[2] = 1
    
    if RGB[0]==0:
        return 0
    elif RGB[1]==0:
        return 1
    else:
        return 2
def col(s):
    if s=='R':
        return 0
    elif s=='G':
        return 1
    else:
        return 2

ans = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i]!=S[j]:
            idx = color(i,j)
            ans += cum[idx][N]-cum[idx][j+1]
            k = 2*j-i
            if k<N and col(S[k])==idx:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b
for x in range(N):
    for a in range(N):
        y = x+a
        z = y+a
        if z >= N:
            continue
        if S[x] == S[y]:
            continue
        if S[y] == S[z]:
            continue
        if S[x] == S[z]:
            continue
        ans = ans-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
N = int(input())
S = input().rstrip()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b
for x in range(N):
    for a in range(N):
        y = x+a
        z = y+a
        if z >= N:
            continue
        if S[x] == S[y]:
            continue
        if S[y] == S[z]:
            continue
        if S[x] == S[z]:
            continue
        ans = ans-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
N = int(input())
S = input().rstrip()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b
for x in range(N):
    for a in range(N):
        y = x+a
        z = y+a
        if z >= N:
            continue
        if S[x] != S[y]and S[y] != S[z] and S[x] != S[z]:
            ans = ans-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
N = int(input())
S = input().rstrip()
R = []
G = []
B = []
ans=0
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)

for r in R:
    for g in G:
        for b in B:
            if g-r!=b-g and b-r!=g-b and r-g!=b-r and b-g!=r-b and r-b!=g-r and g-b!=r-g:
                ans=ans+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
N = int(input())
if N <= 3:
    print(0)
    sys.exit()
s = input()
ls = [defaultdict(int) for i in range(N)]
a = ['R', 'G', 'B']
for i, x in enumerate(s):
    for y in a:
        if x == y:
            ls[i][y] = ls[max(0, i-1)][y] + 1
        else:
            ls[i][y] = ls[max(0, i-1)][y]
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if s[i] != s[j]:
            for k in a:
                if k != s[i] and k != s[j]:
                    ans += ls[-1][k] - ls[j][k]
            if 2*j-i <N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import statistics

n = int(input());
s = str(input());
rlist = [0]
glist = [0]
blist = [0]
for i in range(n):
    if s[i] == 'R':
        rlist.append(i+1);
    elif s[i] == 'G':
        glist.append(i+1);
    elif s[i] == 'B':
        blist.append(i+1);
#print(rlist);
#print(glist);
#print(blist);

rr = len(rlist)
gg = len(glist)
bb = len(blist)
if rr==1 or gg==1 or bb==1:
    print(0);
else:
    con = 0;
    for r in range(1,rr):
        x = rlist[r]
        for g in range(1,gg):
            y = glist[g]
            for b in range(1,bb):
                z = blist[b]
                sol = [0]*3
                sol[0] =x
                sol[1] =y
                sol[2] = z
                if (max(x,y,z)-statistics.median(sol))!=(statistics.median(sol)-min(x,y,z)):
                    con+=1
    print(con);

Submission status: Time Limit Exceeded

Solution code:
n = int(input());
s = str(input());
rlist = [0]
glist = [0]
blist = [0]
for i in range(n):
    if s[i] == 'R':
        rlist.append(i+1);
    elif s[i] == 'G':
        glist.append(i+1);
    elif s[i] == 'B':
        blist.append(i+1);

rr = len(rlist)
gg = len(glist)
bb = len(blist)
if rr==1 or gg==1 or bb==1:
    print(0);
else:
    con = 0;
    for r in range(1,rr):
        x = rlist[r]
        for g in range(1,gg):
            y = glist[g]
            for b in range(1,bb):
                z = blist[b]
                sol = [0]*3;
                sol[0]= x
                sol[1]= y
                sol[2]= z
                sol.sort()
                if (sol[2]-sol[1])!=(sol[1]-sol[0]):
                    con+=1
    print(con);

Submission status: Time Limit Exceeded

Solution code:
import itertools
from collections import Counter

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

    C=Counter(S)
    r = C['R']
    g = C['G']
    b = C['B']


    cnt = r * g * b

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

    cnt -= sum

    print(cnt)

SUMCHK()

Submission status: Time Limit Exceeded

Solution code:
import itertools
from collections import Counter

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

C=Counter(S)
r = C['R']
g = C['G']
b = C['B']


cnt = r * g * b

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

cnt -= sum

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

r = S.count('R')
g = S.count('G')
b = S.count('B')


cnt = r * g * b

sum = 0
for i in range(0,N-2):
    for j in range(i,N-1):
        for k in range(j,N):
            if j - i == k - j:
                if (S[i] != S[j]) and (S[i] != S[k] and S[j] != S[k]):
                    sum += 1
                    break
                
cnt -= sum

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

sum = 0
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i] == S[j]:
            continue
        for k in range(j+1,N):
            if S[i] == S[k] or S[j] == S[k]:
                continue
            if j - i != k - j:
                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:
import itertools

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
S = input()
S_No = []

for n in range(N):
    S_No.append( (n, S[n]) )
    
ans = 0

C = list(itertools.combinations(S_No ,3))

for c in C:
    if c[0][1] != c[1][1] and c[0][1] != c[2][1] and c[1][1] != c[2][1] and c[1][0] - c[0][0] != c[2][0] - c[1][0] :
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(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 = sum(R)*sum(G)*sum(B)

for i in range(N):
    for j in range(i+1,N):
        k = 2*(j+1)-(i+1)-1
        if  k<N:
            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()
r=[[0,0,0] for _ in range(n)]
c=0
l={"GB":0,"BG":0,"RB":1,"BR":1,"RG":2,"GR":2}
for i in range(n):
  if s[i] =="R":
    r[i][0]=r[i-1][0]+1
    r[i][1]=r[i-1][1]
    r[i][2]=r[i-1][2]
  elif s[i]=="G":
    r[i][1]=r[i-1][1]+1
    r[i][0]=r[i-1][0]
    r[i][2]=r[i-1][2]
  elif s[i]=="B":
    r[i][2]=r[i-1][2]+1
    r[i][0]=r[i-1][0]
    r[i][1]=r[i-1][1]

for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i]==s[j]:
      continue
    else:
      c+=r[n-1][l[s[i]+s[j]]]-r[j][l[s[i]+s[j]]]
    
      
      if 2*j-i<n:
        if s[2*j-i]!= s[i] and s[2*j-i]!=s[j]:
          c-=1
   
  
print(c)

Submission status: Time Limit Exceeded

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

from collections import defaultdict

memo = [defaultdict(int) for j in range(N+1)]

ans = 0



for i,s in enumerate(S):
    memo[i+1]["R"] = memo[i]["R"]
    memo[i+1]["G"] = memo[i]["G"]
    memo[i+1]["B"] = memo[i]["B"]
    memo[i+1][s]+=1
    others = set(["R","G","B"])
    others.remove(s)
    others = list(others)
    ans += memo[i+1][others[0]] * memo[i+1][others[1]]

rgb = set(["R","G","B"])
# print(rgb)

if N > 2:
    if N %2 == 1:
        length = N // 2
    else:
        length = N // 2 - 1

    # print(length)
    
    for skip in range(length):
        for i in range(len(S)):
            # print(i)
            if i+2+skip*2 > len(S)-1:

                break
            a,b,c = S[i],S[i+1+skip],S[i+2+skip*2]
            # print(set([a,b,c]))
            if set([a,b,c]) == rgb:
                ans -= 1
        

print(ans)

Submission status: Time Limit Exceeded

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

lr = list()
lg = list()
lb = list()

for i in range(n):
    if s[i] == "R":
        lr.append(i)
    if s[i] == "G":
        lg.append(i)
    if s[i] == "B":
        lb.append(i)

r = len(lr)
g = len(lg)
b = len(lb)

ans = r*g*b

for i in lr:
    for j in lg:
        d1 = abs(i-j)
        if max(i,j) + d1 in lb:
            ans -= 1
        if min(i,j) - d1 in lb:
            ans -= 1
        if min(i,j) + d1/2 in lb:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

lr = list()
lg = list()
lb = list()

for i in range(n):
    if s[i] == "R":
        lr.append(i)
    if s[i] == "G":
        lg.append(i)
    if s[i] == "B":
        lb.append(i)
b = len(lb)

cnt = 0
for i in lr:
    for j in lg:
        d1 = abs(i-j)
        p = 0
        if max(i,j) + d1 in lb:
            p += 1
        if min(i,j) - d1 in lb:
            p += 1
        if min(i,j) + d1/2 in lb:
            p += 1
        cnt += b-p


print(cnt)

Submission status: Time Limit Exceeded

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

lr = list()
lg = list()
lb = list()

for i in range(n):
    if s[i] == "R":
        lr.append(i)
    if s[i] == "G":
        lg.append(i)
    if s[i] == "B":
        lb.append(i)

cnt = 0
for i in lr:
    for j in lg:
        d1 = abs(i-j)
        for k in lb:
            d2 = abs(k-i)
            if abs(k-j) != d1 and abs(k-j) != d2 and d1 != d2:
                cnt += 1 
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def readinput():
    n=int(input())
    s=input()
    s=' '+s
    return n,s

def main(n,s):
    count=0
    all=set(['R','G','B'])
    for i in range(1, n-1):
        l1=s[i]
        for j in range(i+1, n):
            l2=s[j]
            if l1==l2:
                continue
            l3=(all - set([l1,l2])).pop()
            ij=j-i
            sub1=s[j+1:j+ij]
            sub2=s[j+ij+1:]
            count+=sub1.count(l3)
            count+=sub2.count(l3)
    return count

if __name__=='__main__':
    n,s=readinput()
    ans=main(n,s)
    print(ans)

Submission status: Time Limit Exceeded

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

slist = []

for c in S:
    num = 1

    if c == 'G':
        num = 2
    elif c == 'B':
        num = 4

    slist.append(num)

count = 0


for i in range(0, N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if slist[i] + slist[j] + slist[k] == 7 and 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(0, 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:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

r_idx = []
g_idx = []
b_idx = []
for i in range(N):
    if S[i] == 'R':
        r_idx.append(i)
    elif S[i] == 'G':
        g_idx.append(i)
    else:
        b_idx.append(i)
        
cnt = 0
for r in r_idx:
    for g in g_idx:
        for b in b_idx:
            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 = 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]:
            continue
        for k in range(j+1, N+1):
            if S[i-1] != S[k-1] and S[j-1] != S[k-1]:
                if j-i != k-j:
                    cnt += 1
print(cnt)

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

Submission status: Time Limit Exceeded

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

a = []

for j in range(1, n-1):
    sj = s[j]
    i = j - 1
    while i > -1:
        if sj != s[i]:
           a.append((i, j)) 
        i -= 1

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

print(ans)

Submission status: Time Limit Exceeded

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

num_R = S.count('R')
num_G = S.count('G')
num_B = S.count('B')
S = '_' + S

count = 0
for i in range(1, N+1):
    for j in range(i, N+1):
        for k in range(j, N+1):
            if j - i == k - j and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                count += 1
#                print(i, j, k)
print(num_R*num_G*num_B - count)

Submission status: Time Limit Exceeded

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

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

for i in range(1,N+1):
    if S[i-1] == 'R':
        R.append(i)
    elif S[i-1] == 'G':
        G.append(i)
    elif S[i-1] == 'B':
        B.append(i)

ans = 0
for a in R:
    for b in G:
        for c in B:
            i = min(a,b,c)
            k = max(a,b,c)
            j = a+b+c-i-k
            if j-i != k-j:
                ans += 1


print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

c = list(itertools.combinations_with_replacement(list(range(1,N+1)), 3))

ans = 0
for i in range(len(c)):
    if S[c[i][0]-1] != S[c[i][1]-1] and S[c[i][0]-1] != S[c[i][2]-1] and S[c[i][1]-1] != S[c[i][2]-1] and c[i][1]-c[i][0] != c[i][2]-c[i][1]:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
"""
rcount=0
gcount=0
bcount=0
for i in range(n):
    if s[i]=="R":
        rcount+=1
    elif s[i]=="G":
        gcount+=1
    else:
        bcount+=1
"""
al=s.count("R")*s.count("G")*s.count("B")

for i in range(n-2):
    for k in range(i+1,n):
        dis=k-i
        l=k+dis
        if k+dis>=n:
            continue
        if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
            al-=1

print(al)

Submission status: Time Limit Exceeded

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

rcount=0
gcount=0
bcount=0
for i in range(n):
    if s[i]=="R":
        rcount+=1
    elif s[i]=="G":
        gcount+=1
    else:
        bcount+=1

al=rcount*bcount*gcount

for i in range(n-2):
    for k in range(i+1,n):
        dis=k-i
        l=k+dis
        if k+dis>=n:
            continue
        if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
            al-=1

print(al)

Submission status: Time Limit Exceeded

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

rcount=0
gcount=0
bcount=0
for i in range(n):
    if s[i]=="R":
        rcount+=1
    elif s[i]=="G":
        gcount+=1
    else:
        bcount+=1

al=rcount*bcount*gcount

for i in range(n-2):
    for k in range(i+1,n):
        dis=k-i
        if k+dis<=n-1:
            l=k+dis
            if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
                al-=1

print(al)

Submission status: Time Limit Exceeded

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

red = []
green = []
blue = []


for i in range(N):
  if S[i] == 'R':
    red.append(i)
  if S[i] == 'G':
    green.append(i)
  if S[i] == 'B':
    blue.append(i)
all_ans = len(red)*len(green)*len(blue)

for r in red:
  for g in green:
    for b in blue:
      k = [r,g,b]
      k.sort()

      if k[1]-k[0] == k[2]-k[1]:
        all_ans -= 1
        
print(all_ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i, n-1):
        for k in range(j, n):
            if j-i==k-j:
                if set([s[j], s[i], s[k]]) == set(['R', 'G', 'B']):
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i,j in enumerate(s):
  if j == 'R':
    r.append(i)
  if j == 'G':
    g.append(i)
  if j == 'B':
    b.append(i)
cnt = 0
for i in r:
  for j in g:
    cnt += len(b)
    if 2*j-i in b:
      cnt -= 1
    if 2*i-j in b:
      cnt -= 1
    if (i+j)%2 == 0 and (i+j)//2 in b:
      cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

r, g, b = 0, 0, 0
for i in range(n):
  if s[i] == 'R':
    r += 1
  elif s[i] == 'G':
    g += 1
  else:
    b +=1

count = r * g * b
sub = 0 

for i in range(n):
  for j in range(i+1, n):
    k = j*2-i
    if s[i] == s[j] or k >= n or s[k] == s[i] or s[k] == s[j]:
      continue
    else:
      sub += 1
print(count - sub)

Submission status: Time Limit Exceeded

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

r, g, b = 0, 0, 0
for i in range(n):
  if s[i] == 'R':
    r += 1
  elif s[i] == 'G':
    g += 1
  else:
    b +=1

count = r * g * b
sub = 0 

for i in range(n):
  for j in range(i+1, n):
    k = j*2-i
    if s[i] == s[j]:
      continue
    elif k >= n or s[k] == s[i] or s[k] == s[j]:
      continue
    else:
      sub += 1
print(count - sub)