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



R = []
G = []
B = []
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)
ans = 0

Rt = tuple(R)
Gt = tuple(G)
Bt = tuple(B)

for i in Rt:
    for j in Gt:
        for k in Bt:
            x = sorted([i, j, k])
            if x[2] - x[1] != x[1] - x[0]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

A = {'R':[] ,'G':[] ,'B':[]}
sum = 0

for i in range(N):
    A[S[i]].append(i+1)

for i in A['R']:
    for j in A['B']:
        for k in A['G']:
            if not i-j == k-j:
                l = [i,j,k]
                l.sort()
                if not l[1] - l[0] == l[2] - l[1]:
                    sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline
s =int(readline().rstrip().decode())
t = readline().rstrip().decode()
res = 0
for i in range(s):
    for j  in range(s):
        for k  in range(s):
            if i < j and j < k:
                if t[i] != t[j] and t[j] != t[k] and t[i] !=t[k] and j -i != k-j:
                    res += 1
print(res)

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],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 (big+small)/2 == medium:
                    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()

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

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

        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:
# input
N = int(input())
S = input()

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

ans = 0
for r in R:
    for g in G:
        for b in B:
            i, j, k = r, g, b
            if i > j:
                i, j = j, i
            if i > k:
                i, j, k = k, i, j
            elif j > k:
                j, k = k, j

            if k-j != j-i:
                ans += 1

# output
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):
        a = s[i]
        b = s[j]
        if a == b:
            continue
        else:
            for k in range(j+1,n):
                c = s[k]
                if a != c and b != c and j-i != k-j:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    r = [0]*n
    g = [0]*n
    b = [0]*n
    ans = 0

    dict = {"R":r, "G":g, "B":b}

    for i in range(n):
        dict[s[i]][i] = 1
 
    def rgb_sum(t,i,j):
        for s3 in "RGB":
            if s3 not in t:
                if 2*j-i < n:
                    return sum(dict[s3][j+1:]) - dict[s3][2*j-i]
                else:
                    return sum(dict[s3][j+1:])
                break

    for i in range(n):
        for j in range(i,n):
            s1 = s[i]
            s2 = s[j]
            t = (s1,s2)
            if s1 != s2:
                ans += rgb_sum(t,i,j)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if s[j] != a:
            b = s[j]
            for k in range(j+1,n):
                if s[k]!=a and s[k]!=b:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
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-2):
    for j in range(0,(N//2)+1):
        if(j+j+i < N) and (j+j < N) and (i+j < 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 = input()
A = 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):
            si = S[i]
            sj = S[j]
            sk = S[k]
            if not ( j-i == k-j or si==sk or sj==sk ):
                A += 1
print(A)

Submission status: Time Limit Exceeded

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

count=0

for index1, val1 in enumerate(S):
      count += sum([sum([1 for index3, val3 in enumerate(S[index1+index2+2:]) if val3 not in [val1, val2] and index2 != index3]) for index2, val2 in enumerate(S[index1+1:]) if val1 != val2])
          
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
st = input()
i = 0
ans = 0
R = [0]*n
G = [0]*n
B = [0]*n
rl = 0
gl = 0
bl = 0
for i,val in enumerate(st[:0:-1],2):
    if val=='R':
        rl += 1
    elif val=='G':
        gl += 1
    else:
        bl += 1
    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[j]
            char = "B"
        elif "R"!=st[i] and "R"!=st[j]:
            cnt = R[j]
            char = "R"
        else:
            cnt = G[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 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] == "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] == "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] == "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:
from numpy import array, append

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

RGBs = []
count = 0
RGBs.append({"value": S[0], "left_R": array([]), "left_G": array([]), "left_B": array([])})

for i in range(1, N):
    new = S[i]
    for j in range(i):
        if (S[j] == 'G' and new == 'B') or (S[j] == 'B' and new == 'G'):
            count += len(RGBs[j]["left_R"]) - int(i-j in RGBs[j]["left_R"])
        elif (S[j] == 'R' and new == 'B') or (S[j] == 'B' and new == 'R'):
            count += len(RGBs[j]["left_G"]) - int(i-j in RGBs[j]["left_G"])
        elif (S[j] == 'R' and new == 'G') or (S[j] == 'G' and new == 'R'):
            count += len(RGBs[j]["left_B"]) - int(i-j in RGBs[j]["left_B"])

    tmp_left_R = RGBs[i-1]["left_R"] + 1
    tmp_left_G = RGBs[i-1]["left_G"] + 1
    tmp_left_B = RGBs[i-1]["left_B"] + 1
    if S[i-1] == 'R':
        tmp_left_R = append(tmp_left_R, 1)
    elif S[i-1] == 'G':
        tmp_left_G = append(tmp_left_G, 1)
    elif S[i-1] == 'B':
        tmp_left_B = append(tmp_left_B, 1)
    RGBs.append({"value": S[i], "left_R": tmp_left_R, "left_G": tmp_left_G, "left_B": tmp_left_B})

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
i = 0
j = 0
for a in s:
    j = i+1
    for b in s[i+1:n]:
        if a != b:
            k = j+1
            for c in s[j+1:n]:
                if (a != c and b != c):
                    if (j-i != k-j):
                        ans +=1
                k+=1
        j+=1
    i+=1
print(ans)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
    return [id for id, _x in enumerate(l) if _x == x]

R_index = my_index_multi(s,'R')
G_index = my_index_multi(s,'G')
B_index = my_index_multi(s,'B')

#for R_index[i] in range(len(R_index)):
#    R_index[R_index[i]] += 1
#for G_index[j] in range(len(G_index)):
#    G_index[G_index[j]] += 1
#for B_index[k] in range(len(B_index)):
#    B_index[B_index[k]] += 1

count =  len(R_index)*len(G_index)*len(B_index)
for i in range(len(R_index)):
    for j in range(len(G_index)):
        if (R_index[i]+G_index[j])//2 in B_index and (R_index[i]+G_index[j])%2==0:
            count = count -1
    for k in range(len(B_index)):
        if (R_index[i]+B_index[k])//2 in G_index and (R_index[i]+B_index[k])%2==0:
            count = count -1
for j in range(len(G_index)):
    for k in range(len(B_index)):
        if (G_index[j]+B_index[k])//2 in R_index and (G_index[j]+B_index[k])%2==0:
            count = count -1

#        for B_index[k] in range(len(B_index)):
#            li = [R_index[R_index[i]],G_index[G_index[j]],B_index[B_index[k]]]
#            li.sort()
#            if (li[1]*2 != li[0]+li[2]):
#                count = count +1

#print(len(R_index))
#print(len(G_index))
#print(len(B_index))
print(count)

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):
        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()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'

