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:
from collections import deque
N = int(input())
S = list(input())
R = deque()
G = deque()
B = deque()
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    elif S[i] == "B":
        B.append(i)
count = 0


for a in R:
    for b in G:
        for c in B:
            num = [a, b, c]
            num.sort()
            if num[1] - num[0] != num[2] - num[1]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

ans = 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):
      #print(i, j, k)
      if s[i] == s[k] or s[j] == s[k]:continue
      if j-i == k-j:continue
      ans += 1      

print(ans)

Submission status: Time Limit Exceeded

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

# def main(a=0, b=''):
def main():
    # 整数の入力
    a = int(input())
    # a = int(a)
    # スペース区切りの整数の入力
    # b, c = map(int, input().split())
    # b, c = map(int, input())

    # # 文字列の入力
    b = input()
    # 出力
    # print("{} {}".format(a+b+c, s))
    count = 0
    for i in range(a):
        for j in range(i, a):
            if b[i] == b[j]:
                continue
            for k in range(j, a):
                if j - i == k - j:
                    continue
                if b[j]==b[k] or b[i]==b[k]:
                    continue
                count += 1
    print(count)


if __name__ == '__main__':
    # main(4, 'RRGB')
    # main(39, 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB')
    main()

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
s = input()
r = []
g = []
b = []

for i in range(0, len(s)):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    elif s[i] == "B":
        b.append(i+1)

cnt = 0
a_min = 0
a_mid = 0
a_max = 0

# forループのネスト
for i, j, k in itertools.product(r, g, b):

    a_min = min(i, j, k)
    a_max = max(i, j, k)
    if i != a_max and i != a_min:
        a_mid = i
    elif j != a_max and j != a_min:
        a_mid = j
    elif k != a_max and k != a_min:
        a_mid = k

    if a_max-a_mid != a_mid-a_min:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0,n-3):
  ii = s[i]
  for j in range(i+1, n-1):
    jj = s[j]
    for k in range(j+1,n):
      kk=s[k]
      if j-i != k-j:
        if ii != jj and jj != kk and kk!=ii:
          count = count + 1
print(count)

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            #print(i,j,k)
            a = j - i
            b = k - j
            if a != b:
                string = S[i] + S[j] + S[k]
                #print(string)
                if "R" in string  and "G" in string and "B" in string:
                    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*2)
    elif s[i]=="G":
        g.append(i*2)
    else:
        b.append(i*2)
c=len(r)*len(g)*len(b)
for rr in r:
    for gg in g:
        if (rr+gg)/2 in b:
            c-=1
        if rr*2-gg in b:
            c-=1
        if gg*2-rr in b:
            c-=1
print(c)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()
ans = 0
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
for i in range(N):
    for j in range(i+1, N):
        if S[i] == 'R' and S[j] =='G':
            ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'R' and S[j] =='B':
            ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='R':
            ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='B':
            ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='R':
            ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='G':
            ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        # for k in range(j+1, N):
        #     if j-i == k-j:
        #         continue
        #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        #         ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
# 整数の入力
n = int(input())
# 文字列の入力
s = input()
# 数え上げ
count = 0
for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] and j-i != k-j:
                count +=1
print(count)

Submission status: Time Limit Exceeded

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

for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      if ((S[i] == "R") & (S[j] == "G"))|((S[i] == "G") & (S[j] == "R")): 
        ans = ans + S.count("B", j)
        try:
          if S[j+(j-i)] == "B":
            ans = ans -1
        except:
          None
    if ((S[i] == "R") & (S[j] == "B"))|((S[i] == "B") & (S[j] == "R")): 
        ans = ans + S.count("G", j)
        try:
          if S[j+(j-i)] == "G":
            ans = ans -1
        except:
          None
    if ((S[i] == "G") & (S[j] == "B"))|((S[i] == "B") & (S[j] == "G")): 
        ans = ans + S.count("R", j)
        try:
          if S[j+(j-i)] == "R":
            ans = ans -1
        except:
          None
 

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      if i<j<k:
        if S[i-1]!=S[j-1]:
          if S[j-1]!=S[k-1]:
            if S[k-1]!=S[i-1]:
              if (j-i)!=(k-j):
                counter=counter+1
print(counter)

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
ans = 0

for i, x in enumerate(S):
  if x == 'R':
    R.append(i)
  elif x == 'G':
    G.append(i)
  else:
    B.append(i)
    
for r in R:
  for g in G:
    for b in B:
      lis = sorted([r, g, b])
      if lis[1] - lis[0] != lis[2] - lis[1]:
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

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

