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

Submission status: Time Limit Exceeded

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

A  = []
for i in range(n):
    if S[i] == "R":
        A.append(-1)
    if S[i] == "G":
        A.append(-2)
    if S[i] == "B":
        A.append(3)

cnt = 0
for i in range(0, n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if A[i]+A[j]+A[k]== 0:
                if (j - i) != (k - j):
                    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

Klist = [N-1]
IJ = list()
for i in range(N-2):
    for j in range(i+1, N-1):
        if not S[i] == S[j]:
            IJ.append((i, j))
            Klist.append(j)
K = sorted(list(set(Klist)), reverse=True)
if 0 in K:
    K.remove(0)
if 1 in K:
    K.remove(1)
for ij in IJ:
    i, j = ij
    for k in K:
        if not j < k:
            break
        if S[j] == S[k]:
            continue
        if 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()
r=[]
g=[]
b=[]
cnt=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 range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)

for i in g:
  for j in range(i,n):
    if s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)

for i in b:
  for j in range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)
    elif s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)
print(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')

ans = r * g * b

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i+1)
    elif ss == 'G':
        b_li.append(i+1)
    else:
        c_li.append(i+1)
if len(a_li) == 0 or len(b_li)== 0 or len(c_li) == 0:
    print(0)
    sys.exit()
#all_count = 0
""" for a in a_li:
    for b in b_li:
        tmp = len(c_li)
        if (2*b - a)  in c_li:
            #lili.append(2*b - a)
            tmp -= 1
        if (2*a - b)  in c_li:
            #lili.append(2*a - b)
            tmp -= 1
        if (a + b)% 2 == 0 and (a + b)//2   in c_li:
            #lili.append((a + b)//2)
            tmp -= 1
        #tmp -= len(set(lili))
        all_count += tmp
print(all_count) """

#print(a_li,b_li,c_li)
all_count = 0
for i in range(1,n-1):
    for j in range(i+1,n):
        for k in range(j+1,n+1):
            if j-i == k-j:
                continue
            else:
                if i in a_li and j in b_li and k in c_li:
                    all_count += 1
                if i in a_li and j in c_li and k in b_li:
                    all_count += 1
                if i in b_li and j in a_li and k in c_li:
                    all_count += 1
                if i in b_li and j in c_li and k in a_li:
                    all_count += 1
                if i in c_li and j in a_li and k in b_li:
                    all_count += 1
                if i in c_li and j in b_li and k in a_li:
                    all_count += 1