n_R = S.count('R')
n_G = S.count('G')
n_B = S.count('B')
ans = n_R * n_G * n_B

for a in range(N - 2):
    # a = 2
    for b in range(1, N // 2 + 1):
        # b = 3
        i = a
        j = a + b
        k = a + 2 * b

        if k > N - 1:
            continue

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

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

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

n = int(input())
s = [i for i in input()]
ans = 0
for i in range(n):
    rgb = ['R', 'G', 'B']
    a = s[i]
    rgb.remove(a)
    for j in range(i+1,n):
        rgb2 = rgb.copy()
        b = s[j]
        if a!=b:
            rgb2.remove(b)
            c = rgb2[0]
            ans += s[j+1:].count(c)
            try:
                if s[2*j-i]==c:
                    ans -= 1
            except:
                pass

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
ans=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)
for r in R:
    for g in G:
        for b in B:
            a=r+g+b
            if a%3!=0:
                ans=ans+1
            else:
                a=a//3
                if a==r or a==g or a==b:
                    continue
                else:
                    ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

def check(r,g,b):
    ans=0
    for i in range(len(r)):
        G=bisect.bisect_left(g,r[i])
        for j in range(G,len(g)):
            B=bisect.bisect_left(b,g[j])
            ans+=len(b)-B
            if 2*g[j]-r[i] in b:
                ans-=1
    return ans

N=int(input())
S=input()
ans=0
r=[]
g=[]
b=[]

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

ans=check(r,g,b)+check(g,b,r)+check(b,r,g)+check(b,g,r)+check(g,r,b)+check(r,b,g)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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)
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 = list(input())

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

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

ans = 0

for i in R:
    for j in G:
        for k in B:
            if i < j < k and j-i != k-j:
                ans += 1

for i in R:
    for j in B:
        for k in G:
            if i < j < k and j-i != k-j:
                ans += 1

for i in G:
    for j in R:
        for k in B:
            if i < j < k and j-i != k-j:
                ans += 1


for i in G:
    for j in B:
        for k in R:
            if i < j < k and j-i != k-j:
                ans += 1

for i in B:
    for j in G:
        for k in R:
            if i < j < k and j-i != k-j:
                ans += 1

for i in B:
    for j in R:
        for k in G:
            if i < j < k and j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

if n < 2:
    print(0)
    exit(0)

cum_r = [0] * (n + 1)
cum_g = [0] * (n + 1)
cum_b = [0] * (n + 1)