ridx = [i for i, s in enumerate(S, 1) if s == 'R']
gidx = [i for i, s in enumerate(S, 1) if s == 'G']
bidx = [i for i, s in enumerate(S, 1) if s == 'B']

ans = 0

def count(I, J, K):
    rtn = 0
    for i in I:
        for j in J:
            if j > i:
                for k in K:
                    if k > j:
                        if j-i != k-j:
                            rtn += 1
    return rtn


ans += count(ridx, gidx, bidx)
ans += count(ridx, bidx, gidx)
ans += count(gidx, bidx, ridx)
ans += count(gidx, ridx, bidx)
ans += count(bidx, ridx, gidx)
ans += count(bidx, gidx, ridx)

print(ans)

Submission status: Time Limit Exceeded

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

r_index = [i for i, x in enumerate(s) if x == 'R']
g_index = [i for i, x in enumerate(s) if x == 'G']
b_index = [i for i, x in enumerate(s) if x == 'B']

tap = []
tap.append(itertools.product(r_index, g_index, b_index))
tap.append(itertools.product(r_index, b_index, g_index))
tap.append(itertools.product(g_index, r_index, b_index))
tap.append(itertools.product(g_index, b_index, r_index))
tap.append(itertools.product(b_index, r_index, g_index))
tap.append(itertools.product(b_index, g_index, r_index))

ans = []
for i in tap:
    for j in i:
        if j[1] - j [0] < 0 or j[2] - j[1] < 0:
            continue
        if j[1] - j[0] != j[2] - j[1]:
            ans.append(j)
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Sl = list(map(str, input()))
cnt = 0
for i in range(N-1):
    for m in range(N-i):
        for n in range(N-i-m):
            if (Sl[i]!=Sl[i+m] and Sl[i]!=Sl[i+m+n] and Sl[i+m]!=Sl[i+m+n] and m!=n):
                cnt += 1
print(cnt)

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
ans = 0
for i in range(N):
    if S[i] == "R": R.append(i)
    elif S[i] == "G": G.append(i)
    else: B.append(i)
for i in R:
    for j in G:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,B)))
    for j in B:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,G)))
for i in G:
    for j in R:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,B)))
    for j in B:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,R)))
for i in B:
    for j in R:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,G)))
    for j in G:
        if i < j:
            ans += len(list(filter(lambda x:j < x and j - i != x - j,R)))
print(ans)

Submission status: Time Limit Exceeded

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

rlist, glist, blist = [], [], []
for i in range(N):
    if S[i] == "R":
        rlist.append(i)
    if S[i] == "G":
        glist.append(i)
    if S[i] == "B":
        blist.append(i)
#print(rlist)

rnumber = len(rlist)
gnumber = len(glist)
bnumber = len(blist)
total = rnumber * gnumber * bnumber
#print(total)

overcount = 0
for r in rlist:
    for g in glist:
        for b in blist:
            options = sorted([r,g,b])
            if options[0]+options[2] == 2*options[1]:
                overcount += 1
    
print(total - overcount)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
for i in range(2001):
    for j in range(n-2):
        if j + (2*i) <= n-1:
            if s[j] != s[j+i] and s[j+i] != s[j+(2*i)] and s[j+(2*i)] != s[j]:
                total -= 1
print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cum_r = [0] * (N + 1)  # cum_r[i] = i文字目までに含まれるRの数（i = 0, .., N)
cum_g = [0] * (N + 1)
cum_b = [0] * (N + 1)

for i in range(1, N + 1):
    cum_r[i] = cum_r[i - 1] + int(S[i - 1] == 'R')
    cum_g[i] = cum_g[i - 1] + int(S[i - 1] == 'G')
    cum_b[i] = cum_b[i - 1] + int(S[i - 1] == 'B')


