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:

a=int(input())
b=str(input())
sum=0
c=b
d=b
i=0
for s in b:
    if s=="R":
        if a>=i:
            for n in range(i,a):
                if c[n]=="G":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="B":
                                k=m-n
                                if k!=j:
                                    sum+=1
    if s=="R":
        if a>=i:
            for n in range(i,a):
                if c[n]=="B":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="G":
                                k=m-n
                                if k!=j:
                                    sum+=1

    if s=="G":
        if a>=i:
            for n in range(i,a):
                if c[n]=="R":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="B":
                                k=m-n
                                if k!=j:
                                    sum+=1
    if s=="G":
        if a>=i:
            for n in range(i,a):
                if c[n]=="B":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="R":
                                k=m-n
                                if k!=j:
                                    sum+=1

    if s=="B":
        if a>=i:
            for n in range(i,a):
                if c[n]=="R":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="G":
                                k=m-n
                                if k!=j:
                                    sum+=1
    if s=="B":
        if a>=i:
            for n in range(i,a):
                if c[n]=="G":
                    j=n-i
                    if a>=n:
                        for m in range(n,a):
                            if c[m]=="R":
                                k=m-n
                                if k!=j:
                                    sum+=1


    i+=1
print(sum)

Submission status: Time Limit Exceeded

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

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    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[i] != s[k] and s[j] != s[k] and j-i != k-j:
                    ans += 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

Slist = list(S)

count = 0
for i in range(len(Slist)):
    for j in range(i+1, len(Slist)):
        #if(Slist[i] == Slist[j]):
        #    break
        for k in range(j+1, len(Slist)):
            notEqualColor = (Slist[i] == Slist[j] or Slist[j] == Slist[k] or Slist[k] == Slist[i])
            notEqualDistance = (j - i == k - j)

            if((notEqualColor or notEqualDistance) == False):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
ans = 0
for a in range(0,N-2):
    for  b in range(a+1,N-1,1):
        if s[a]!=s[b]:
            RGBlist = ["R","G","B"]
            RGBlist.remove(s[a])
            RGBlist.remove(s[b])
            ans_list = s[(b+1):]
            if b-a <= len(s[(b+1):]):
                ans_list.pop(b-a-1)
            ans += ans_list.count(RGBlist[0])
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Rcnt = 0
Gcnt = 0

excep = 0

for i in S:
    if i == 'R':
        Rcnt += 1
    elif i == 'G':
        Gcnt += 1

for i in range(len(S)):
    for j in range(1, len(S)):
        if i+j+j < len(S) and S[i] != S[i+j]:
            if S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                # print(S[i], S[i+1], S[i+2])
                excep += 1

RGBcalc = Rcnt * Gcnt * (N - Rcnt - Gcnt)

print(RGBcalc - excep)

Submission status: Time Limit Exceeded

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

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

for index, color in enumerate(S):
  if color == "R":
    R.append(index)
  if color == "G":
    G.append(index)
  if color == "B":
    B.append(index)

result = 0
import itertools

for r_index, g_index, b_index in itertools.product(R, G, B):
  sorted_index = sorted([r_index, g_index, b_index])
  if(sorted_index[1] - sorted_index[0] != sorted_index[2] - sorted_index[1]):
    result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

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

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

ans = N * (N - 1) * (N - 2) // 6
for i in range(N):
  I = S[i]
  for j in range(i+1, N):
    J = S[j]
    if I == J:
      ans -= N - 2
    else:
      if 2 * j - i < N:
        if len(set([I, J, S[2 * j - i]])) == 3:
          ans -= 1
          
ans += r * (r - 1) * (r - 2) // 3
ans += g * (g - 1) * (g - 2) // 3
ans += b * (b - 1) * (b - 2) // 3

print(ans)

Submission status: Time Limit Exceeded

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

def f(a, b, c):
    if abs(a-b) == abs(a-c) or abs(a-b) == abs(b-c) or abs(b-c) == abs(a-c):
        return 0
    else:
        return 1