for i in range(n):
    cum_r[i + 1] = cum_r[i]
    cum_g[i + 1] = cum_g[i]
    cum_b[i + 1] = cum_b[i]
    if s[i] == "R":
        cum_r[i + 1] += 1
    elif s[i] == "G":
        cum_g[i + 1] += 1
    else:  # s[i] == "b":
        cum_b[i + 1] += 1

sum = 0
for i in range(n - 2):
    for k in range(n - 1, i + 1, -1):
        if s[i] == s[k]:
            continue

        # check "b"
        if (s[i] == "R" and s[k] == "G") or (s[i] == "G" and s[k] == "R"):
            sum += cum_b[k] - cum_b[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "B":
                sum -= 1
        elif (s[i] == "R" and s[k] == "B") or (s[i] == "B" and s[k] == "R"):
            sum += cum_g[k] - cum_g[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "G":
                sum -= 1
        else:
            sum += cum_r[k] - cum_r[i + 1]
            if (k + i) % 2 == 0 and s[(k + i) // 2] == "R":
                sum -= 1

print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str,list(input())))
c = 0

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

print(c)

Submission status: Time Limit Exceeded

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

def judge(i,j,k):
  if j-i == k-j:
    # print(i,j,k, "false")
    return False
  else:
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      # print(i,j,k, "true")
      return True
    else:
      # print(i,j,k, "false")
      return False

def main(R):
  for i in range(N):
    for j in range(i+1,N):
      for k in range(j+1,N):
        if judge(i,j,k):
          R+=1
  print(R)

main(R)

Submission status: Time Limit Exceeded

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

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

print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for i in range(n):
    si=s[i]
    for j in range(i+1,n):
        sj=s[j]
        tmp=j-i
        if si==sj:
            continue
        for k in range(j+1,n):
            sk=s[k]
            if tmp==k-j:
                continue
            if sk==si or sk==sj:
                continue
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    if s[i] == 'R':
        r.append(i)
    if s[i] == 'G':
        g.append(i)
    if s[i] == 'B':
        b.append(i)

for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            rgb = sorted([r[i],g[j],b[k]])
            if rgb[2]-rgb[1] != rgb[1]-rgb[0]:
                count += 1
print(count)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
res = 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 b:
  for j in r:
    res += len(g) - int((i + j) / 2 in g) - int(2 * i - j in g) - int(2 * j - i in g)
print(res)

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],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+small)/2 == medium:
                    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()

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        a = max(i,j)
        u = min(i,j)
        if a*2-u >= 0 and a*2-u < n :
            if s[a*2-u] == 'B':
                ans -= 1
        if 2*u-a >= 0 and 2*u-a < n:
            if s[2*u-a] == 'B':
                ans -= 1
        if (a-u)%2 == 0 and u+(a-u)/2 >= 0 and u+(a-u)/2 < n:
            if s[u+(a-u)//2] == 'B':
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

X = 0

# R_l = [i for i, x in enumerate(S) if x == 'R']
# G_l = [i for i, x in enumerate(S) if x == 'G']
# B_l = [i for i, x in enumerate(S) if x == 'B']
#
# for a in R_l:
#     for b in G_l:
#         if a < b:
#             for c in B_l:
#                 if b < c:
#                     if b - i
#                     X += 1

for i, a in enumerate(S):
    if i + 1 == len(S) - 1:
        break
    else:
        for j in range(i+1,len(S)):
            if a == S[j]:
                pass
            else:
                if j + 1 == len(S):
                    break
                else:
                    for k in range(j+1,len(S)):
                        if a != S[k] and S[j] != S[k]:
                            if j - i != k - j:
                                X += 1

print(X)

Submission status: Time Limit Exceeded

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

ans=0
RGB=[]
for i in range(N-2):
  RGB.append(S[i])
  for j in range(i+1,N-1):
    if S[i]!=S[j]:
      RGB.append(S[j])
      if not 'R' in RGB:
        ans+=S[j+1:].count('R')
        if 2*j-i<N and S[2*j-i]=='R':
          ans-=1
      elif not 'G' in RGB:
        ans+=S[j+1:].count('G')
        if 2*j-i<N and S[2*j-i]=='G':
          ans-=1
      else:
        ans+=S[j+1:].count('B')
        if 2*j-i<N and S[2*j-i]=='B':
          ans-=1
    RGB=[S[i]]
  RGB=[]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def F(p,q,r):
    ret = 0
    for i in range(len(p)):
        pq = [q[t] for t in range(len(q)) if p[i]<q[t]]
        for j in range(len(pq)):
            pr = [r[t] for t in range(len(r)) if pq[j]<r[t]]
            for k in range(len(pr)):
                if not (2*pq[j]==p[i]+pr[k]): ret += 1
    return ret

n,s = int(input()),input()
ans = 0
R = []
G = []
B = []
for i in range(n):
    if s[i]=='R': R.append(i)
    if s[i]=='G': G.append(i)
    if s[i]=='B': B.append(i)
ans += F(R,G,B)
ans += F(R,B,G)
ans += F(G,B,R)
ans += F(G,R,B)
ans += F(B,G,R)
ans += F(B,R,G)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            if si != sj:
                for k in range(j, N):
                    sk = S[k]
                    if j - i != k -j and si != sk and sj != sk:
                        sum += 1
    print(sum)

main()

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):
    k = 2*j-i
    if j < k <= N-1:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        Ans -=1
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
"""
def f(char, start, end):
    global rgb
    return rgb[char][end] - rgb[char][start]

rgb = {'R':[0]*(n+1), 'G':[0]*(n+1), 'B':[0]*(n+1)}
for i in range(1, n+1):
    j = s[i-1]
    rgb['R'][i] = rgb['R'][i-1] + (j=='R')
    rgb['G'][i] = rgb['G'][i-1] + (j=='G')
    rgb['B'][i] = rgb['B'][i-1] + (j=='B')

print(rgb['R'])
print(rgb['G'])
print(rgb['B'])

print(f('R', 0,0) )
print(f('R', 0,1) )
print(f('R', 0,2) )
print(f('R', 0,3) )
print(rgb['R'][-1] * rgb['G'][-1] * rgb['B'][-1])
ans = rgb['R'][-1] * rgb['G'][-1] * rgb['B'][-1]
"""
rgb = {'R':0, 'G':0,'B':0}
for i in range(1, n+1):
    j = s[i-1]
    rgb[j] += 1
