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())
l = list(input())
count = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    flag = True
    if l[i] == l[j]:
      flag = False
    for k in range(j+1,n):
      if l[j] != l[k] and l[i] != l[k] and j-i != k-j and flag:
        count += 1
print(count)

Submission status: Time Limit Exceeded

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


for i in range(1, N-1):
    for j in range(i+1, N):
        if S[i-1] == S[j-1]:
            pass
        else:
            for k in range(j+1, N+1):
                if (S[i-1] != S[k-1]) and (S[j-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()
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)
    res += lst[ni][dic[lst3[0]]] * lst[ni][dic[lst3[1]]]
    for nj in range(ni+1,n):
        nk = nj + (nj - ni)
        if nk < n:
            if set(lst3) == set([s[nj],s[nk]]):
                res -= 1
        else:
            break
print(res)

Submission status: Time Limit Exceeded

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

ri = -1
gi = -1
bi = -1

count =  0

lri = []
lgi = []
lbi = []


while True:
    ri = s.find('R', ri+1)
    if ri == -1:
        break
    lri.append(ri)

while True:
    gi = s.find('G', gi+1)
    if gi == -1:
        break
    lgi.append(gi)

while True:
    bi = s.find('B', bi+1)
    if bi == -1:
        break
    lbi.append(bi)


for r in lri:
    for g in lgi:
        for b in lbi:
            l = sorted([r, g, b])
            if l[2] - l [1] != l[1] - l[0]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(n)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
ss=input()
l=["R","G","B"]
ans=0
for v in itertools.permutations(l, 3):
    for i in range(n-2):
        if ss[i]==v[0]:
            for k in range(i+1,n-1):
                if ss[k]==v[1]:
                    ans+=ss[k+1:].count(v[2])
                    if (k+k-i <=n-1) and ss[k+k-i]==v[2]:
                        ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S={"R":[],"G":[],"B":[]}
for i,s in enumerate(input()):
	S[s].append(i)

lis = [sorted([a, b, c]) for a in S["R"] for b in S["G"] for c in S["B"]]
print(len([1 for i,j,k in lis if j-i!=k-j]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(input())
r=[0]*n
g=[0]*n
b=[0]*n
for i in range(n):
  if i==0:
    if a[n-1-i]=='R':
      r[n-1-i]=1
    elif a[n-1-i]=='G':
      g[n-1-i]=1
    else:
      b[n-1-i]=1
  else:
    if a[n-1-i]=='R':
      r[n-1-i]=r[n-i]+1
      g[n-1-i]=g[n-i]
      b[n-1-i]=b[n-i]
    elif a[n-1-i]=='G':
      r[n-1-i]=r[n-i]
      g[n-1-i]=g[n-i]+1
      b[n-1-i]=b[n-i]
    else:
      r[n-1-i]=r[n-i]
      g[n-1-i]=g[n-i]
      b[n-1-i]=b[n-i]+1

count=0
if n==1:
  print(0)
elif n==2:
  print(0)
else:
  for i in range(n-2):
    for j in range(i+1,n-1):
      if a[i]!=a[j] and 2*j-i<n and a[i]!=a[2*j-i] and a[j]!=a[2*j-i]:
        if a[i]!='R' and a[j]!='R':
          count+=r[j+1]-1
        elif a[i]!='G' and a[j]!='G':
          count+=g[j+1]-1
        else:
          count+=b[j+1]-1
      elif a[i]!=a[j]:
        if a[i]!='R' and a[j]!='R':
          count+=r[j+1]
        elif a[i]!='G' and a[j]!='G':
          count+=g[j+1]
        else:
          count+=b[j+1]
  print(count)

Submission status: Time Limit Exceeded

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



sum = 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[i] != s[k] and s[j] != s[k]:
                    #print(i,j,k)
                    sum+=1

print(sum)

Submission status: Time Limit Exceeded

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

R = [0]*n # R[i] := i文字目より右にある'R'の個数
G = [0]*n
B = [0]*n

# R
for i in range(n-2, -1, -1):
    if s[i+1] == 'R':
        R[i] = R[i+1] + 1
    else:
        R[i] = R[i+1]
# G
for i in range(n-2, -1, -1):
    if s[i+1] == 'G':
        G[i] = G[i+1] + 1
    else:
        G[i] = G[i+1]
# B
for i in range(n-2, -1, -1):
    if s[i+1] == 'B':
        B[i] = B[i+1] + 1
    else:
        B[i] = B[i+1]

cnt = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] != s[j]:
            Set = set((s[i], s[j]))
            if Set == set(('R', 'G')):
                cnt += B[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'B':
                        cnt -= 1
            elif Set == set(('G', 'B')):
                cnt += R[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'R':
                        cnt -= 1
            else:
                cnt += G[j]
                if j+(j-i) < n:
                    if s[j+(j-i)] == 'G':
                        cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

cnt=0

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

        if (j-i)!=(k-j):
          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:
from copy import deepcopy
from collections import deque
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            tal = sr
        elif ta == 'G':
            tal = sg
        else:
            tal = sb
        for ib in range(ia+1, n-1):
            if tal[ib]:
                pass
            else:
                tb = s[ib]
                t = (ta, tb)
                sa = ib - ia

                if 'R' in t and 'G' in t:
                    r += sum(sb[ib+1:])
                    if ib + sa < n:
                        r -= sb[ib + sa]
                elif 'G' in t and 'B' in t:
                    r += sum(sr[ib+1:])
                    if ib + sa < n:
                        r -= sr[ib + sa]
                else:
                    r += sum(sg[ib + 1:])
                    if ib + sa < n:
                        r -= sg[ib + sa]

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    print()
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            for k in range(j, N):
                sk = S[k]
                #if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                #if j - i != k -j:
                if j - i != k -j and si != sj and si != sk and sj != sk:
                    #if si != sj and si != sk and sj != sk:
                    #if j - i != k -j:
                    sum += 1
    print(sum)



main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

R_index = []
G_index = []
B_index = []

for i, s in enumerate(S):
    if s == 'R':
        R_index.append(i)
    elif s == 'G':
        G_index.append(i)
    elif s == 'B':
        B_index.append(i)

ans = 0
for i in R_index:
    sj = bisect_left(G_index, i)
    for ij in range(sj, len(G_index)):
        j = G_index[ij]
        sk = bisect_left(B_index, j)
        for ik in range(sk,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(B_index, i)
    for ij in range(sj,len(B_index)):
        j = B_index[ij]
        sj = bisect_left(G_index, j)
        for ik in range(sj,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in G_index:
    sj = bisect_left(B_index, i)
    for ij in range(sj, len(B_index)):
        j = B_index[ij]
        sk = bisect_left(R_index, j)
        for ik in range(sk,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(R_index, i)
    for ij in range(sj,len(R_index)):
        j = R_index[ij]
        sj = bisect_left(B_index, j)
        for ik in range(sj,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in B_index:
    sj = bisect_left(R_index, i)
    for ij in range(sj, len(R_index)):
        j = R_index[ij]
        sk = bisect_left(G_index, j)
        for ik in range(sk,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(G_index, i)
    for ij in range(sj,len(G_index)):
        j = G_index[ij]
        sj = bisect_left(R_index, j)
        for ik in range(sj,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

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

#print(B)
count = len(R)*len(G)*len(B)
for i in R:
    for j in G:
        big = max(i,j)
        small = min(i,j)
        
        if 2*big-small in B:
            count -= 1
            #print(i,j)
            #print("hoi")
        if 2*small-big in B:
            count -= 1
            #print(i,j)
        if (big+small)/2 in B:
            #print((big+small)/2)
            count -= 1
            #print(i,j)
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

n=int(sys.stdin.readline())
s=sys.stdin.readline()
#n,k=map(int,sys.stdin.readline().split())
#a=np.array(sys.stdin.readline().split(),dtype='int64')
#a=np.array([sys.stdin.readline().split() for i in range(n)],dtype='int64')

a=[[],[],[]]
r=0
g=0
b=0
m=0
for i in range(n):
    if s[i]=='R':
        a[0].append(i)
        r+=1
        for j in a[1]:
            for k in a[2]:
                if i-max(j,k)==abs(j-k):
                    m+=1
    elif s[i]=='G':
        a[1].append(i)
        g+=1
        for j in a[0]:
            for k in a[2]:
                if i-max(j,k)==abs(j-k):
                    m+=1
    elif s[i]=='B':
        a[2].append(i)
        b+=1
        for j in a[1]:
            for k in a[0]:
                if i-max(j,k)==abs(j-k):
                    m+=1
print(r*g*b-m)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items():
      a *= 1 if k==s else v
    for j in range(i//2):
      if len(set(s+S[:i+1][::-(j+1)][:3]))==3: a-=1
    ans += a
    memo[s] += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans = 0


def solve():

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

    def dfs(len, a, b):
        global ans

        if len == 1:
            for i in range(a, N-1):
                if S[a] != S[i]:
                    dfs(2, a, i)
        elif len == 2:
            for i in range(b, N):
                if S[b] != S[i] and S[a] != S[i]:
                    if i - b != b - a:
                        ans += 1

    for i in range(0, N-2):
        dfs(1, i, 0)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

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

# R: 0, G: 1, B: 2

# 後ろにいくつ自分と同じ文字があるかの表を作る
DP = []
for i in range(3):
    DP.append([0] * N)

# 末尾だけループの前に代入
if S[-1] == 'R':
    DP[0][-1] = 1
elif S[-1] == 'G':
    DP[1][-1] = 1
elif S[-1] == 'B':
    DP[2][-1] = 1

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

#print(DP)

ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        # i と j が一緒か確認
        if S[i] == S[j]:
            # 一緒であればこの2文字目はスキップ
            continue
        else:
            #print('i: {}, j: {}'.format(i, j))
            # 一緒じゃなければ3文字目(s3)を探す
            ij = (S[i], S[j])
            s3 = 'B'
            int_s3 = 2
            if 'R' not in ij:
                s3 = 'R'
                int_s3 = 0
            elif 'G' not in ij:
                s3 = 'G'
                int_s3 = 1

            # 3文字目の後ろの文字数を取得
            s3_back = DP[int_s3][j + 1]
            #print('s3: {}, s3_back = {}'.format(s3, s3_back))

            # 0ならスキップ
            if s3_back == 0:
                continue

            # j + (j - i)の場所に3文字目があれば文字数を1引く
            if j + (j - i) < N:
                if S[j + (j - i)] == s3:
                    s3_back -= 1
                    #print('kita')

            ans += s3_back

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
len_s = len(s)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
s = input()
c = Counter(s)
cnt = c['R'] * c['G'] * c['B']

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        w = j - i
        if j+w >= n:
            continue
        if s[i] == s[j+w] or s[j+w] == s[j]:
            continue
        if s[i] != s[j+w] != s[j]:
            cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(0, len(S)):
    for j in range(i + 1, len(S)):
        if S[i] == S[j]:
            continue
        else:
            for k in range(j + 1, len(S)):
                if (S[j] == S[k] or S[i] == S[k]) or (j - i == k - j):
                    continue
                else:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
lst=list(s)
r=[]
g=[]
b=[]
for i,v in enumerate(lst):
    if v=="R":
        r.append(i)
    elif v=="G":
        g.append(i)
    elif v=="B":
        b.append(i)
comb=len(r)*len(g)*len(b)
out=0
for i in r:
  for j in g:
    for k in b:
      if (i+j)==2*k or (j+k)==2*i or (k+i)==2*j:
        out+=1
print(comb-out)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R_i = [i for i, x in enumerate(S) if x == 'R']
G_i = [i for i, x in enumerate(S) if x == 'G']
B_i = [i for i, x in enumerate(S) if x == 'B']
ans = 0
for r_i in R_i:
    for g_i in G_i:
        ans += len(B_i)
        if min(r_i, g_i) - abs(r_i - g_i) in B_i:
            ans -= 1
        if max(r_i, g_i) + abs(r_i - g_i) in B_i:
            ans -= 1
        if min(r_i, g_i) + (max(r_i, g_i) - min(r_i, g_i)) / 2 in B_i:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
R=[]
G=[]
B=[]
for i in range(n):
  if s[i]=="R":
    R.append(i)
  if s[i]=="G":
    G.append(i)
  if s[i]=="B":
    B.append(i)
    
r=len(R)
g=len(G)
b=len(B)
ans=0
for i in range(r):
  for j in range(g):
    E=[]
    A=[]
    e1=2*R[i]-G[j]
    e2=(R[i]+G[j])/2
    e3=2*G[j]-R[i]
    if e1>=0:
      E.append(e1)
    if e2>=0:
      E.append(e2)
    if e3>=0:
      E.append(e3)
    A=list(set(B)-set(E))
    ans+=len(A)
    
print(ans)

Submission status: Time Limit Exceeded

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

R_list = []
G_list = []
B_list = []
for i in range(N):
    if S[i] == "R":
        R_list.append(i)
    elif S[i] == "G":
        G_list.append(i)
    else:
        B_list.append(i)

def RGB_func(F_list, S_list, T_list):
    F_len = len(F_list)
    S_len = len(S_list)
    T_len = len(T_list)
#     F_count = 0
#     S_count = 0
#     T_count = 0
    result = 0
    for i in range(F_len):
        F_pos = F_list[i]
        for j in range(S_len):
            S_pos = S_list[j]
            if S_pos > F_pos:
                for k in range(T_len):
                    T_pos = T_list[k]
                    if T_pos > S_pos and S_pos - F_pos != T_pos - S_pos:
                        result += 1
    return result

ans = 0
ans += RGB_func(R_list, G_list, B_list)
ans += RGB_func(R_list, B_list, G_list)
ans += RGB_func(G_list, R_list, B_list)
ans += RGB_func(G_list, B_list, R_list)
ans += RGB_func(B_list, R_list, G_list)
ans += RGB_func(B_list, G_list, R_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
sen=input()
S=list(sen)
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]!=S[k] and S[i]!=S[k]) and j-i!=k-j:
                count=count+1
print(count)

Submission status: Time Limit Exceeded

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

rList = []
gList = []
bList = []

for indx in range(N):
    if S[indx] == 'R':
        rList.append(indx)
    elif S[indx] == 'G':
        gList.append(indx)
    elif S[indx] == 'B':
        bList.append(indx)

NoSum = 0
fList = rList
sList = gList
remain = 'B'

for fItem in fList:
    for sItem in sList:
        if (max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem)) <= N-1):
            if (S[max(fItem, sItem) + (max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1
        
        if ((max(fItem, sItem) - min(fItem, sItem)) % 2 == 0):
            if (S[int((max(fItem, sItem) + min(fItem, sItem))/2)] == remain):
                NoSum += 1
        
        if (min(fItem, sItem) - (max(fItem, sItem) - min(fItem, sItem)) >= 0):
            if  (S[min(fItem, sItem) - (max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1

print(len(rList) * len(gList) * len(bList) - NoSum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
ans = 0
rr,gg,bb = [],[],[]
for i in range(0,len(s)):
    if s[i] == "R":
        rr.append(i)
    elif s[i] == "G":
        gg.append(i)

bb = len(s) -len(rr) -len(gg)
ans = len(rr) * len(gg) * bb
for i in range(0,len(rr)):
    for j in range(0,len(gg)):
        che = 0
        a = rr[i] + gg[j]
        if a % 2 == 0:
            if s[a//2] == "B":
                che += -1
        b = min(rr[i],gg[j]) - abs(rr[i]-gg[j])
        if b >= 0:
            if s[b] == "B":
                che += -1
        c = max(rr[i],gg[j]) + abs(rr[i]-gg[j])
        if c < n:
            if s[c] == "B":
                che += -1
        ans += che

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    R = []
    G = []
    B = []
    R_a = R.append
    G_a = G.append
    B_a = B.append

    for i in range(n):
        if s[i] == "R":
            R_a(i + 1)
        elif s[i] == "G":
            G_a(i + 1)
        else:
            B_a(i + 1)

    def chk(R,G,B):
        res = 0
        for r in R:
            for g in G:
                for b in B:
                    if r + g != b * 2 and g + b != r * 2 and b + r != g * 2:
                        res += 1
        return res
    if len(R) > len(G):
        print(chk(G, B, R))
    elif len(G) > len(B):
            print(chk(B, R, G))
    elif len(B) > len(R):
        print(chk(R, G, B))
    else:
        print(chk(R, G, B))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i+1,N):
        x,y=S[i],S[j]
        if not x==y:
            for k in range(j+1,N):
                z=S[k]
                if not x==z:
                    if not y==z:
                        if not 2*j==i+k:
                            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
A = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        si = S[i]
        sj = S[j]
        if si==sj:
            continue
        for k in range(j+1,N):
            sk = S[k]
            if not j-i == k-j and not si==sk and not sj==sk:
                A += 1
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input()) 
s = str(input()) 
count = 0
leng = len(s)
for i in range(leng):
    for j in range(leng):
        for k in range(leng):
            if (s[i] !=s[j]) and (s[i] != s[k]) and (s[j] != s[k]) and ((j-i) != (k-j)) and (i < j) and (j < k):
                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+1)
    elif s == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
ans = len(R)*len(G)*len(B)
# RGB
for a in R:
    for b in [b for b in G if b > a]:
        if b + (b-a) in B:
            ans -= 1
# RBG
for a in R:
    for b in [b for b in B if b > a]:
        if b + (b-a) in G:
            ans -= 1
# BRG
for a in B:
    for b in [b for b in R if b > a]:
        if b + (b-a) in G:
            ans -= 1
# BGR
for a in B:
    for b in [b for b in G if b > a]:
        if b + (b-a) in R:
            ans -= 1
# GRB
for a in G:
    for b in [b for b in R if b > a]:
        if b + (b-a) in B:
            ans -= 1
# GBR
for a in G:
    for b in [b for b in B if b > a]:
        if b + (b-a) in R:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

    c = 0

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for i in range(1,n-1):
    for j in range(i+1,n):
        for k in range(j+1,n+1):
            if 2*j==k+i:
                continue
            else:
                if((s[i-1]!=s[j-1])&(s[j-1]!=s[k-1])&(s[i-1]!=s[k-1])):
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
S = input()

ans = 0

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


for i in range(0, N-3):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        for c in "RGB":
            if S[i] != c and S[j] != c:
                last_color = c
        ans += S[j+1:].count(last_color)
        if j+j-i <= N-1 and S[j+j-i] == last_color:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

Rindexes = [i for i, x in enumerate(S) if x == 'R']
Gindexes = [i for i, x in enumerate(S) if x == 'G']
Bindexes = [i for i, x in enumerate(S) if x == 'B']
count = 0

# rgb
for i in Rindexes:
  for j in filter(lambda j: i < j, Gindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Bindexes)))
# rbg
for i in Rindexes:
  for j in filter(lambda j: i < j, Bindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Gindexes)))
# grb
for i in Gindexes:
  for j in filter(lambda j: i < j, Rindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Bindexes)))
# gbr
for i in Gindexes:
  for j in filter(lambda j: i < j, Bindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Rindexes)))
# brg
for i in Bindexes:
  for j in filter(lambda j: i < j, Rindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Gindexes)))
# bgr
for i in Bindexes:
  for j in filter(lambda j: i < j, Gindexes):
    count += len(list(filter(lambda k: j < k and j - i != k - j, Rindexes)))
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

result = 0
for i in list(itertools.combinations(range(N),3)):
    if (S[i[0]] != S[i[1]]) and (S[i[1]] != S[i[2]]) and (S[i[2]] != S[i[0]]) and (i[2]-i[1]!=i[1]-i[0]):
        result += 1

print(result)

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

Submission status: Time Limit Exceeded

Solution code:
# 文字列1行
N = int(input().strip())
S = input().strip()


sum_ = 0
for i in range(N - 2):
    first = S[i]
    S_ = S[i+1:]
    if first == "R":
        sum_ += S_.count("G") * S_.count("B")
    if first == "G":
        sum_ += S_.count("R") * S_.count("B")
    if first == "B":
        sum_ += S_.count("G") * S_.count("R")
    g = 1
    while True:
        j = i + g
        k = j + g
        if k >= N:
            break
        if len(set([S[i], S[j], S[k]])) == 3:
            sum_ -= 1
        g += 1


print(sum_)

Submission status: Time Limit Exceeded

Solution code:

def main():
    import numpy as np
    n = int(input())
    s = input()
    dic = {'R':np.array([]),'G':np.array([]),'B':np.array([])}
    for i in range(n):
        if s[i]== 'R':
            dic['R'] = np.append(dic['R'],i)
        elif s[i]== 'G':
            dic['G'] = np.append(dic['G'],i)
        else:
            dic['B'] = np.append(dic['B'],i)
    cnt = len(dic['R'])*len(dic['G'])*len(dic['B'])
    for i in dic['R']:
        for j in dic['G']:
            if abs(j-i)%2==0:
                no = np.array([(j+i)//2,2*j-i,2*i-j])
            else:
                no = np.array([2*j-i,2*i-j])
            cnt -= len(np.intersect1d(no,dic['B']))
    print(cnt)
     
            
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
x = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    if 2*j-i<= n-1 and len({s[i], s[j], s[2*j-i]})==3:
        x += 1
print(a*b*c-x)

Submission status: Time Limit Exceeded

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

for a in range(N-2):
  for b in range(a+1,N-1):
    if S[a] != S[b]:
      for c in range(b+1,N):
        if a-b != b-c and S[b] != S[c] and S[c] != S[a]:
        	count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

for index, color in enumerate(S):
  if color == "R":
    R.append(index)
  if color == "G":
    G.append(index)
  if color == "B":
    B.append(index)

result = 0
for r_index in R:
  for g_index in G:
    for b_index in B:
      sorted_index = sorted([r_index, g_index, b_index])

      if(sorted_index[1] - sorted_index[0] != sorted_index[2] - sorted_index[1]):
        result += 1
print(result)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if (j-i!=k-j):
                if (s[i] != s[j] and s[i] != s[k] and s[j] != s[k]):
    #                print("{}:{}:{}".format(i,j,k))
                    c+=1
print(c)