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 = list(str(input()))
from collections import Counter
from functools import lru_cache

@lru_cache(maxsize=1000)
def solve(idx, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(S[idx:])
        if len(S)>dis:
            if S[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        for i, j in enumerate(S[idx:]):
            if j==p:
                ans += solve(i+idx, (q,), i*2+idx)
            elif j==q:
                ans += solve(i+idx, (p,), i*2+idx)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(S):
            if j==p:
                ans += solve(i, (q, r), i)
            elif j==q:
                ans += solve(i, (p, r), i) 
            elif j==r:
                ans += solve(i, (p, q), i) 
        return ans

print(solve(0, ('R', 'G', 'B'), 0))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# D - RGB Triplets
# 標準入力の取得
N = int(input())
S = input()

# 求解処理
def get_S_k(S_i, S_j):
    S_k = "RGB"
    return S_k.replace(S_i, "").replace(S_j, "")

result = 0
size = len(S)
for i in range(size-2):
    S_i = S[i]
    for j in range(i+1, size-1):
        S_j = S[j]
        if S_i == S_j:
            continue
        S_k = get_S_k(S_i, S_j)
        result += S[j+1:].count(S_k)
        if (2*j - i < size) and (S[2*j-i] == S_k):
            result -= 1

# 結果出力
print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for i in range(n):
    si=s[i]
    for j in range(i+1,n):
        sj=s[j]
        tmp=j-i
        if si==sj:
            pass
        else:
            for k in range(j+1,n):
                sk=s[k]
                if tmp==k-j:
                    continue
                if sk==si or sk==sj:
                    continue
                ans+=1
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(i+1,N+1):
    for k in range(j+1,N+1):
      if (j-i)!=(k-j):
        if S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1]:
          counter=counter+1
print(counter)

Submission status: Time Limit Exceeded

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[]
g=[]
b=[]
for i in range(N):
    r.append(S[i:].count('R'))
    g.append(S[i:].count('G'))
    b.append(S[i:].count('B'))
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
All = [i for i in range(1,N+1)]
R = []
G = []
B = []
for i in range(N):
    if S[i] == 'R':
        R.append(i+1)
    elif S[i] == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
ans = 0
count = 0
for k in R:
    for j in G:
        for i in B:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j - 1 == j - i - 1:
                fr += 1

            if fr == 0:
                ans += 1

for k in R:
    for j in B:
        for i in G:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j == j - i:
                fr += 1

            if fr == 0:
                ans += 1

for k in G:
    for j in R:
        for i in B:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j - 1 == j - i - 1:
                fr += 1

            if fr == 0:
                ans += 1

for k in G:
    for j in B:
        for i in R:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j - 1 == j - i - 1:
                fr += 1

            if fr == 0:
                ans += 1

for k in B:
    for j in R:
        for i in G:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j - 1 == j - i - 1:
                fr += 1

            if fr == 0:
                ans += 1

for k in B:
    for j in G:
        for i in R:
            fr = 0
            if k < j:
                fr += 1
            if j < i:
                fr += 1
            if k - j - 1 == j - i - 1:
                fr += 1

            if fr == 0:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

count = 0

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

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):
            for k in range(j, N):
                if j-i == k-j:
                    continue
                elif S[i]==S[j] or S[i]==S[k] or S[j]==S[k]:
                    continue
                else:
                    c = c+1

    print(c)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

def split(s):
    r = []
    g = []
    b = []
    for i in range(len(s)):
        if 'R' == s[i]:
            r.append(i)
        elif 'G' == s[i]:
            g.append(i)
        elif 'B' == s[i]:
            b.append(i)
    return { 'R':r, 'G':g, 'B':b }

def f(s):
    for key in 'RGB':
        if key not in (s):
            return key

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

    rgb = split(s)
    count = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            if s[i] == s[j]:
                continue
            color = f(s[i] + s[j])
            count += len(rgb[color]) - bisect.bisect_left(rgb[color], j+1)
            if (j + (j - i)) in rgb[color]:
                count -= 1
    print(count)

main()

Submission status: Time Limit Exceeded

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

count = 0