cnt = 0
R, B, G = [], [], []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "B":
        B.append(i)
    else:
        G.append(i)

for r in R:
    for b in B:
        for g in G:
            if f(r, b, g):
                cnt += 1
# print(R, G, B)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
notToAllow = 0


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

total = R*G*B

for i in range(0,N):
    for j in range(0,(N//2)+1):
        if(j+j+i < N):
            if ((S[i] != S[i+j]) and (S[i] != S[i+j+j]) and (S[i+j] != S[i+j+j])):
                notToAllow = notToAllow + 1

print(R*G*B - notToAllow)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b

for i in range(N):
    for j in range(i + 1, N):
        x = j + j - i
        if x <= N - 1:
            if {S[i], S[j], S[x]}.__contains__("R") and {S[i], S[j], S[x]}.__contains__("G") and {S[i], S[j], S[x]}.__contains__("B"):
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    for k in range(j+1, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1)] = total
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        if a+str(i) in check:
            total += check[a+str(i)]
            break
        if a != s[j]:
            total += get_pattern2(i, j)
    check[a+str(i)] = total

    # k-j = i-jなものをぬく
    for j in range(i+1, n):
        k = j + j - i
        if k < n:
            if a != s[k] and s[j] != s[k] and a != s[j]:
                total -= 1

    return total

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
out=0
n1=0
n2=0
for i in range (0,N-3):
    mark1=i
    mark2=i
    n1=0
    n2=0
    if S[i]=="R":
        def1="G"
        def2="B"
    if S[i]=="G":
        def1="R"
        def2="B"
    if S[i]=="B":
        def1="G"
        def2="R"
    for j in range (i+3,N):
        if def1==S[j]:
            for k in range (mark1+1,j):
                if S[k]==def2:
                    n1 = n1 + 1
            mark1=j
            out = out+n1
            if (i+j)%2==0:
                if S[int((i+j)/2)]==def2:
                    out =out -1
        elif def2==S[j]:
            for k in range (mark2+1,j):
                if S[k]==def1:
                    n2 = n2 + 1
            mark2=j
            out = out+n2
            if (i+j)%2==0:
                if S[int((i+j)/2)]==def1:
                    out =out -1
print(out)

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

if __name__ == '__main__':
    n = int(input())
    s = list(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)
    x = len(b)
    ans = 0
    for i in range(n):
        for j in range(i, n):
            if s[i] == s[j]:
                continue
            if s[i] == "R" and s[j] == "G":
                out = j * 2 - i
                tmp = bisect.bisect_left(b, j)
                t = len(b) - tmp
                ans += (t - 1) if out in b[tmp:] else t

            elif s[i] == "R" and s[j] == "B":
                out = j * 2 - i
                tmp = bisect.bisect_left(g, j)
                t = len(g) - tmp
                ans += (t - 1) if out in g[tmp:] else t

            elif s[i] == "G" and s[j] == "B":
                out = j * 2 - i
                tmp = bisect.bisect_left(r, j)
                t = len(r) - tmp
                ans += (t - 1) if out in r[tmp:] else t

            elif s[i] == "G" and s[j] == "R":
                out = j * 2 - i
                tmp = bisect.bisect_left(b, j)
                t = len(b) - tmp
                ans += (t - 1) if out in b[tmp:] else t

            elif s[i] == "B" and s[j] == "R":
                out = j * 2 - i
                tmp = bisect.bisect_left(g, j)
                t = len(g) - tmp
                ans += (t - 1) if out in g[tmp:] else t

            elif s[i] == "B" and s[j] == "G":
                out = j * 2 - i
                tmp = bisect.bisect_left(r, j)
                t = len(r) - tmp
                ans += (t - 1) if out in r[tmp:] else t

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
T = {'R', 'G', 'B'}
is_R, is_G, is_B = [], [], []

for i in S:
    is_R.append(i=='R')
    is_G.append(i=='G')
    is_B.append(i=='B')
    
sum_R = [0] + is_R
sum_G = [0] + is_G
sum_B = [0] + is_B

for i in range(1, N+1):
    sum_R[i] = sum_R[i-1] + sum_R[i]
    sum_G[i] = sum_G[i-1] + sum_G[i]
    sum_B[i] = sum_B[i-1] + sum_B[i]
    
dict_RGB = {'R':(is_R, sum_R), 'G':(is_G, sum_G), 'B':(is_B, sum_B)}

for i in range(N-2):
    s_i = S[i]
    for j in range(i+1, N-1):
        s_j = S[j]
        if s_i == s_j:
            continue
        
        s_k = list(T - set((s_i, s_j)))[0]
        
        is_s_k, sum_s_k = dict_RGB[s_k]
        ans = ans + sum_s_k[-1] - sum_s_k[j]
        if (2 * j - i < N) and (is_s_k[2*j - i]):
            ans -= 1
        
    
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

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

new_list = []

r_list = []
g_list = []
b_list = []

for i, s in enumerate(S):
    if s == 'R':
        r_list.append(i)
    elif s == 'G':
        g_list.append(i)
    else:
        b_list.append(i)

num = 0

for r in r_list:
    for g in g_list:
        for b in b_list:
            if abs(g-b) == abs(b-r) or abs(b-r) == abs(g-r) or abs(g-b) == abs(g-r):
                pass
            else:
                num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=0
if n<=3:
    print(0)
    quit()
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]!=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()