dp = [0] * (N + 1)
for i in range(3, N + 1):
    dp[i] = dp[i - 1]
    for j in range(1, i - 1):
        if S[i - 1 - j] == S[i - 1]: continue

        if S[i - 1] == 'R':
            if S[i - 1 - j] == 'G':
                dp[i] += cum_b[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'B':
                    dp[i] -= 1
            else: # B
                dp[i] += cum_g[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'G':
                    dp[i] -= 1

        elif S[i - 1] == 'G':
            if S[i - 1 - j] == 'R':
                dp[i] += cum_b[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'B':
                    dp[i] -= 1
            else: # B
                dp[i] += cum_r[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'R':
                    dp[i] -= 1
        else:  # B
            if S[i - 1 - j] == 'R':
                dp[i] += cum_g[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'G':
                    dp[i] -= 1
            else: # G
                dp[i] += cum_r[i - 1 - j]
                if i - 1 - 2 * j >= 0 and S[i - 1 - 2 * j] == 'R':
                    dp[i] -= 1
print(dp[N])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
N = int(input())
S = input()
X = {"R": [], "G": [], "B": []}
Xset = {"R": set(), "G": set(), "B": set()}
for i, c in enumerate(S):
    X[c].append(i)
    Xset[c].add(i)

ans = 0
for i in range(N - 2):
    si = S[i]
    for j in range(i + 1, N - 1):
        sj = S[j]
        if si == sj:
            continue
        x = ["R", "G", "B"]
        x.remove(si)
        x.remove(sj)
        sk = x[0]
        ks = X[sk]
        count = len(ks) - bisect_left(ks, j)
        if 2 * j - i in Xset[sk]:
            count -= 1
        ans += count
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(i, n):
        if s[i] == s[j]:
            continue
        for k in range(j, n):
            if s[j] == s[k] or s[i] == s[k] or j-i == k-j:
                continue

            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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

if min([lr, lg, lb]) == lb:
    r, b = b, r

for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and a in b:
            ans -= 1
        if 0 <= c and c <= n-1 and c in b:
            ans -= 1
        if (i + j) % 2 == 0 and (i + j) / 2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    ans=0
    for i in range(n):
        if s[i]=='R':
            r.append(i)
        if s[i]=='G':
            g.append(i)
        if s[i]=='B':
            b.append(i)
    for i in r:
        for j in g:
            for k in b:
                if k+i!=2*j and i+j!=2*k and j+k!=2*i:
                    ans+=1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if n<=3:
    print(0)
    exit()
r=[]
g=[]
b=[]
for i,c in enumerate(s):
    if c=='R':
        r.append(i)
    elif c=='G':
        b.append(i)
    else:
        g.append(i)
ans=0
for i in r:
    for j in g:
        t1=-1
        if abs(i-j)%2==0:
            t1=min(i,j)+abs(i-j)//2
        t2=max(i,j)+abs(i-j)
        t3=min(i,j)-abs(i-j)
        for k in b:
            if k!=t1 and k!=t2 and k!=t3:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
c=s.count
r,g,b=c('R'),c('G'),c('B')
t=0
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j*2-i
    if k<n:
      if s[i]+s[j]+s[k] in ('RGB','RBG','GRB','GBR','BRG','BGR'):
        t+=1
print(r*g*b-t)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
  return [i for i, _x in enumerate(l) if _x == x]
    
r_inds = my_index_multi(s, "R")
g_inds = my_index_multi(s, "G")
b_inds = my_index_multi(s, "B")

ans = 0
#for r in r_inds:
#  for g in g_inds:
#    for b in b_inds:
#      x = sorted([r, g, b])
#      if x[1]-x[0] != x[2]-x[1]:
#        ans += 1
        
import itertools
for x in itertools.product(r_inds, g_inds, b_inds):
  x2 = sorted(x)
  if x2[1]-x2[0] != x2[2]-x2[1]:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cr = S.count('R')
cg = S.count('G')
cb = S.count('B')
ans = cr*cg*cb
#cr*cg*cbからj-i == k-jとなるijkの組みの数を引く
for i in range(N-2):
    for j in range(i+1,N-1):
        num = j-i
        if S[i] != S[j]:
            for k in range(j+1,N):
                if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                    if num == k - j:
                        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
s = list(input())
num = list(range(1, n+1))

items = [item for item in itertools.combinations(s, 3)]
nums = [item for item in itertools.combinations(num, 3)]

ans = 0
for num, item in zip(nums, items):
  if len(set(item)) == 3 and (num[1] - num[0]) != (num[2] - num[1]):
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(str,input()))
cnt=0
for i in range(N-2):
    for j in range(1,N-1):
        for k in range(2,N):
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k] and (j-i)!=(k-j) and i<j<k<=N:
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
for j in range(N):
    original = "RGB"
    if S[j] in original:
        for i in range(j):
            obj = original.replace(S[j], "")
            if S[i] in obj:
                for k in range(j+1, N):
                    if (j-i) != (k-j):
                        obj_2 = obj.replace(S[i], "")
                        if S[k] == obj_2:
                        	ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()
    total = 0
    count_r = 0
    count_g = 0
    count_b = 0
    for i in range(N):
        ch = S[i]
        if ch == 'R':
            count_r += 1
        elif ch == 'G':
            count_g += 1
        else:
            count_b += 1
        for j in range(i + 1, N):
            if S[i] != S[j]:
                r, g = S[i], S[j]
                other = 'B'
                if 'G' != r and 'G' != g:
                    other = 'G'
                if 'R' != r and 'R' != g:
                    other = 'R'
                k = j - i + j
                if k < N and S[k] == other:
                    total -= 1
    print(total + count_r*count_b*count_g)

Submission status: Time Limit Exceeded

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

answer = s.count('R') * s.count('G') * s.count('B')

for i in range(n - 2):
  j = 0
  while((i + 1) + (j - i) * 2 <= n):
    if s[i] != s[j] and s[j] != s[(i + 1) + (j - i) * 2] and s[i] != s[(i + 1) + (j - i) * 2]:
      answer -= 1
      j += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# from collections import Counter, deque # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
        if S[i]=='R':
            R.append(i)
        if S[i]=='G':
            G.append(i)
        if S[i]=='B':
            B.append(i)

    L=list(product(R,G,B))
    cnt=0
    for i in L:
        i=sorted(list(i))
        if i[1]-i[0]==i[2]-i[1]:
            continue
        else:
            cnt+=1
    print(cnt)




resolve()

Submission status: Time Limit Exceeded

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

ans = 0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

remain = [[0,0,0]]
for i in range(n):
    r, g, b = remain[-1]
    if s[-i-1] == "R":
        r = r+1
    elif s[-i-1] == "G":
        g = g + 1
    elif s[-i-1] == "B":
        b = b + 1

    remain.append([r,g,b])

total = 0
for i in range(n-2):
    if s[i] == "R":
        for j in range(i+1, n):
            if s[j] == "G":
                total += remain[-j-1][2]
                try:
                    if s[i+((j-i)*2)]=="B":
                        total -= 1
                except:
                    pass
            elif s[j] == "B":
                total += remain[-j-1][1]
                try:
                    if s[i+((j-i)*2)]=="G":
                        total -= 1
                except:
                    pass
    elif s[i] == "G":
        for j in range(i+1, n):
            if s[j] == "B":
                total += remain[-j-1][0]
                try:
                    if s[i+((j-i)*2)]=="R":
                        total -= 1
                except:
                    pass
            elif s[j] == "R":
                total += remain[-j-1][2]
                try:
                    if s[i+((j-i)*2)]=="B":
                        total -= 1
                except:
                    pass
    elif s[i] == "B":
        for j in range(i+1, n):

            if s[j] == "R":
                total += remain[-j-1][1]
                try:
                    if s[i+((j-i)*2)]=="G":
                        total -= 1
                except:
                    pass
            elif s[j] == "G":
                total += remain[-j-1][0]
                try:
                    if s[i+((j-i)*2)]=="R":
                        total -= 1
                except:
                    pass
print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
answer=0
 
for i in range(N-2):
    for j in range(1,N-1):
        for k in range(2,N):
            d=j-i
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] and (k-j)!=d and i<j<k:
                answer+=1
                #print(i,j,k)
 
                    
print(answer)

Submission status: Time Limit Exceeded

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

count = 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] or S[j]==S[k] or S[i]==S[k]:
        continue
      elif j-i == k-j:
        continue
      else:
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

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

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

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

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

for i in range(n-2):
    if s[i] == "B":
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "R":
                        if j - i != k - j:
                            ans += 1
"""
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
ans=0
for k in range(len(s)):
    for j in range(k):
        for i in range(j):
            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:
n = int(input())
s = str(input())
res = 0

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

Submission status: Time Limit Exceeded

Solution code:
import re

def main():
    NumString = int(input())
    String = input()
    
    total_ans = 0
    for i in range(NumString):
        i_RGB = 'RGB'
        for j in range(i+1, NumString):
            j_RGB = re.sub(String[i], '', i_RGB)
            if String[i] == String[j]:
                continue

            k_RGB = re.sub(String[j], '', j_RGB)
            total_ans += String.count(k_RGB, j+1)
            if (j + (j-i)) < NumString and String[j + (j-i)] == k_RGB:
                total_ans -= 1
    
    print(total_ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
s=[str(c) for c in(S)]
a=0
for i in range(N):
  for j in range(i+1,N):
    for h in range(j+1,N):
      if s[i]!=s[j] and s[i]!=s[h] and s[j]!=s[h] and (h-j)!=(j-i):
        a+=1
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = "F"+s
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]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                  if j-i != k-j:
                      cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(1,(n-i)//2+1):
        if i+2*j <= n-1:
            tem = s[i]+s[i+j]+s[i+2*j]
            num_r = tem.count("R")
            num_g = tem.count("G")
            num_b = tem.count("B")
            if num_r == 1 and num_g == 1 and num_b == 1:
                minus += 1
            

print(sum - minus)