for first_index in range(1, N+1):
  first = S[first_index-1]
  for second_index in range(first_index, N+1):
    second = S[second_index-1]
    for third_index in range(second_index, N+1):
      third = S[third_index - 1]
      # print(first,second,third)
      if first != second and second != third and first != third and second_index - first_index != third_index-second_index:
        count += 1
      # print(count)

print(count)

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 = []
g_inds = []
b_inds = []
for i,_x in enumerate(s):
  if _x == "R":
    r_inds.append(i)
  elif _x == "G":
    g_inds.append(i)
  else:
    b_inds.append(i)
  
#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
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[str(i) for i  in input()]
count=0

for i in range(n):
    for j in range(i+1,n):
        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):
                    count =count+1
print(count)

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+1)
    elif S[i] == "G":
        G.append(i+1)
    else:
        B.append(i+1)
        
ans = 0
for r in R:
    for g in G:
        for b in B:
            i = min(r, g, b)
            k = max(r, g, b)
            j = r+g+b-i-k
            if j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1

    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N):
            Sj = S[j]
            if Si != Sj:
                if len(set([Si, Sj])^set(["R", "G"])) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(set([Si, Sj])^set(["G", "B"])) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(set([Si, Sj])^set(["R", "B"])) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    rest_r = []
    rest_g = []
    rest_b = []

    c_r = 0
    c_g = 0
    c_b = 0

    i_r = []
    i_g = []
    i_b = []

    for i in range(N-1, -1, -1):
        if S[i] == 'R':
            c_r+=1
            i_r.append(i)
        if S[i] == 'G':
            c_g+=1
            i_g.append(i)
        if S[i] == 'B':
            c_b+=1
            i_b.append(i)
        rest_r.append(c_r)
        rest_g.append(c_g)
        rest_b.append(c_b)

    rest_r.reverse()
    rest_g.reverse()
    rest_b.reverse()

    i_r.reverse()
    i_g.reverse()
    i_b.reverse()

    sum = 0

    # r, g, b
    for r in i_r:
        for g in i_g:
            i_max = max(r, g)
            sum += rest_b[i_max]
            if (i_max + abs(g - r)) in i_b:
                sum -= 1

    for r in i_r:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_g[i_max]
            if (i_max + abs(g - r)) in i_g:
                sum -= 1

    for r in i_g:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_r[i_max]
            if (i_max + abs(g - r)) in i_r:
                sum -= 1

    print(sum)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

sum = 0
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)
for x in R_list:
	for y in G_list:
		for z in B_list:
			tmp = []
			tmp.extend([x,y,z])
			tmp.sort()
			if tmp[1]-tmp[0]!=tmp[2]-tmp[1]:
				sum+=1
print(sum)

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)

t=0
k=0
A=len(r)
B=len(g)
for i in range(A):
    for j in range(B):

        if 2*r[i]-g[j] in b:
            k+=1
        if 2*g[j]-r[i] in b:
            k+=1
        if (r[i]+g[j])/2 in b:
            k+=1


print(A*B*len(b)-k)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = [False]*n
g = [False]*n
b = [False]*n
for i in range(len((s))):
    if s[i] == 'R':
        r[i] = True
    elif s[i] == 'G':
        g[i] = True
    else:
        b[i] = True
count = 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            if r[i] and g[j] and b[k] and j - i != k - j:
                count += 1
            if r[i] and g[k] and b[j] and j - i != k - j:
                count += 1
            if r[j] and g[i] and b[k] and j - i != k - j:
                count += 1
            if r[j] and g[k] and b[i] and j - i != k - j:
                count += 1
            if r[k] and g[i] and b[j] and j - i != k - j:
                count += 1
            if r[k] and g[j] and b[i] and j - i != k - j:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = [i for i, x in enumerate(s) if x == 'R']
g = [i for i, x in enumerate(s) if x == 'G']
b = [i for i, x in enumerate(s) if x == 'B']
ans = len(r)*len(g)*len(b)
num = 0
for i in r:
    for j in g:
        for k in b:
            if ((i+j)/2 == k) | ((j+k)/2 == i) | ((k+i)/2 == j):
                num += 1
print(ans-num)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(n):
    a = s[i]
    for j in range(i+1, n):
        b = s[j]
        if a == b:
            continue
        for k in range(j+1, n):
            c = s[k]
            if a == b or b == c or c == a or i + k == 2 * j:
                continue
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
rr=[]
gg=[]
bb=[]
for i in range(n):
  if s[i]=='R':
    rr.append(i)
  elif s[i]=='G':
    gg.append(i)
  else:
    bb.append(i)