r_list = []
g_list = []
b_list = []

res = 0

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

# print(r_list)
# print(g_list)
# print(b_list)


res = len(r_list)*len(g_list)*len(b_list)
for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            tmp = sorted([r_index, g_index, b_index])
            if tmp[2]-tmp[1] == tmp[1]-tmp[0]:
                res -= 1
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
count = 0
N = int(input())
S = list(input())
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if(S[i] != S[j] and S[i] != S[k] and S[j] != S[k] and (j - i) != (k - j)):
                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[i] != s[j]:
            for k in range(j,n):
                if s[k] != s[i] and s[k] != s[j]  and j-i != k-j:
                    #print(i,j,k,s[i],s[j],s[k] )
                    cnt += 1
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
S = input()
SS = list(S)
n = 0
for i in range(N):
  if SS[i] == "R":
    SS[i] = 1
  if SS[i] == "G":
    SS[i] = 2
  if SS[i] == "B":
    SS[i] = 3
for i in range(N):
  for j in range(N):
    for k in range(N):
      kk = []
      kk.append(i)
      kk.append(j)
      kk.append(k)
      kk.sort()
      if SS[int(kk[0])] != SS[int(kk[1])] and SS[int(kk[1])] != SS[int(kk[2])] and SS[int(kk[2])] != SS[int(kk[0])]:
        if int(kk[1]) - int(kk[0]) != int(kk[2]) - int(kk[1]):
            n+=1
f = math.factorial(3)
ans = int(n / f)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(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)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            temp = [r[i], g[j], b[k]]
            temp.sort()

            if temp[1]-temp[0] != temp[2]-temp[1]:
                cnt += 1
                if temp[2] == b[k] and temp[1]-temp[0] < temp[2]-temp[1]:
                    cnt = cnt+len(b)-1-k
                    break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input()) 
s = str(input()) 
count =  [0]
leng = len(s)
for i in range(leng):
    for j in range(leng):
        for k in range(leng):
            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):
                count[0] += 1
