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:
import itertools

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

# R,G,Bのそれぞれのインデックスをリストで取得する
r_index = []
g_index = []
b_index = []
for i in range(len(S)):
  if S[i]=='R':
    r_index.append(i)
  elif S[i]=='G':
    g_index.append(i)
  else:
    b_index.append(i)

#　全組み合わせ(制約1)
rgb_num = len(r_index) * len(g_index) * len(b_index)

# 組み合わせを全探索して、制約2に当てはまるものをカウントする
ck = 0
for rgb in list(itertools.product(r_index, g_index, b_index)):
  rgb_list = list(rgb)
  rgb_list.sort()
  i, j, k = rgb_list

  if (j*2 -i -k)==0:
    ck += 1

print(rgb_num - ck)

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(0,N-2):
    for j in range(i+1,N-1):
        try:
            if S[i]!=S[j] and S[j]!=S[2*j-i] and S[i]!=S[2*j-i]:
                ans-=1
        except:
            continue
print(ans)

Submission status: Time Limit Exceeded

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

num_r = s.count("R")
num_g = s.count("G")
num_b = n - num_r - num_g
count_2 = 0
for x in range(1,int(n/2)+1):
    for i in range(n-2):
        # print(x)
        # print(i)
        # print()
        if (i + 2 * x < n and s[i] != s[i + x] and s[i + x] != s[i + 2 * x] and s[i] != s[i + 2 * x] ):
            count_2 += 1
            # print(x)
            # print(i)
            # print()

res = num_r * num_g * num_b - count_2
# print(num_r)
# print(num_g)
# print(num_b)
print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")
ans=R*G*B
for i in range(0,N-2):
    for j in range(i+1,N-1):
        try:
            if S[i]!=S[j] and S[j]!=S[2*j-i] and S[i]!=S[2*j-i]:
                ans-=1
        except:
            continue
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# R,G,Bのそれぞれのインデックスをリストで取得する
r_index = []
g_index = []
b_index = []
for i in range(len(S)):
  if S[i]=='R':
    r_index.append(i)
  elif S[i]=='G':
    g_index.append(i)
  else:
    b_index.append(i)

#　全組み合わせ(制約1)
rgb_num = len(r_index) * len(g_index) * len(b_index)

# 組み合わせを全探索して、制約2に当てはまるものをカウントする
ck = 0
for r in r_index:
  for g in g_index:
    for b in b_index:
      lis = [r, g, b]
      lis.sort()
      i, j, k = lis

      if (j*2 -i -k)==0:
        ck += 1

print(rgb_num - ck)

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

print(cnt)

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    if S[i]=='R':
        R[i]=1
    elif S[i]=='G':
        G[i]=1
    elif S[i]=='B':
        B[i]=1
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]:
                if j-i != k-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

print(res)

Submission status: Time Limit Exceeded

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

print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c = s.count('R') * s.count('G') * s.count('B')
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 s[j] == s[k] or s[k] == s[i]:
                continue
            if j - i == k - j:
                c -= 1
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n = int(input())

    # ord('B') == 66 (== 0 mod 3)
    # ord('R') == 82 (== 1 mod 3)
    # ord('G') == 71 (== 2 mod 3)
    s = [ord(c) % 3 for c in input()]

    accs = [[0] * (n + 1) for _ in range(3)]
    for i in range(3):
        for j in range(n):
            if i != s[j]:
                accs[i][j + 1] = accs[i][j]
            else:
                accs[i][j + 1] = accs[i][j] + 1

    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j]:
                continue
            residual = ({0, 1, 2} - {s[i], s[j]}).pop()
            ans += accs[residual][n] - accs[residual][j]
            if j + j - i + 1 <= n:
                ans -= accs[residual][j + j - i + 1] - accs[residual][j + j - i]
    return ans


print(solve())

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

lis = list(range(1, N+1))
c_list = list(itertools.combinations(lis, 3))
ans = 0
for c in c_list:
    i, j, k = c

    if (S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1]) and (j-i!=k-j):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

