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()

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

ng=0

all=r*g*b

#NGパターン数の洗い出し
for i in range(0,n-2):
  for j in range(i,n-1):
    if s[i]==s[j]:
      continue
    else:
      if j*2-i<=n-1:
#        print(i,j,j*2-i)
        if s[j]!=s[j*2-i] and s[j*2-i]!=s[i]:
          ng+=1
print(all-ng)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = input()
listS = list(S)
l=0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if j-i == k-j:
                continue
            elif listS[j]==listS[k] or listS[i]==listS[k] or listS[i]==listS[j]:
                continue
            else:
                l+=1
print(l)

Submission status: Time Limit Exceeded

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

import numpy as np 

# Input Stdin
## 1 <= N <= 4000
## S = R/G/B length(N)

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

num = 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] and (j - i) != (k - j):
                #print(str(i) + str(j) + str(k) + ' ' + S[i] + S[j] + S[k])
                num = num + 1
                
print(num)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = input()
listS = list(S)
l=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if listS[i]==listS[j]:
            continue
        for k in range(j+1,N):
            if j-i == k-j:
                continue
            elif listS[j]==listS[k] or listS[i]==listS[k]:
                continue
            else:
                l+=1
print(l)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
listS = list(S)
l=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if listS[i]==listS[j]:
            continue
        for k in range(j+1,N):
            if j-i == k-j:
                continue
            elif listS[j]==listS[k] or listS[i]==listS[k]:
                continue
            else:
                l+=1
print(l)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
listS = list(S)
l=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if listS[i]==listS[j]:
            continue
        for k in range(j+1,N):
            if listS[j]==listS[k] or listS[i]==listS[k]:
                continue
            elif j-i == k-j:
                continue
            else:
                l+=1
print(l)

Submission status: Time Limit Exceeded

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

def dkey(dict, val):
    for e in dict:
        if dict[e] == val:
            return e

sum = [[0] * (n+1) for i in range (3)]
for i in range (3):
    for j in range (n):
        if(s[j] == dkey(colors,i + 1)):
            sum[i][j + 1] = sum[i][j] + 1
        else:
            sum[i][j + 1] = sum[i][j]

ans = 0
for i in range (n-2):
    for j in range (i+1,n-1):
        if s[i] == s[j]:
            continue
        k = 6//(colors[s[i]]*colors[s[j]])
        ans += sum[k-1][n] - sum[k-1][j+1]
        if 2*j-i < n and s[2*j-i] == dkey(colors,k):
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

n_total = 0
S_ik, S_jk = [], []
for i in range(0,N-2):
  tmp = []
  for k in range(i+2,N):
    if S[i] != S[k]:
      tmp.append(k)
  S_ik.append(tmp)
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      group1 = list(set(S_ik[i])&set(S_jk[j-1]))
      group2 = list(set([j-i+j])&set(group1))
      if len(group2) == 0:
        n_total += len(group1)
      else:
        n_total += len(group1)-1
print(n_total)

Submission status: Time Limit Exceeded

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

r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i, N):
        k = 2 * j - i
        if (i < j) & (j < k) & (k < N) :
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

n_total = 0
S_ik, S_jk = [], []
for i in range(0,N-2):
  tmp = []
  for k in range(i+2,N):
    if S[i] != S[k]:
      tmp.append(k)
  S_ik.append(tmp)
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      group = list(set(S_ik[i])&set(S_jk[j-1]))
      for k in range(len(group)):
        if (j-i) != (group[k]-j):
          n_total+=1
print(n_total)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
num=[0]*3

for i,co in enumerate(('R','G','B')):
  for c in s:
    if c == co:
      num[i] +=1
      
all = num[0]*num[1]*num[2]

ot = 0
i=1
while i*2 +1 <= len(s):
  idx = 0
  #print(i)
  while idx + i*2 < len(s):
    #print('index',idx)
    wk = s[idx]+s[idx+i]+s[idx+2*i]
    #print(wk)
    if ('R' in wk) and ('G' in wk) and ('B' in wk):
      ot +=1
    idx +=1
  i += 1
  
print(all-ot)

Submission status: Time Limit Exceeded

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

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

total = 0

for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
  else:
    B.append(i)
    