print(count[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L = []
Ans = 0
for i in range(N):
  s = S[i]
  a = 0
  b = 0
  for j in range(i+1,N):
    t = S[j]
    if s != t:
      K = ["R","G","B"]
      K.remove(s)
      if t == K[0]:
        a +=1
      else:
        b +=1
  Ans += a*b

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

Submission status: Time Limit Exceeded

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

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

r = list()
g = list()
b = list()

for i in range(N):
    if S[i] == "R":
        r.append(i)
    elif S[i] == "G":
        g.append(i)
    else:
        b.append(i)

bb = len(b)
gg = len(g)
rr = len(r)

for i in range(bb):
    for j in range(gg):
        for k in range(rr):
            if abs(r[k] - g[j]) == abs(g[j] - b[i]) or abs(g[j] - b[i]) == abs(b[i] - r[k]) or abs(b[i] - r[k]) == abs(r[k] - g[j]):
                count += 1
sum = len(r) * len(g) * len(b) - count
print(sum)

Submission status: Time Limit Exceeded

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

for i in range(0, len(S) - 2):
    for j in range(i + 1, len(S) - 1):
        if S[i] == S[j]:
            continue
        else:
            for k in range(j + 1, len(S)):
                if S[j] == S[k] or S[i] == S[k]:
                    continue
                elif j - i == k - j:
                    continue
                else:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

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


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

print(count)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

count=0
for i in range(N):
    s_i = S[i]

    for j in range(i+1, N):
        s_j = S[j]
        if s_i == s_j:
            continue

        for k in range(j+1, N):
            s_k = S[k]

            if s_i!=s_j and s_i!=s_k and s_j!=s_k and j-i!=k-j:
                count+=1

print(count)

Submission status: Time Limit Exceeded

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


colors = ['R', 'G', 'B']
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):
    first = S[i]
    for j in range(i+1, N):
        second = S[j]
        if first == second:
            continue
        third = [color for color in colors if color not in [first, second]][0]
        res += accs[third][j]
        if j + (j - i) < N:
            if S[j + (j - i)] == third:
                res -= 1
                
print(res)

Submission status: Time Limit Exceeded

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

for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            L = "RGB"
            L = L.replace(S[i],"",1)
            L = L.replace(S[k],"",1)
            ans += S[i+1:k].count(L)
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == L:
                    ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
def calc(first, second, last_add_li, last_li):
    ans = 0
    for i in range(len(S)):
        if S[i] == first:
            for j in range(i+1, len(S)):
                if S[j] == second:
                    ans += (last_add_li+[0])[j+1]
                    if j+j-i < len(Bs) and last_li[j+j-i] == 1:
                        ans -= 1
    return ans

# print('input >>')
N = int(input())
S = input()

Rs = [1 if i == 'R' else 0 for i in S]
Gs = [1 if i == 'G' else 0 for i in S]
Bs = [1 if i == 'B' else 0 for i in S]

Rs_add = [0] * len(S)
Rs_add[0] = sum(Rs)
Gs_add = [0] * len(S)
Gs_add[0] = sum(Gs)
Bs_add = [0] * len(S)
Bs_add[0] = sum(Bs)

for i in range(len(Rs)-1):
    if Rs[i] == 1:
        Rs_add[i+1] = Rs_add[i]-1
    else:
        Rs_add[i+1] = Rs_add[i]
for i in range(len(Gs)-1):
    if Gs[i] == 1:
        Gs_add[i+1] = Gs_add[i]-1
    else:
        Gs_add[i+1] = Gs_add[i]
for i in range(len(Bs)-1):
    if Bs[i] == 1:
        Bs_add[i+1] = Bs_add[i]-1
    else:
        Bs_add[i+1] = Bs_add[i]

ans = 0

ans += calc('R', 'G', Bs_add, Bs)
ans += calc('G', 'R', Bs_add, Bs)
ans += calc('R', 'B', Gs_add, Gs)
ans += calc('B', 'R', Gs_add, Gs)
ans += calc('G', 'B', Rs_add, Rs)
ans += calc('B', 'G', Rs_add, Rs)


# print('-----output-----')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N = int(input())
  S = input()
  ct = 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):
          ct += 1
  print(ct)
  return 0
 
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i, s in enumerate(S):
    if s == 'R':
        R.append(i+1)
    elif s == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
ans = len(R)*len(G)*len(B)
# (RG)B
for a in R:
    for b in G:
        if b > a:
            if b + (b-a) in B:
                ans -= 1
        else:
            if a + (a-b) in B:
                ans -= 1