r_num = S.count("R")
g_num = S.count("G")
b_num = S.count("B")

count = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j * 2 - i
        if k >= N:
            continue
        si, sj, sk = S[i], S[j], S[k]
        if si != sj and sj != sk and sk != si:
            count += 1

print(r_num * g_num * b_num - count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    # N <= 4000
    # full search
    N = int(input())
    S = input()

    count = 0
    for i in range(N):
        first = S[i]
        for j in range(i+1, N):
            second = S[j]
            if first == second:
                continue
            else:
                for k in range(j+1, N):
                    third = S[k]
                    if (first == third
                        or second == third):
                            continue
                    else:
                        if j - i == k - j:
                            continue
                        else:
                            count += 1

    print(count)


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
R = 0
G = 0
B = 0
Z = 0
for i in range(N):
    if S[i] == 'R':
        R += 1
    if S[i] == 'G':
        G += 1
    if S[i] == 'B':
        B += 1

for i in range(N-1):
    for j in range(i+1, N):
        k = 2 * j - i
        if k > N-1:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            Z += 1
print(R*G*B-Z)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
R = s.count("R")
G = s.count("G")
B = s.count("B")
cnt = 0
for i in range(n-1):
    for j in range(i+1,n):
        if 2*j - i <= n - 1:
            k = 2*j - i
            if len(set([s[i],s[j],s[k]])) == 3:
                cnt += 1
print(R*G*B - cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count=0

i=0
while i < n:
  si = s[i]
  j = i + 1
  while j < n:
    sj = s[j]
    if si != sj:
      k = j + 1
      while k < n:
        sk = s[k]
        if si != sk and sj != sk and (j - i) != (k - j):
          count += 1
        k += 1
    j += 1
  i += 1
  
print(count)

Submission status: Time Limit Exceeded

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

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0

cnt = 0
for i in range(N):
    a = S[i]

    if a == 'R':
        r_cnt += 1
    elif a == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i):
        b = S[j]

        d = i - j
        if (a == 'R' and b == 'G') or (a == 'G' and b == 'R'):
            if j - 1 >=0 and B_lst[j - 1] > 0:
                cnt += B_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'B':
                    cnt -= 1

        elif (a == 'G' and b == 'B') or (a == 'B' and b == 'G'):
            if j - 1 >=0 and R_lst[j - 1] > 0:
                cnt += R_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'R':
                    cnt -= 1

        elif (a == 'B' and b == 'R') or (a == 'R' and b == 'B'):
            if j - 1 >= 0 and G_lst[j - 1] > 0:
                cnt += G_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'G':
                    cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from _collections import defaultdict

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

counted_dict = defaultdict(list)
for i in range(N):
    char = S[i]
    if char == "R":
        counted_dict["R"].append(i)
    elif char == "G":
        counted_dict["G"].append(i)
    else:
        counted_dict["B"].append(i)

count = 0
for x in range(len(counted_dict["R"])):
    r_pos = counted_dict["R"][x]
    for y in range(len(counted_dict["G"])):
        g_pos = counted_dict["G"][y]
        for z in range(len(counted_dict["B"])):
            b_pos = counted_dict["B"][z]
            colors_pos_list = [r_pos, g_pos, b_pos]
            colors_pos_list.sort()
            if not colors_pos_list[2] + colors_pos_list[0] == 2 * colors_pos_list[1]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
sum = 0
for i in range(N-1,2,-1):

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
sum=0
for k in range(n):
  for j in range(k):
    for i in range(j):
      if(((j-i)!=(k-j))&(s[i]!=s[j])&(s[j]!=s[k])&(s[k]!=s[i])):

        sum=sum+1
          
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
for s in range(N):
    if S[s] == 'R':
        r += 1
    elif S[s] == 'G':
        g += 1
    elif S[s] == 'B':
        b += 1

ans = (r*g*b)

for i in range(N):
    for k in range(N):
        j = int((i+k)/2)
        if k <= i or (i+k)%2 != 0 or j <= i or k <= j:
            continue
        if (S[i] != S[j]) and (S[j] != S[k]) and (S[i] != S[k]):
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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
if len(r) == 0 or len(g) == 0 or len(b) == 0:
	print (0)
	exit()
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r_list=[]
g_list=[]
b_list=[]
for i in range(len(s)):
    if s[i]=="R":
        r_list.append(i)
    elif s[i]=="G":
        g_list.append(i)
    elif s[i]=="B":
        b_list.append(i)

count=0
for i in range(len(r_list)):
    for j in range(len(g_list)):
        for k in range(len(b_list)):
            mx=max(r_list[i],g_list[j],b_list[k])
            mn=min([r_list[i],g_list[j],b_list[k]])
            middle=r_list[i]+g_list[j]+b_list[k]-mx-mn
            if middle-mn != mx-middle:
                count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y, z, a):
  if x==y and z==a:
    return 1
  return 0

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)
    