lr=len(rr)
lg=len(gg)
lb=len(bb)
ans=lr*lg*lb
if ans==0:
  print(0)
  exit()
#print(ans,lr,lg,lb)
for x in rr:
  for y in gg:
    for z in bb:
      su=x+y+z
      xx,zz=min(x,y,z),max(x,y,z)
      yy=su-xx-zz
      if zz-yy==yy-xx:
        ans-=1




print(ans)

Submission status: Time Limit Exceeded

Solution code:

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(range(0, len(s)), range(i, len(s)), range(j, len(s))):
for i in range(0, len(s)):
    for j in range(i, len(s)):
        for k in range(j, len(s)):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                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

r_index = []
g_index = []
b_index = []

for idx in range(n):
    if s[idx] == "R":
        r_index.append(idx+1)
    elif s[idx] == "G":
        g_index.append(idx+1)
    else:
        b_index.append(idx+1)

count = len(r_index) * len(g_index) * len(b_index)
for r_idx in r_index:
    for g_idx in g_index:
        for b_idx in b_index:
          num_list = [r_idx, g_idx, b_idx]
          num_list.sort()

          i, j, k = map(int, num_list)
          if (j-i) == (k-j):
              count -= 1
            


# for r_idx in r_index:
#     for g_idx in g_index:
#         for b_idx in b_index:
#           num_list = [r_idx, g_idx, b_idx]
#           num_list = sorted(num_list)
#           i, j, k = map(int, num_list)
#           if (j-i) != (k-j):
#               count += 1
              

# 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]:
                    # count += 1
                    # print(i, j, k)

print(count)

Submission status: Time Limit Exceeded

Solution code:
# 入力は二行でそれぞれ[数値]

input_nums = int(input())
input_line = input()

result = 0

for k in range(input_nums):
    for j in range(k):
        for i in range(j):
            if(j-i == k-j):
                pass
            elif(input_line[i] != input_line[j] and input_line[i] != input_line[k] and input_line[j] != input_line[k]):
                    result = result + 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
# Pypy3のほうがTLE数少なかった、速そう

import itertools
import sys
input = sys.stdin.readline  # 改行コード「\n」を呼んでしまうため、文字数など、注意！！

n = int(input().replace("\n",""))
s = input().replace("\n","")

ans = 0

for i in range(n):
    for j in range(i+1, n):
        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):
                    ans += 1


"""
for i in range(n):
    chknum = []
    chknum.append(i)
    for j in range(i+1, n):
        if s[i] != s[j]:
            chknum.append(j)
            for k in range(j+1, n):
                if (s[i] != s[k]) and (s[j] != s[k]) and (j-i != k-j):
                    ans += 1
"""

"""
for i, j, k in itertools.product(range(n), range(n), range(n)):
    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):
        ans += 1
"""

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = II()
S = input()

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R_indices = []
G_indices = []
B_indices = []
for i, s in enumerate(S):
    if s == 'R':
        R_indices.append(i)
    elif s == 'G':
        G_indices.append(i)
    elif s == 'B':
        B_indices.append(i)

count = len(R_indices) * len(G_indices) * len(B_indices)

for r in R_indices:
    for g in G_indices:
        for b in B_indices:
            i, j ,k = sorted([r, g, b])
            if j - i == k - j:
                count -=  1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    r = 0
    s = [ord(l) for l in s]
    for k in range(n):
        sk = s[k]
        for j in range(k):
            sj = s[j]
            if sj != sk:
                for i in range(j):
                    si = s[i]
                    if j - i != k - j and si != sj and si != sk:
                        r += 1
    print(r)


main()

Submission status: Time Limit Exceeded

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

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    if r < g: x, y = r, g
    else: x, y = g, r
    ans += len(B)
    if x*2 - y >= 0:
      if x*2 - y in B: ans -= 1
    if (x + y) % 2 == 0:
      if (x+y)//2 in B: ans -= 1
    if y*2 - x in B: ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets

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

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