for i in range(len(R)):
  for j in range(len(G)):
    for k in range(len(B)):
      if (R[i] > G[j]) & (R[i] > B[k]):
        if (R[i] - G[j]) == (2 * (R[i] - B[k])):
          continue
        elif (2 * (R[i] - G[j])) == (R[i] - B[k]):
          continue
        else:
          total += 1
      if (G[j] > R[i]) & (G[j] > B[k]):
        if (G[j] - R[i]) == (2 * (G[j] - B[k])):
          continue
        elif (2 * (G[j] - R[i])) == (G[j] - B[k]):
          continue
        else:
          total += 1
      if (B[k] > R[i]) & (B[k] > G[j]):
        if (B[k] - R[i]) == (2 * (B[k] - G[j])):
          continue
        elif (2 * (B[k] - R[i])) == (B[k] - G[j]):
          continue
        else:
          total += 1 
        
print(total)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []

for x in range(n):
  if s[x] == 'R':
    r.append(x)
  elif s[x] == 'G':
    g.append(x)
  elif s[x] == 'B':
    b.append(x)

#ans = 0    
ans = len(r)*len(g)*len(b)
tmp = []
not_ans = 0

for i in r:
  for j in g:
    for k in b:
      tmp.extend([i,j,k])
      tmp.sort()
      if tmp[1]-tmp[0] == tmp[2]-tmp[1]:
#        ans += 1
        not_ans -= 1
      tmp = []

print(ans + not_ans)

Submission status: Time Limit Exceeded

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

n_total = 0

S_jk = []
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(len(S_jk[j-1])):
        if S[i] != S[S_jk[j-1][k]] and (j-i) != (S_jk[j-1][k]-j):
          n_total+=1
print(n_total)

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

Submission status: Time Limit Exceeded

Solution code:
#author : dokueki

import sys
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n = int(sys.stdin.readline())
    s = input()
    ans = 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:
                    ans += 1
    print(ans)


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Submission status: Time Limit Exceeded

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

rcount = S.count("R")
gcount = S.count("G")
bcount = N-rcount-gcount



count = rcount * gcount * bcount

for i in range(N-1):
    for j in range(i+1,N):
        if S[i] == "R" and S[j] == "G":
            if j+j-i < N and S[j+j-i] == "B":
                count -= 1
            
        if S[i] == "R" and S[j] == "B":
            if j+j-i < N and S[j+j-i] == "G":
                count -= 1

        if S[i] == "G" and S[j] == "R":
            if j+j-i < N and S[j+j-i] == "B":
                count -= 1

        if S[i] == "G" and S[j] == "B":
            if j+j-i < N and S[j+j-i] == "R":
                count -= 1

        if S[i] == "B" and S[j] == "R":
            if j+j-i < N and S[j+j-i] == "G":
                count -= 1

        if S[i] == "B" and S[j] == "G":
            if j+j-i < N and S[j+j-i] == "R":
                count -= 1

print(count)

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
    elif S[i] == "B":
        bcount += 1

count = rcount * gcount * bcount

for i in range(N-1):
    for j in range(i+1,N):
        if S[i] == "R" and S[j] == "G":
            if j+j-i < N and S[j+j-i] == "B":
                count -= 1
            
        if S[i] == "R" and S[j] == "B":
            if j+j-i < N and S[j+j-i] == "G":
                count -= 1

        if S[i] == "G" and S[j] == "R":
            if j+j-i < N and S[j+j-i] == "B":
                count -= 1

        if S[i] == "G" and S[j] == "B":
            if j+j-i < N and S[j+j-i] == "R":
                count -= 1

        if S[i] == "B" and S[j] == "R":
            if j+j-i < N and S[j+j-i] == "G":
                count -= 1

        if S[i] == "B" and S[j] == "G":
            if j+j-i < N and S[j+j-i] == "R":
                count -= 1

print(count)

Submission status: Time Limit Exceeded

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

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

tmp=0

for i in range(N):
  for j in range(i,N):
    if j-i+j < N:
      if len(set([S[i],S[j],S[j-i+j]]))==3:
        tmp += 1

ans = ans - tmp
print(ans)

Submission status: Time Limit Exceeded

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

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'

# N = 4
# S = 'RRGB'

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    r = [0]*n
    g = [0]*n
    b = [0]*n
    ans = 1

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

    for i in range(n):
        dict[s[i]][i] = 1
    
    ans = sum(dict["R"])*sum(dict["G"])*sum(dict["B"])
 
    for i in range(n):
        for j in range(i,n):
            s1 = s[i]
            s2 = s[j]
            if s1 != s2:
                t = (s1,s2)
                for s3 in "RGB":
                    if s3 not in t:
                        if 2*j-i < n and dict[s3][2*j-i] == 1:
                            ans -= 1
                        continue

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    r = [0]*n
    g = [0]*n
    b = [0]*n
    ans = 1

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

    for i in range(n):
        dict[s[i]][i] = 1
    
    for l in dict.values():
        ans *= sum(l)
 
    for i in range(n):
        for j in range(i,n):
            if dict[s[i]][j] == 0:                
                s1 = s[i]
                s2 = s[j]
                t = (s1,s2)
                for s3 in "RGB":
                    if s3 not in t:
                        if 2*j-i < n and dict[s3][2*j-i] == 1:
                            ans -= 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N,=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