# (RB)G
for a in R:
    for b in B:
        if b > a:
            if b + (b-a) in G:
                ans -= 1
        else:
            if a + (a-b) in G:
                ans -= 1
# (BG)R
for a in B:
    for b in G:
        if b > a:
            if b + (b-a) in R:
                ans -= 1
        else:
            if a + (a-b) in R:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

c = 0

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

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

print(count)

Submission status: Time Limit Exceeded

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

s_r = []
s_g = []
s_b = []

for i in range(n):
  if s[i] == "R":
    s_r.append(i)
  elif s[i] == "G":
    s_g.append(i)
  else:
    s_b.append(i)

ans = 0
    
def conditions(a, b):
  c = [-1, -1, -1]
  if (a+b) % 2 == 0:
    c[0] = (a+b) / 2
    c[1] = 2*b - a
    c[2] = 2*a - b
  else:
    c[1] = 2*b - a
    c[2] = 2*a - b
  list = []
  for i in range(3):
    if c[i] >= 0 and c[i] <= n-1:
      list.append(c[i])
  return list
  
for item in s_r:
  for item2 in s_g:
    count = 0
    for i in conditions(item, item2):
      if i in s_b:
        count += 1
    ans += len(s_b) - count

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(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
count = 0
for k in R:
    for j in G:
        for i in B:
            fr = 0
            ab = [k,j,i]
            ab.sort()
            if ab[2] - ab[1] == ab[1] - ab[0]:
                pass
            else:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
ans=0
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            continue
        if j+(j-i)<N:
            s=S[j+(j-i)]
            S[j+(j-i)]="O"
        if i-(j-i)>=0:
            s1=S[i-(j-i)]
            S[i-(j-i)]="O"
        S2="".join(S)
        if (S[i]=="R" and S[j]=="G") or (S[i]=="G" and S[j]=="R"):
            ans+=S2.count("B",j+1,N)
        if (S[i]=="R" and S[j]=="B") or (S[i]=="B" and S[j]=="R"):
            ans+=S2.count("G",j+1,N)
        if (S[i]=="B" and S[j]=="G") or (S[i]=="G" and S[j]=="B"):
            ans+=S2.count("R",j+1,N)
        if j+(j-i)<N:
            S[j+(j-i)]=s
        if i-(j-i)>=0:
            S[i-(j-i)]=s1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
l = [input() for _ in range(2)]
n = l[0]
S = l[1]
N = int(n)
count = 0

indexR = []
indexG = []
indexB = []

i = 0
j = 0
k = 0

for a in range(N):
    character = str(S)[a:a+1]

    if character == 'R':
        indexR.append(a+1)
    elif character == 'G':
        indexG.append(a+1)
    elif character == 'B':
        indexB.append(a+1)

for r in indexR:
    for g in indexG:
        for b in indexB:
            if abs(r-g) != abs(g-b) and abs(g-r) != abs(r-b) and abs(g-b) != abs(r-b):
                count = count + 1

print(count)

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)
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:
import copy
n = int(input())
s = input()

rs = {}
gs = {}
bs = {}

for i, e in enumerate(s):
  if e == 'R':
    rs[i] = ''
  if e == 'G':
    gs[i] = ''
  if e == 'B':
    bs[i] = ''
   
      
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  if len(rs) < len(bs):
    for i in rs.keys():
      for j in gs.keys():
        bs_tmp = copy.deepcopy(bs)
        mi = i if i < j else j
        ma = j if i < j else i
        p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
        for e in p:
          if e%1 != 0 or e < 0 or e > len(s):
            continue
          if e in bs_tmp:
            del bs_tmp[e]
            saidai -= 1
    print(int(saidai))
  else:
    for i in bs.keys():
      for j in gs.keys():
        rs_tmp = copy.deepcopy(rs)
        mi = i if i < j else j
        ma = j if i < j else i
        p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
        for e in p:
          if e%1 != 0 or e < 0 or e > len(s):
            continue
          if e in rs_tmp:
            del rs_tmp[e]
            saidai -= 1
    print(int(saidai))