ans = 0
for r in R:
    for g in G:
        for b in B:
            tmp = [r, g, b]
            tmp.sort()
            if (tmp[1]-tmp[0])==(tmp[2]-tmp[1]):
                continue
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

total = 0
for i in range(N) :
    for j in range(N) :
        if (i >= j) :
            continue

        if (S[i] == S[j]) :
            continue

        for k in range(N) :
            if (j >= k) :
                continue

            if (S[i] == S[k] or S[j] == S[k]) :
                continue

            if (j-i == k-j) :
                continue
            total += 1

print(total)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    if s[i] == 'R':
        R_index.append(i)
    elif s[i] == 'G':
        G_index.append(i)
    elif s[i] == 'B':
        B_index.append(i)

ans = 0
# print(R_index)
# print(G_index)
# print(B_index)

for i in R_index:
    for j in G_index:
        for k in B_index:
            temp_i = min(i,j,k)
            temp_k = max(i,j,k)
            temp_j = i + j + k - temp_i - temp_k
            if temp_k - temp_j == temp_j - temp_i:
                continue
            ans += 1


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 = []

    for i in range(n):
        if s[i] == "R":
            R.append(i + 1)
        elif s[i] == "G":
            G.append(i + 1)
        else:
            B.append(i + 1)
    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
    print(res)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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)

ans = len(R)*len(G)*len(B)
cnt = 0
for r in R:
    for g in G:
        if 2*r - g in B:
            cnt += 1
        if 2*g - r in B:
            cnt += 1
        if g>r:
            if (g-r)%2==0 and r+(g-r)//2 in B:  
                cnt += 1
        if r>g:
            if (r-g)%2==0 and g+(r-g)//2 in B:
                cnt += 1
print(ans - cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

if __name__ == "__main__":
	N = int(input())
	S = tuple(input())

	sum = 0

	for c in combinations(range(N),3):
		if (S[c[0]] != S[c[1]]) and (S[c[0]] != S[c[2]]) and (S[c[1]] != S[c[2]]):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            for a in range(len(L[i])):
                for b in range(len(L[j])):
                    if L[i][a] <= L[j][b]:
                        for c in range(len(L[k])):
                            if L[j][b] <= L[k][c] and L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

r = {}
b = {}
g = {}
for i in range(len(s)):
    if s[i] == 'R':
        r[i] = s[i]
    elif s[i] == 'B':
        b[i] = s[i]
    else:
        g[i] = s[i]

ans = len(r) * len(b) * len(g)

nums = list(itertools.product(r.keys(), b.keys(), g.keys()))

for n in nums:
    num = list(n)
    num.sort()
    if num[2] - num[1] == num[1] - num[0]:
        ans -= 1

#print(r.keys())
#print(b.keys())
##print(g.keys())
#for i in r.keys():
#    for j in b.keys():
#        for k in g.keys():
#            if abs(k -j) == abs(j -i) or abs(k-i) == abs(i-j) or abs(k-j) == abs(k-i) :
#                ans -= 1


print(ans)

Submission status: Time Limit Exceeded

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

cnt = 0
l = ["R", "G", "B"]
for i in range(len(S)-2):
    for j in range(i, len(S)):
        for k in range(j, len(S)):
            s = [S[i], S[j], S[k]]
            if set(s) == set(l):
                if j-i != k-j:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

R_list = []
G_list = []
B_list = []
pair_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)

for i in R_list:
    for j in G_list:
        for k in B_list:
            pair_list.append([i,j,k])
        
count = 0
for pair in pair_list:
    pair.sort()
    if((pair[1]-pair[0])==(pair[2]-pair[1])):
        continue
    else:
        count += 1

#print(R_list)
#print(G_list)
#print(B_list)
#print(pair_list)
print(count)

Submission status: Time Limit Exceeded

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

# process
R = []
G = []
B = []
for i, s in enumerate(list(S)):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)

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

# output
print(ans)

Submission status: Time Limit Exceeded

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

# def main(a=0, b=''):
def main():
    # 整数の入力
    a = int(input())
    # # 文字列の入力
    b = input()

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

def check(a,b,c):
  if j-i != k-j:
    if S[a] != S[b]:
      if S[a] != S[c]:
        if S[b] != S[c]:
          return 1
  return 0


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):
      ans += check(i,j,k)
print(ans)