st,=map(list, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
sum=0
K=range(1,N+1)
for k in K:
	J=range(1,k)
	for j in J:
		if (st[k-1]==st[j-1]):
			continue
		I=range(1,j)   
		for i in I:
			if st[i-1]!=st[j-1] and st[k-1]!=st[i-1] and i!=2*j-k:
				sum+=1     
print(sum)

Submission status: Time Limit Exceeded

Solution code:
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

for r in r_set:
    for g in g_set:
        for b in b_set:
            mi = min(r, g, b)
            ma = max(r, g, b)
            set_re = {r, g, b} - {mi, ma}
            re = list(set_re)[0]
            if re - mi != ma - re:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = len(S.replace("R", "").replace("G", ""))
b = N - r - g
 
ans = r * g * b
 
is_break = False
for i in range(N):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
        if (0 <= i) & (i < j) & (j < k) & (k < N):
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
st,=map(str, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
sum=0
st = list(st)
#print(st)
for k in range(1,N+1):
    for j in range(1,k):
      if (st[k-1]==st[j-1]):
      	continue
      for i in range(1,j):     
        if (st[i-1]!=st[j-1] and st[k-1]!=st[i-1] and (j-i)!=(k-j)):
        	#print(st[k-1],st[j-1],st[i-1])
        	sum+=1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
#b = len(S.replace("R", "").replace("G", ""))
b = N - r - g
 
ans = r * g * b
 
for i in range(N):
    for k in [N - k_ - 1 for k_ in range(N)]:
        if i >= k:
            break
            
        tar_j = i + k
        if (tar_j > 2 * i) & (tar_j < 2 * k):
            if np.mod(tar_j, 2) == 0:
                j = int(tar_j / 2)
                s = S[i] + S[j] + S[k]
                if ("R" in s) & ("G" in s) & ("B" in s):
                    ans -= 1
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
#b = len(S.replace("R", "").replace("G", ""))
b = N - r - g
 
ans = r * g * b
 
for i in range(N):
    for k in [N - k_ - 1 for k_ in range(N)]:
        tar_j = i + k
        if (tar_j > 2 * i) & (tar_j < 2 * k):
            if np.mod(tar_j, 2) == 0:
                j = int(tar_j / 2)
                s = S[i] + S[j] + S[k]
                if ("R" in s) & ("G" in s) & ("B" in s):
                    ans -= 1
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

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

# N = 39
# S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

def calculate(n, s):
    arr = list(s)

    rNum = arr.count("R")
    gNum = arr.count("G")
    bNum = arr.count("B")

    sum = 0

    for step in range(1,math.ceil(n / 2) + 1):
        for i in range(n - 2*step):
            dict = {"R":False,"G":False,"B":False}

            dict.__setitem__(arr[i],True)
            dict.__setitem__(arr[i+step],True)
            dict.__setitem__(arr[i+step*2],True)

            if list(dict.values()).count(True) == 3:
                sum = sum + 1

    print(rNum*gNum*bNum - sum)



calculate(N, S)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
S=input()
r = len([x for x in S if x == "R"])
g = len([x for x in S if x == "G"])
b = len([x for x in S if x == "B"])
 
ans = r * g * b
 
for i in range(N):
  for k in [N - k_ - 1 for k_ in range(N)]:
    tar_j = i + k
    if (tar_j > 2 * i) & (tar_j < 2 * k):
      if np.mod(tar_j, 2) == 0:
        j = int(tar_j / 2)
        s = S[i] + S[j] + S[k]
        if ("R" in s) & ("G" in s) & ("B" in s):
          ans -= 1
    else:
      break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

sum = r*g*b

for i in range(N):
    for j in range(i+1, N):
        A = ['R','G','B']
        if S[i] == S[j]:
            continue
        
        A.remove(S[i])
        A.remove(S[j])
            
        # S[k]
        k = 2 * j - i
        if k < len(S):
            if S[k] == A[0]:
                sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
S =input()
#第一段階
R,G,B =0,0,0
for i in S:
    if i =="R": R +=1
    elif i == "G": G +=1
    elif i == "B": B +=1
con1 = R*G*B

#j-i = k-j　を引く
con2 =0
if N <=2: con2 =0
else:
    for i in range(0,N-2):
        for j in range(i+1, N-1):
            if j < 2*j - i <= N-1:
                if S[i] != S[2*j-i] and S[j] !=S[2*j-i] and S[i] != S[j]:
                    con2 +=1

print(con1-con2)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

def calculate(n, s):
    arr = list(s)

    rNum = arr.count("R")
    gNum = arr.count("G")
    bNum = arr.count("B")


    sum = 0

    for i in range(n):
        for j in range(i + 1, n):
            start = i
            middle = j
            end = i + (j - i) * 2
            if end > n - 1:
                break

            dict = {"R": False, "G": False, "B": False}
            dict.__setitem__(arr[start], True)
            dict.__setitem__(arr[middle], True)
            dict.__setitem__(arr[end], True)
            dictArr = np.array(list(dict.values()))
            if np.sum(dictArr == True) == 3:
                sum = sum + 1

    print(rNum*gNum*bNum - sum)


calculate(N, S)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

def calculate(n, s):
    arr = list(s)

    rNum = arr.count("R")
    gNum = arr.count("G")
    bNum = arr.count("B")


    sum = 0

    for i in range(n):
        for j in range(i + 1, n):
            start = i
            middle = j
            end = i + (j - i) * 2
            if end > n - 1:
                continue

            dict = {"R": False, "G": False, "B": False}
            dict.__setitem__(arr[start], True)
            dict.__setitem__(arr[middle], True)
            dict.__setitem__(arr[end], True)
            dictArr = np.array(list(dict.values()))
            if np.sum(dictArr == True) == 3:
                sum = sum + 1

    print(rNum*gNum*bNum - sum)


calculate(N, S)

Submission status: Time Limit Exceeded

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

i_range = [int(x) for x in range(1, N-1)]
j_range = [int(x) for x in range(2, N)]
k_range = [int(x) for x in range(3, N+1)]

val = 0

for k in k_range:
    S_k = S[k-1]
    for j in j_range:
        if(j >= k):
            break
        S_j = S[j-1]
        for i in i_range:
            if(i >= j):
                break
            S_i = S[i-1]
            if((S_i != S_j) and (S_j != S_k) and (S_i != S_k) and (j-i != k-j)):
                val = val + 1

print(val)

Submission status: Time Limit Exceeded

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

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

print(count)

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:
s = int(input())
n = input()
ans = 0
for a in range(s):
    p = n[a]
    for b in range(a+1, s):
        q = n[b]
        if p != q:
            for c in range(b+1, s):
                if (b - a) != (c - b):
                    r = n[c]
                    if p != r and q!= r:
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = int(input())
n = input()
ans = 0
for a in range(s):
    p = n[a]
    for b in range(a+1, s):
        q = n[b]
        if p != q:
            for c in range(b+1, s):
                r = n[c]
                if p != r and q!= r and (b - a) != (c - b):
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

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


ans = S.count('R') * S.count('G') * S.count('B')
for i,j in permutations(range(N), 2):
  if (i+j)%2 or i>=j: continue
  a = S[i]
  b = S[j]
  k = (i+j)//2
  if sorted([a,S[k],b]) == ['B','G','R']:
    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):
  a = S[i]
  for j in range(i+1,N):
    b = S[j]
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if sorted([a,S[k],b]) == ['B','G','R']:
      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):
  a = S[i]
  for j in range(i+1,N):
    b = S[j]
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if len(set([a,S[k],b])) == 3:
      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):
  for j in range(i+1,N):
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if len(set([S[i],S[k],S[j]])) == 3:
      ans -= 1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def function(s):
  ans = 0
  for i in range(len(s) - 2):
    for j in range(i + 1, len(s) - 1):
      if s[i] != s[j]:
        rest = "RGB".replace(s[i], "").replace(s[j], "")
        ans += s[j + 1:].count(rest)
        if 2 * j - i < len(s) and s[2 * j - i] == rest:
          ans -= 1
  return ans
 