ans = rgb['R'] * rgb['G'] * rgb['B']
for j in range(1, n):
    for i in range(j):
        if j-i>(n-1)-j: continue
        if s[j]!=s[i] and s[j+(j-i)]!=s[j] and s[j+(j-i)]!=s[i] :
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

positions = {}
positions['R'] = [i for i in range(n) if s[i]=='R']
positions['G'] = [i for i in range(n) if s[i]=='G']
positions['B'] = [i for i in range(n) if s[i]=='B']
positions['RG'] = sorted(positions['R']+positions['G'])
positions['RB'] = sorted(positions['R']+positions['B'])
positions['GB'] = sorted(positions['G']+positions['B'])

for p1 in range(n-3):
    if s[p1] == 'R':
        pos2 = positions['GB']
    elif s[p1] == 'G':
        pos2 = positions['RB']
    else:
        pos2 = positions['RG']

    for p2 in pos2:
        if p2 > p1:
            if s[p1] == 'R' and s[p2] == 'G':
                pos3 = positions['B']
            elif s[p1] == 'R' and s[p2] == 'B':
                pos3 = positions['G']
            elif s[p1] == 'G' and s[p2] == 'B':
                pos3 = positions['R']
            elif s[p1] == 'G' and s[p2] == 'R':
                pos3 = positions['B']
            elif s[p1] == 'B' and s[p2] == 'R':
                pos3 = positions['G']
            elif s[p1] == 'B' and s[p2] == 'G':
                pos3 = positions['R']
            
            for p3 in pos3:
                if p3 > p2:                
                    if (p1-p2 != p2-p3):
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

# R,G,B ひとつずつ
# 距離が等間隔でない

R_pos = []
G_pos = []
B_pos = []
for i in range(N):
    if S[i] == "R":
        R_pos.append(i)
    elif S[i] == "G":
        G_pos.append(i)
    elif S[i] == "B":
        B_pos.append(i)

answer = 0
for r in R_pos:
    for g in G_pos:
        for b in B_pos:
            dis1 = abs(r-g)
            dis2 = abs(g-b)
            dis3 = abs(b-r)
            if dis1 != dis2 and dis2 != dis3 and dis3 != dis1:
                answer += 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
ct=1
co=1
ai=0
da=0
sum=0
if a>3:
    for r in range(a-2):
        while ai<=(a-2):
            ai=r+ct
            while da<=(a-1):
                da=r+co
                keyw=b[r]+b[r+ai]+b[r+da]
                if ai==da:
                    pass
                elif ('R' in keyw)and('G' in keyw)and('B' in keyw):
                    sum+=1
print(sum)