rgb = r * g * b

# Rが最小
for i in r0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'G', S[k], 'B')
      equal += func2(S[j], 'B', S[k], 'G')

# Gが最小
for i in g0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'R', S[k], 'B')
      equal += func2(S[j], 'B', S[k], 'R')

# Bが最小
for i in b0:
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[j], 'R', S[k], 'G')
      equal += func2(S[j], 'G', S[k], 'R')

print(rgb - equal)

Submission status: Time Limit Exceeded

Solution code:
def func1(x, y):
  if x == y:
    return 1 
  return 0

def func2(x, y, z):
  if x=='R' and y=='G' and z=='B':
    return 1
  return 0

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0

for i in range(N):
  r += func1(S[i], 'R')
  g += func1(S[i], 'G')
  b += func1(S[i], 'B')
    
rgb = r * g * b

for i in range(0, N-2):
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k <= N-1:
      equal += func2(S[i], S[j], S[k])
      equal += func2(S[i], S[k], S[j])
      equal += func2(S[j], S[i], S[k])
      equal += func2(S[j], S[k], S[i])
      equal += func2(S[k], S[i], S[j])
      equal += func2(S[k], S[j], S[i])

print(rgb - equal)

Submission status: Time Limit Exceeded

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

c = 0

for i in range(n-1):
  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[k] != s[i]):
          c+=1
print(c)

Submission status: Time Limit Exceeded

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

c = 0

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

Submission status: Time Limit Exceeded

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

c = 0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
res = 0
for i in range(0, n):
    for j in range(i+1,n):
        if(j > n):
            break
        for k in range(j+1,n):
            if(k > n):
                break
            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:
def judge(x, y, z):
  if y-x != z-y and y>x and z>y:
    return 1
  
  return 0


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

count = 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]=='R' and S[j]=='G' and S[k]=='B':
        count += judge(i, j, k)
      elif S[i]=='R' and S[j]=='B' and S[k]=='G':
        count += judge(i, j, k)
      elif S[i]=='G' and S[j]=='R' and S[k]=='B':
        count += judge(i, j, k)
      elif S[i]=='G' and S[j]=='B' and S[k]=='R':
        count += judge(i, j, k)
      elif S[i]=='B' and S[j]=='R' and S[k]=='G':
        count += judge(i, j, k)
      elif S[i]=='B' and S[j]=='G' and S[k]=='R':
        count += judge(i, j, k)        
        
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r=[]
g=[]
b=[]
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
#print(r)
#print(g)
#print(b)
sum = len(r)*len(g)*len(b)

for i in r:
  for j in g:
    ma = max(i,j)
    mi = min(i,j)
    df = ma - mi
    k1 = mi - df
    k2 = ma + df
    k3 = (ma + mi) / 2
    if k1 in b:
      sum -=1
    if k2 in b:
      sum -=1
    if k3 in b:
      sum -=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = input()
r=[]
g=[]
b=[]
for i in range(n):
  #print(s[i])
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)

#print(r)
#print(g)
#print(b)
sum = 0
for i in r:
  for j in g:
    for k in b:
      wk = [i,j,k]
      wk.sort()
      mm = i + j + k
      if wk[1]*3 != mm :
        sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