n = input()
s = input()
 
print(function(s))

Submission status: Time Limit Exceeded

Solution code:
N=range(int(input()))
S=input()
ans=0
for x in N:
  for y in N:
    for z in N:
      if x-y!=y-z and x<y<z:
        if S[x]!=S[y]!=S[z]!=S[x]:
          ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=range(int(input()))
S=input()
ans=0
for x in N:
  for y in N:
    for z in N:
      if x-y!=y-z and x<y and y<z:
        p,q,r=S[x],S[y],S[z]
        if p!=q and q!=r and r!=p:
          ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for x in range(2,N):
  for y in range(1,x):
    for z in range(0,y):
      if x-y!=y-z:
        p,q,r=S[x],S[y],S[z]
        if p!=q and q!=r and r!=p:
          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+1,n-1):
        for k in range(j+1,n):
            if s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
                if j-i == k-j:
                    ans -= 1
                    
print(ans)

Submission status: Time Limit Exceeded

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

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

Ans = L[0]*L[1]*L[2]

for i in range(N):
  s = S[i]
  for j in range(i+1,N):
    t = S[j]
    if s != t:
      k = 2*j-i
      if k <= N-1:
        u = S[k]
        if t != u and u != s:
          Ans -=1
print(Ans)

Submission status: Time Limit Exceeded

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