print(all_count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = input()
tmp = Counter(S)
# print(tmp)
r = tmp["R"]
g = tmp["G"]
b = tmp["B"]
ans = 0
# for s in S:
#     if s == "R":
#         r += 1
#     elif s == "G":
#         g += 1
#     else:
#         b += 1

for i in range(len(S) - 2):
    j = 1
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    while not (i + j + j >= len(S)):
        if S[i] == "R":
            if S[i + j] == "R":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "G":
                j += 1
                continue
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "B":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
        j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
R = []
G = []
B = []
answer = 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 r in R:
    for g in G:
        for b in B:
            if abs(min(r, g)-b) != abs(max(r, g)-b) and abs(min(r, b)-g) != abs(max(r, b)-g) and abs(min(b,g)-r) != abs(max(g, b)-r):
                answer += 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
#h  = list(map(int, input().split()))
#h, w = list(map(int, input().split()))

#j-i=k-jではない 2j=k+i i=2j-k

s = 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 j-i!=k-j:
                    if s[i]!=s[k] and s[j]!=s[k]:
                        count += 1
"""
for i in range(n):
    a = s[i:]
    if s[i]=="R":
        point = a.count("G")*a.count("B")
    elif s[i]=="G":
        point = a.count("R")*a.count("B")
    else:
        point = a.count("R")*a.count("G")
    count += point

#j-i=k-jではない 2j=k+i i=2j-k
#rgb

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


print(count-kom)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
def findb(ind,checker):
    global count
    b = S[ind:].count("B")
    if b > 0:
        if ind + checker < N:
            if S[ind+checker] == "B":
                count += b - 1
            else:
                count += b
        else:
            count += b
def findg(ind,checker):
    global count
    g = S[ind:].count("G")
    if g > 0:
        if ind + checker < N:
            if S[ind+checker] == "G":
                count += g - 1
            else:
                count += g
        else:
            count += g
def findr(ind,checker):
    global count
    r = S[ind:].count("R")
    if r > 0:
        if ind + checker < N:
            if S[ind+checker] == "R":
                count += r - 1
            else:
                count += r
        else:
            count += r
for i in range(N):
    if S[i] == "R":
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            findb(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            findg(i+j,j)
    elif S[i] == "G":
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            findr(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            findb(i+j,j)
    else:
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            findg(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            findr(i+j,j)
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_list = [i for i in range(n) if s[i] == "R"]
g_list = [i for i in range(n) if s[i] == "G"]
b_list = [i for i in range(n) if s[i] == "B"]
ans = 0
for r in r_list:
    for g in g_list:
        for b in b_list:
            L = sorted([r, g, b])
            if L[1] - L[0] != L[2] - L[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-3):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            x = {'R', 'G', 'B'} - {S[i], S[j]}
            ans += S[j+1:N].count(*x)

            if (2 * j - i <= N-1):
                if ({S[j+j-i]} == x):
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

def main():
    NumString = int(input())
    String = input()
    
    total_ans = 0
    for i, i_string in enumerate(String[:-2]):
        RGB = 'RGB'
        for j, j_string in enumerate(String[i+1:-1], i+1):
            if i_string == j_string:
                continue

            k_RGB = RGB.replace(i_string, '').replace(j_string, '')
            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()

count = 0

for i in range(0, N):
    for j in range(i+1, N):
        if(S[i] != S[j]):
            check_char = ''
            if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
                check_char = 'B'
            elif (S[i] == 'R' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'R'):
                check_char = 'G'
            else:
                check_char = 'R'
            count += S[j+1:].count(check_char)
            if len(S[j+1:]) >= j - i:
                if S[j + (j - i)] == check_char:
                    count -= 1

print(count)

Submission status: Time Limit Exceeded

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

R_num=S.count('R')
G_num=S.count('G')
B_num=S.count('B')

ans=0
for k in range(N//2-2+1 if N&1==0 else N//2-1+1):
    flag=0
    for i in range(N-k-2):

        if i+2*(k+1)<=N-1:
            ls=[S[i],S[i+k+1],S[i+2*(k+1)]]
    
            if "R" in ls and "G" in ls and "B" in ls:
                ans+=1


print(R_num*G_num*B_num-ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def bool3(a,b,c):
    if (a!=b) and (b!=c) and (c!=a):
        return True
    else:
        return False

def bool2(a,b,c):
    if a-b != b-c:
        return True
    else:
        return False

def main():
    n = int(input())
    s = input()
    ans = 0
    for i in range(n):
        for j in range(i):
            ans += sum([1 if bool3(s[i],s[j],s[k]) and bool2(i,j,k) else 0 for k in range(j)])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
N = int(input())
S = list(input())
nums = [i for i in range(N)]
combinations = itertools.combinations(nums, 3)
ans = 0
for comb in combinations:
    temp = {S[comb[0]], S[comb[1]], S[comb[2]]}
    if len(temp) < 3:
        pass
    elif (S[comb[0]] != S[comb[1]] and S[comb[0]] != S[comb[2]] and S[comb[1]] != S[comb[2]]) and (comb[1] - comb[0] != comb[2] - comb[1]):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for k in range(n):
    for j in range(n):
        for i in range(n):
            if i < j < k:
                a = (s[i], s[j], s[k])
                #print(a)
                a_set = set(a)
                #print(a_set)
                if len(a_set) == 3 and (j - i) != (k - j):
                    count += 1
            else:
                continue

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

	sum = 0

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

	print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R_list=[]
G_list=[]
B_list=[]
daburi = 0

for i in range(n):
    if s.find('R') == 0:
        R_list.append(i)
    elif s.find('G') == 0:
        G_list.append(i)
    else:
        B_list.append(i)
    s = s[1:]

for j in range(len(R_list)):
    for k in range(len(G_list)):
        for l in range(len(B_list)):
            big = max(R_list[j],G_list[k])
            small = min(R_list[j],G_list[k])
            sa = abs(R_list[j]-G_list[k])
            #big = max(R_list[j],G_list[k],B_list[l])
            #small =min(R_list[j],G_list[k],B_list[l])
            #medium = (R_list[j]+G_list[k]+B_list[l])-big-small
            #if abs(R_list[j] - G_list[k]) == abs(G_list[k]- B_list[l]) or abs(R_list[j] - G_list[k]) == abs(R_list[j] - B_list[l]) or abs(G_list[k]- B_list[l]) == abs(R_list[j]- B_list[l]) :
            if big < B_list[l]:
                if B_list[l] == big +sa:
                    daburi +=1
            elif small > B_list[l]:
                if B_list[l] ==small -sa:
                    daburi +=1
            else:
                if B_list[l] == (big+small)/2:
                    daburi +=1

ans = len(R_list)*len(G_list)*len(B_list)
print(ans - daburi)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = [S[i] for i in range(len(S))]
R_list = []
G_list = []
B_list = []
for i in range(len(s)):
    if s[i] == "R":
        R_list.append(i+1)
    elif s[i] == "G":
        G_list.append(i+1)
    elif s[i] == "B":
        B_list.append(i+1)
ans = 0
for i in range(len(R_list)):
    for j in range(len(G_list)):
        for k in range(len(B_list)):
            list = [R_list[i],G_list[j],B_list[k]]
            list.sort()
            if list[1] - list[0] != list[2] - list[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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)
    
mcount = 0
for i in range(int((n-1)/2)):
  k = i+1
  for j in range(k, n-k):
    if s[j] != s[j-k] and s[j] != s[j+k] and s[j-k] != s[j+k]:
      mcount += 1
  

for i in range(n):
  if i in r:
    for j in g:
      for k in b:
        if k < j:
          continue
        count += 1
    for j in b:
      for k in g:
        if k < j:
          continue
        count += 1      
  if i in g:
    for j in r:
      for k in b:
        if k < j:
          continue
        count += 1
    for j in b:
      for k in r:
        if k < j:
          continue
        count += 1
  if i in b:
    for j in g:
      for k in r:
        if k < j:
          continue
        count += 1
    for j in r:
      for k in g:
        if k < j:
          continue
        count += 1
  if i in r:
    r = r[1:]
  elif i in g:
    g = g[1:]
  else:
    b = b[1:]


print(count-mcount)

Submission status: Time Limit Exceeded

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

colors = ['R', 'G', 'B']
thirds = {'RG': 'B', 'GR': 'B', 'BR': 'G', 'RB': 'G', 'BG': 'R', 'GB': 'R'}
accs = {color: [] for color in colors}
nums = {color: 0 for color in colors}
sums = {color: len([s for s in S if s == color]) for color in colors}
for i in range(N):
    nums[S[i]] += 1
    for color in colors:
        accs[color].append(sums[color] - nums[color])
        
res = 0
for i in range(N-2):
    first = S[i]
    for j in range(i+1, N-1):
        second = S[j]
        if first == second:
            continue
        third = thirds[first+second]
        res += accs[third][j]
        if 2 *j - i < N and S[2*j - i] == third:
            res -= 1
                
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
if N > 3:
    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 and 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:
N = int(input())
S = input()

R = [i for i, x in enumerate(S) if x == 'R']
G = [j for j, y in enumerate(S) if y == 'G']
B = [k for k, z in enumerate(S) if z == 'B']

cnt=0
for r in R:
    for g in G:
        for b in B:
            if abs(g-r) != abs(b-g) and \
                abs(b-r) != abs(g-b) and \
                abs(r-b) != abs(g-r):
                cnt+=1

print(cnt)

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)
    elif s[i]=='G':
        G.append(i)
    else:
        B.append(i)
B = tuple(B)
if len(R)==0 or len(G)==0 or len(B)==0:
    print(0)
    exit()
cnt = 0
for r in R:
    for g in G:
        a,b = min(r,g), max(r,g)
        if 2*a-b in B:
            cnt += 1
        if 2*b-a in B:
            cnt += 1
        if (b-a)%2==0 and a+(b-a)//2 in B:
            cnt += 1
print(len(R)*len(G)*len(B)-cnt)

Submission status: Time Limit Exceeded

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


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


print(count)

Submission status: Time Limit Exceeded

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

def solve(N: int, S: str):
    r = []
    g = []
    b = []
 
    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        else:b.append(i)
 
    g = np.array(g)
 
    def rgb(r, g, b):
        sb = set(b)
        ans = 0
        for r_ in r:
            # rg
            for g_ in g[g>r_]:
                # rgb
                if (g_ + (g_ - r_)) in sb:
                    ans -= 1
                # brg
                if (r_ - (g_ - r_)) in sb:
                    ans -= 1
                # rbg
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b)

            # gr
            for g_ in g[g<r_]:
                # grb
                if (r_ + (r_ - g_)) in sb:
                    ans -= 1
                # bgr
                if (g_ - (r_ - g_)) in sb:
                    ans -= 1
                # gbr
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b)

        return ans
 
    k = rgb(r,g,b)
    print(k)
 
 
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
#import numpy as np

def main():
    ##get int
    first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    #first_input = input().split(" ")
    second_input = input().split(" ")

    N = first_input[0]
    S = second_input[0]

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

    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def input():
    return sys.stdin.readline()


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

    all_count = 0
    count = 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[k] or s[j] == s[k]:
                    continue

                count += 1

    print(count)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(0, N-3):
        si = S[i]
        for a in retA(si):
            idx1 = S.find(a, i)
            while idx1 != -1:
                b = retB(a, si)
                cnt += S[idx1+1:].count(b)
                if 2*idx1 - i < N:
                    if S[2*idx1 - i] == b:
                        cnt -= 1
                idx1 = S.find(a, idx1+1)

    print(cnt)

def retA(a):
    if a == 'R':
        A = ('G', 'B')
    elif a == 'G':
        A = ('R', 'B')
    elif a == 'B':
        A = ('R', 'G')
    return A

def retB(a, x):
    if (a == 'R' or x == 'R') and (a == 'G' or x == 'G'):
        b = 'B'
    elif (a == 'B' or x == 'B') and (a == 'G' or x == 'G'):
        b = 'R'
    elif (a == 'B' or x == 'B') and (a == 'R' or x == 'R'):
        b = 'G'
    return b

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

def rgb(n,s):    
    a=s[0]
    if len(s)==3:
        SUM=0

    else:
        SUM=rgb(n-1,s[1:])

        for i in range(1,n-1):
            l=["R","G","B"]
            l.remove(a)
            if s[i] != a:
                b=s[i]
                l.remove(b)
                if l!=[]:
                    c=l[0]
                    p=s.count(c,i+1,2*i)
                    q=s.count(c,2*i+1,n+1)
                    SUM+=p+q
                
    return SUM

n=int(input())
s=input()
print(rgb(n,s))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
num=len(s)
ans=0
for i in range(num):
    for j in range(num):
        for k in range(num):
            if k<j or j<i or k<i:
                continue
            elif j-i==k-j:
                continue
            elif s[i]!=s[j] and s[i]!=s[k] and s[k]!=s[j]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(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)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            middle=r+g+b-min(r,g,b)-max(r,g,b)
            mi=min(r,g,b)
            ma=max(r,g,b)
            if (middle-mi)!=(ma-middle):
                count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
st = input()
i = 0
ans = 0
R = defaultdict(int)
G = defaultdict(int)
B = defaultdict(int)
rl = 0
gl = 0
bl = 0
RI = []
BI = []
GI = []
for i,val in enumerate(st[:0:-1],2):
    if val=='R':
        rl += 1
        #RI.append(i)
    elif val=='G':
        gl += 1
        #GI.append(i)
    else:
        bl += 1
        #BI.append(i)
    R[-i] = rl
    G[-i] = gl
    B[-i] = bl
ans = 0
for i in range(n):
    for j in range(i+1,n):
        if st[i]==st[j]:
            continue
        if "B"!=st[i] and "B"!=st[j]:
            cnt = B[-(n-j)]
            char = "B"
        elif "R"!=st[i] and "R"!=st[j]:
            cnt = R[-(n-j)]
            char = "R"
        else:
            cnt = G[-(n-j)]
            char = "G"
            
        if 2*j-i<n and st[2*j-i]==char:
            cnt -= 1
        ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
S = S()

def check(i, j, k):
    return j-i != k-j

chars = list(S)

cnt = 0

for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if not check(i, j, k):
                continue
            if chars[i] != chars[j] and chars[i] != chars[k] and chars[j] != chars[k]:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
        n = int(input())
        s = input()
        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
            else:
                b[i] = 1
        ans = 0
        a = [r, g, b]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])
        a = [r, b, g]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])
        a = [g, r, b]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])
        a = [g, b, r]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])

        a = [b, r, g]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])

        a = [b, g, r]
        for i in range(n):
            if a[0][i] == 1:
                for j in range(i + 1, n):
                    if a[1][j] == 1:
                        ans += sum(a[2][j + 1:n])

        for i in range(1, n // 2+1):
            j = 0
            while j + 2 * i <= n - 1:
                if {s[j], s[j + i], s[j + 2 * i]} == {"R", "G", "B"}:
                    ans -= 1
                j += 1
        print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
sum = 0
for i in range(N):
    for j in range(i):
        if(S[i] == S[j]):
            continue
        for k in range(j):
            if(k - j == j - i):
                continue
            if(S[j] == S[k]):
                continue
            elif(S[i] == S[k]):
                continue
            sum += 1
            
print(sum)

Submission status: Time Limit Exceeded

Solution code:
def main():
    word_len = int(input())
    word = str(input())
    R_list = []
    G_list = []
    B_list = []
    for i in range(word_len):
        if word[i] == "R":
            R_list.append(i)
        if word[i] == "G":
            G_list.append(i)
        if word[i] == "B":
            B_list.append(i)
    counter = 0
    for r in R_list:
        for g in G_list:
            for b in B_list:
                v =  [r,g,b]
                v.sort()
                if (v[1]-v[0]) != (v[2]-v[1]):
                    counter += 1
    print(counter)

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

cnt = 0

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