word=input()
alound=word.count("R")*word.count("G")*word.count("B")
for i in range(num-2):
  for j in range(i+1,num-1):
    k=2*j-i
    if k<=num-1:
      if word[i]!=word[j] and word[i]!=word[k] and word[k]!=word[j]:
        alound=alound-1
print(alound)

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
word=input()
alound=word.count("R")*word.count("G")*word.count("B")
for i in range(num-2):
  for j in range(i+1,num-1):
    for k in range(j+1,num):
      if k-j==j-i and word[i]!=word[j] and word[i]!=word[k] and word[k]!=word[j]:
        alound=alound-1
print(alound)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
rl = []
gl = []
bl = []
i = 1
cnt = 0

for x in list(s):
    if x == 'R':
        rl.append(i)
    elif x == 'G':
        gl.append(i)
    elif x == 'B':
        bl.append(i)
    i += 1

for r in rl:
    for g in gl:
        for b in bl:
            i,j,k = sorted([r ,g, b])
            if j - i != k - j:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for k in range(N):
  for j in range(k):
    for i in range(j):
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i] and j-i != k-j:
        l += 1
      else:
        pass

print(l)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# N, Sをinput
# Sの'R', 'G', 'B'のインデックスを、リストr, g, bに格納
# r, g, bをそれぞれループさせる
# r, g, bの各要素の差を比較し、r-g!=g-bの場合、countに加算

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

r = []
g = []
b = []
count = 0

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)

for x in r:
  for y in g:
    for z in b:
      if x-y != y-z and x>y and y>z:
        count += 1
      if x-z != z-y and x>z and z>y:
        count += 1
      if y-x != x-z and y>x and x>z:
        count += 1
      if y-z != z-x and y>z and z>x:
        count += 1
      if z-x != x-y and z>x and x>y:
        count += 1
      if z-y != y-x and z>y and y>x:
        count += 1
        
print(count)

Submission status: Time Limit Exceeded

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

result = 0

for fst_index in range(n):
    for scd_index in range(fst_index + 1, n):
        if s[fst_index] == s[scd_index]:
            continue
        for trd_index in range(scd_index + 1, n):
            if s[trd_index] == s[fst_index] or s[trd_index] == s[scd_index] or \
               fst_index + trd_index == scd_index * 2:
                continue
            result += 1

print(result)

Submission status: Time Limit Exceeded

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

x, y, z = 0,0,0
for i in range(N):
    if S[i] == "R":
        x += 1
    elif S[i] == "G":
        y += 1
    elif S[i] == "B":
        z += 1
    else:
        print("ERROR")

no = 0
for s in range(N-2):
    d = 1
    while(s+2*d < N):
        if len(set([S[s],S[s+d],S[s+2*d]])) == 3:
            no += 1
        d += 1

print(x*y*z-no)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str, input()))
R = [i+1 for i, x in enumerate(S) if x == 'R']
G = [i+1 for i, x in enumerate(S) if x == 'G']
B = [i+1 for i, x in enumerate(S) if x == 'B']

anslist = []
for r in R:
  for g in G:
    for b in B:
      rgb = [r, g, b]
      rgb.sort()
      ji = rgb[1] - rgb[0]
      kj = rgb[2] - rgb[1]
      if ji != kj:
        anslist.append(rgb)
ans = len(anslist)
print(ans)

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

k = 0
count = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if(i>=j or j >=k or i >=k): continue
        if(i==j or j==k or k==i or k<0 or k>=N): continue
        if(S[i] == S[j] or S[j] == S[k] or S[k] == S[i]): continue
        
        count += 1
        
print(total - count)

Submission status: Time Limit Exceeded

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

count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

an = 0
anl =[]
print(len([anl for i in range(1,n+1)
 for j in range(i+1,n+1)
 for k in range(j+1,n+1)
  if i<j and j<k and (j-i) != (k-j) and s[i-1]!=s[j-1] and s[i-1]!=s[k-1] and s[j-1]!=s[k-1]]))