ans = 0
for a in range(0,n-2):
    for b in range(a,n-1):
        e = b-a
        #if s[a]!=s[b]:
        for c in range(b,n):
            f = c-b
            if e!=f:
                if s[a]!=s[c] and s[b]!=s[c] and s[a]!=s[b]:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for a in range(0,n-2):
    for b in range(a,n-1):
        e = b-a
        if s[a]!=s[b]:
            for c in range(b,n):
                f = c-b
                if e!=f:
                    if s[a]!=s[c] and s[b]!=s[c]:
                        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
string = str(input())

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int( input() )
S = input()
 
def count( first, second, third ):
  i = 0
  ret = 0
  for c in S:
    if c == first:
      S1 = S[ i + 1: ]
      j = 0
      for d in S1:
        if d == second:
          S2 = S1[ j + 1: ]
          if j < len( S1 ):
            S2 = S2[ :j ] + S2[ j + 1 :]
          ret += S2.count(third)
        j += 1
    i += 1
  return ret
 
ans = 0
ans += count('R','G','B')
ans += count('R','B','G')
ans += count('G','R','B')
ans += count('G','B','R')
ans += count('B','R','G')
ans += count('B','G','R')
print( ans )

Submission status: Time Limit Exceeded

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

def count( first, second, third ):
  i = 0
  ret = 0
  for c in S:
    if c == first:
      S1 = S[ i + 1: ]
      j = 0
      for d in S1:
        if d == second:
          S2 = S1[ j + 1: ]
          if j < len( S1 ):
            S2 = S2[ :j ] + S2[ j + 1 :]
          ret += S2.count(third)
        j += 1
    i += 1
  return ret

ans = 0
ans += count('R','G','B')
ans += count('R','B','G')
ans += count('G','R','B')
ans += count('G','B','R')
ans += count('B','R','G')
ans += count('B','G','R')
print( ans )

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
string = str(input())

def check_rgb(si, sj, sk):
    if(si != sj) and (sk != si) and (sj != sk):
        return 1
    return 0

ans = 0
for i in range(num - 2):
    for j in range(i + 1, num - 1):
        for k in range(j  + 1, num):
            if( j - i != k - j):
                ans += check_rgb(string[i], string[j], string[k])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
result = 0
for i in range(n-3):#ラスト３文字はありえない:
    s_i = s[i]
    
    for j in range(i+1,n-1):
        t = ["R", "G", "B"]
        
        s_j = s[j]
        if s_i == s_j:
            continue
        dist = j-i
        t.remove(s_j)
        t.remove(s_i)
            
        k = j+1
        result += s[k:].count(t[0])
        if j + j - i < len(s):
            if s[j + j - i] == t[0]:
                result -= 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
#input
N = int(input())
S = str(input())
 
#output
from collections import Counter
answer = 0
if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = Counter(S[:i])["G"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["G"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["G"]
            c = Counter(S[:i])["G"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1
 
print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
result = 0
for i in range(n-3):#ラスト３文字はありえない:
    s_i = s[i]
    for j in range(i+1,n-1):
        s_j = s[j]
        dist = j-i
        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 dist != k-j:
                result += 1

print(result)

Submission status: Time Limit Exceeded

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

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

ng=0

all=r*g*b

#NGパターン数の洗い出し
for i in range(0,n-2):
  for j in range(i,n-1):
    if s[i]==s[j]:
      continue
    for k in range(j,n):
      if j-i==k-j:
        if s[j]!=s[k] and s[k]!=s[i]:
          ng+=1
print(all-ng)

Submission status: Time Limit Exceeded

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

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

ng=0

all=r*g*b

#NGパターン数の洗い出し
for i in range(0,n-2):
  for j in range(i,n-1):
    for k in range(j,n):
#      print(i,j,k)
      if j-i==k-j:
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
          ng+=1
print(all-ng)