Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
cum = [[0]*3 for _ in range(N+1)]
for i,s in enumerate(S,1):
    cum[i]=cum[i-1].copy()
    if s=='R':
        cum[i][0]+=1
    elif s=='G':
        cum[i][1]+=1
    elif s=='B':
        cum[i][2]+=1


for i in range(len(S)):
  for j in range(i+1,len(S)):
    if S[i]==S[j]:
      continue
    else:
      x = 'RGB'.replace(S[i],'').replace(S[j],'')
      if x=='R':
        ans+=cum[-1][0]-cum[j][0]
        y = 2*j-i
        if y<S_l:
          if S[y]=='R':
            ans-=1
      elif x=='G':
        ans+=cum[-1][1]-cum[j][1]
        y = 2*j-i
        if y<S_l:
          if S[y]=='G':
            ans-=1
      else:
        ans+=cum[-1][2]-cum[j][2]
        y = 2*j-i
        if y<S_l:
          if S[y]=='B':
            ans-=1
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        c_lis = ['R', 'G', 'B']
        c_lis.remove(S[i])
        c_lis.remove(S[j])
        chara = c_lis[0]
        try:
            if S[i+(j-i)*2]==chara:
                ans-=1
        except:
            pass
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
n=int(input())
RGB = str(input())

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

rc = s.count('R')
gc = s.count('G')
bc = s.count('B')
ans = rc * gc * bc

for i in range(n-1):
  for j in range(i+1,n):
    k = j*2 - i
    if k >= n:
      continue
    if len(set([s[i],s[j],s[k]])) == 3:
      ans -= 1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def checkRGB(a,b):
    if not("B" in a) and not("B" in b):
        return "B"
    elif not("G" in a) and not("G" in b):
        return "G"
    else:
        return "R"

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

if N<4:
    print(0)
    exit()
sum = 0
for i in range(1,N//2+N%2+1):
    center = S[i]
    for j in range(i):
        if S[j] != center:
            left = S[j]
            rS1 = S[i+1:2*i-j]
            rS2 = S[2*i-j+1:]
            right = checkRGB(left,center)
            sum += rS1.count(right) + rS2.count(right)
        else:
            pass
for i in range(N//2+N%2+1,N-1):
    center = S[i]
    for j in range(i+1,N):
        if S[j] != center:
            right = S[j]
            lS1 = S[:2*i-j]
            lS2 = S[2*i-j+1:i]
            left = checkRGB(right,center)
            sum += lS1.count(left) + lS2.count(left)
        else:
            pass
print(sum)

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,N-1):
        for k in range(j,N):
            si,sj,sk = S[i],S[j],S[k]
            #print(i,j,k)
            if si != sj and si != sk and sk != sj and (j-i) != (k-j):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

R_list = []
G_list = []
B_list = []
cnt = 0

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)
        
for r in R_list:
    for g in G_list:
        for b in B_list:
            if (r + g + b) not in [3*r, 3*g, 3*b]:
                cnt += 1

print(str(cnt))

Submission status: Time Limit Exceeded

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

print(
    len(
        [(x,y,z)
            for x in [x for x in range(1,N-1)]
            for y in [y for y in range(x+1,N)]
            for z in [z
                        for z in range(y+1,N+1)
                        if z-y!=y-x
                            and S[x-1]!=S[y-1]
                            and S[x-1]!=S[z-1]
                            and S[y-1]!=S[z-1]
                    ]
        ]
    )
)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
r=[]
g=[]
b=[]
for i in range(n):
    tmp=s[i]
    if tmp=="R":
        r.append(i)
    elif tmp=="G":
        g.append(i)
    else:
        b.append(i)
for i in r:
    for j in g:
        for k in b:
            if (i<j and j<k):
                if j-i!=k-j:
                    ans+=1
            elif (i<k and k<j):
                if k-i!=j-k:
                    ans+=1
            elif (j<i and i<k):
                if i-j!=k-i:
                    ans+=1
            elif (j<k and k<i):
                if k-j!=i-k:
                    ans+=1
            elif (k<i and i<j):
                if i-k!=j-i:
                    ans+=1
            elif (k<j and j<i):
                if j-k!=i-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

r = [i for i, x in enumerate(S) if x == "R"]
g = [i for i, x in enumerate(S) if x == "G"]
b = [i for i, x in enumerate(S) if x == "B"]
kotae = 0
# print(r,g,b)
rgb = [r,g,b]
len1 = [len(r),len(g),len(b)]
n3 = len1.index(min(len1))
if n3 == 0:
    n1,n2 = 1,2
elif n3 == 1:
    n1,n2 = 0,2
else:
    n1,n2 = 0,1
# print(len1)
# print(n1,n2,n3)

for i in rgb[n3]:
    k = 1
    y = len(rgb[n1])*len(rgb[n2])
    # print("y = ",y)
    while i-k >=0 or i+k < N+2:
        # print(i,k)
        if i-k in rgb[n1]:
            if i+k in rgb[n2]:
                y -= 1
            if i-2*k in rgb[n2]:
                y-=1
                
        if i-k in rgb[n2]:
            if i+k in rgb[n1]:
                y -= 1
            if i-2*k in rgb[n1]:
                y-=1
                
        if i+k in rgb[n1]:
            if i+2*k in rgb[n2]:
                y -= 1
        if i+k in rgb[n2]:
            if i+2*k in rgb[n1]:
                y -= 1
            
            

        k += 1
    kotae += y
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(str(input()))
from collections import Counter

ans = 0
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        c = Counter(S[j:])
        c_lis = ['R', 'G', 'B']
        c_lis.remove(S[i])
        c_lis.remove(S[j])
        chara = c_lis[0]
        ans += c[chara]
        try:
            if S[i+(j-i)*2]==chara:
                ans-=1
        except:
            pass
print(ans)

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

Submission status: Time Limit Exceeded

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

count = 0
char_candidates = "RGB"
for i in range(len(s)):
    i_char = s[i]

    indexes =  [a+i+1 for a, c in enumerate(s[i+1:]) if c != i_char]
    for j in indexes:
        j_char = s[j]
        
        valid_k_char = char_candidates.replace(s[i], "").replace(s[j], "")

        target_chars = s[j+1:]
        match_count = target_chars.count(valid_k_char)

        if j + j - i < len(s) and s[j + j - i] == valid_k_char:
            match_count -= 1

        count += match_count

print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(sorted(s[i:j+1:(j-i)//2])==list('BGR')for i in range(n-2)for j in range(i+2,n,2)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import itertools

def binary_search(left, right, search, List):
    while right-left > 1:
        mid = (left+right)//2
        if List[mid] == search:
            return mid
        else:
            if search > List[mid]:
                left = mid
            else:
                right = mid
    return -1


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

d = {"R": [], "G": [], "B": []}

for i in range(n):
    d[s[i]].append(i)

p=list(itertools.permutations(["R","G","B"]))

ans = 0
for a,b,c in p:
    for r in range(len(d[a])):
        second = bisect.bisect_left(d[b], d[a][r])
        for g in range(second, len(d[b])):
            third = bisect.bisect_left(d[c], d[b][g])
            ans += len(d[c])-third
            if binary_search(-1,len(d[c]),2*d[b][g]-d[a][r],d[c])!=-1:
                ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = 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 i in r:
        for j in g:
            for k in b:
                rg = abs(i - j)
                gb = abs(j - k)
                br = abs(k - i)

                if rg != gb \
                    and gb != br \
                    and br != rg:
                    ans += 1
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
S = input()
ans = S.count("R") * S.count("G") * S.count("B")
for i in range(n):
    for r in range(1,n):
        if i + r < n and 0 <= i - r:
            if S[i + r] != S[i - r] and S[i] != S[i - r]and S[i] != S[i + r]:
                ans -=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
for i in range(N):
    #print(result)
    for j in range(i + 1, N):
        if S[i] != S[j]:
            target = 'RGB'.strip(S[i]) if S[i] != 'G' else 'RB'
            target = target.strip(S[j])
            result += S.count(target, j + 1, N)
            if 2*j-i < N and S[2 * j - i] == target:
                result -= 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)
    
count = 0

if len(R)>=max(len(G),len(B)):
  X = G
  Y = B
  Z = R
elif len(G)>=len(R):
  X = R
  Y = B
  Z = G
else:
  X = R
  Y = G
  Z = B

for r in X:
  for g in Y:
    len_B = len(Z)
    if (r+g)%2==0 and (r+g)//2 in Z:
      len_B -= 1
    if 2*r-g in Z:
      len_B -= 1
    if 2*g-r in Z:
      len_B -= 1
    count += len_B
      

print(count)

Submission status: Time Limit Exceeded

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

r, g, b = [0] * (n + 1), [0] * (n + 1), [0] * (n + 1)
x, y, z = 0, 0, 0
for i in range(n):
    if s[i] == "R":
        x += 1
    elif s[i] == "G":
        y += 1
    else:
        z += 1
    r[i + 1] = x
    g[i + 1] = y
    b[i + 1] = z

cnt = 0
for i in range(n - 1):
    t = set(["R", "G", "B"])
    t -= set(s[i])
    for j in range(i + 1, n):
        a = 0
        tmp = t - set(s[j])
        if tmp == set("R"):
            a = r[n] - r[j + 1]
            if 2 * j - i < n:
                if s[2 * j - i] == "R":
                    a -= 1
        elif tmp == set("G"):
            a = g[n] - g[j + 1]
            if 2 * j - i < n:
                if s[2 * j - i] == "G":
                    a -= 1
        elif tmp == set("B"):
            a = b[n] - b[j + 1]
            if 2 * j - i < n:
                if s[2 * j - i] == "B":
                    a -= 1
        cnt += a
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
S = np.array(list(input()))

R = (S=='R').sum()
G = (S=='G').sum()
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      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()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            sb = 0
            sc = 0
            flgb = 0
            for a in range(len(L[i])):
                flgc = 0
                for b in range(sb, len(L[j])):
                    if L[i][a] <= L[j][b]:
                        if flgb == 0:
                            sb = b
                            flgb = 1
                        for c in range(sc, len(L[k])):
                            if L[j][b] <= L[k][c]:
                                if flgc == 0:
                                    sc = c
                                    flgc = 1
                                if L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=input()
S={"R":[],"G":[],"B":[]}
for i,s in enumerate(input()):
	S[s].append(i)

lis = [sorted([a, b, c]) for a in S["R"] for b in S["G"] for c in S["B"]]
ans = 0
for i,j,k in lis:
  if j-i!=k-j:
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

res = s.count('R') * s.count('G') * s.count('B')

for i in range(n):
    for j in range(i+1, n-1):
        k = j + (j - i)
        if k < n:
            if len(set([s[i], s[j], s[k]])) == 3:
                res -= 1
        else:
            break

print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
r=[]
g=[]
b=[]
for i in range(n):
    tmp=s[i]
    if tmp=="R":
        r.append(i+1)
    elif tmp=="G":
        g.append(i+1)
    else:
        b.append(i+1)
for i in r:
    for j in g:
        for k in b:
            if (i<j and j<k):
                if j-i!=k-j:
                    ans+=1
            elif (i<k and k<j):
                if k-i!=j-k:
                    ans+=1
            elif (j<i and i<k):
                if i-j!=k-i:
                    ans+=1
            elif (j<k and k<i):
                if k-j!=i-k:
                    ans+=1
            elif (k<i and i<j):
                if i-k!=j-i:
                    ans+=1
            elif (k<j and j<i):
                if j-k!=i-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    R = [0] * N
    G = [0] * N
    B = [0] * N
    rcnt = 0
    gcnt = 0
    bcnt = 0
    rgb = {
        ('R', 'G'):'B',
        ('G', 'R'):'B',
        ('R', 'B'):'G',
        ('B', 'R'):'G',
        ('G', 'B'):'R',
        ('B', 'G'):'R'
    }
    for i, s in enumerate(S):
        if s == 'R':
            rcnt += 1
        elif s == 'G':
            gcnt += 1
        elif s == 'B':
            bcnt += 1
        R[i] = rcnt
        G[i] = gcnt
        B[i] = bcnt
    # print(R, rcnt)
    # print(G, gcnt)
    # print(B, bcnt)
    ans = 0
    for i in range(N-1):
        for j in range(i+1, N):
            # print('i = {}:{}, j = {}:{}'.format(i, S[i], j, S[j]), end=', ')
            if S[i] == S[j]:
                # print('S[i] == S[j] == {}'.format(S[i]))
                continue
            s = rgb[S[i], S[j]]
            if s == 'R':
                ans += rcnt - R[j]
                # print('ans += {}'.format(rcnt - R[j]))
            elif s == 'G':
                ans += gcnt - G[j]
                # print('ans += {}'.format(gcnt - G[j]))
            elif s == 'B':
                ans += bcnt - B[j]
                # print('ans += {}'.format(bcnt - B[j]))
            tn = j + (j-i)
            if tn < N and S[tn] == s:
                ans -= 1

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations, product

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

indices_R = [i for i in range(N) if S[i] == "R"]
indices_G = [i for i in range(N) if S[i] == "G"]
indices_B = [i for i in range(N) if S[i] == "B"]

count = 0
for ijk in product(indices_R, indices_G, indices_B):
    i, j, k = sorted(ijk)
    if j - i != k - j:
        count += 1

print(count)

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

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
ans = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    if S[i] == "G":
        G.append(i)
    if S[i] == "B":
        B.append(i)
rem = 0
for r in R:
    for g in G:
        if g - abs(r - g) in B:
            rem += 1
        if r - abs(g - r) in B:
            rem += 1
        if (g + r) / 2 in B:
            rem += 1
        if g + abs(r - g) in B:
            rem += 1
        if r + abs(g - r) in B:
            rem += 1


print(len(R)*len(G)*len(B) - rem)

Submission status: Time Limit Exceeded

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

res = 0

Rs = [i for i in range(N) if S[i] == 'R']
Gs = [i for i in range(N) if S[i] == 'G']
Bs = [i for i in range(N) if S[i] == 'B']

for r in Rs:
  for g in Gs:
    for b in Bs:
      i, j, k = sorted([r, g, b])
      if j - i != k - j:
        res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
S = input()
dum = []
d = 0
while 3 + d*2 <= N:
    for i in range(N - 2*(d+1)):
        dum.append([S[i], S[i+d+1], S[i+2*(d+1)]])
    d += 1
dum_n = len([x for x in dum if len(set(x)) == 3])

RGB_Counter = Counter(S)
print(RGB_Counter['R'] * RGB_Counter['G'] * RGB_Counter['B'] -dum_n)

Submission status: Time Limit Exceeded

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

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

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
    
    l_useable = [] 
    r_useable = [] 
    for ii in l_col:
      if ii<ind:
        l_useable += [ii]
      else:
        break
    if l_useable == []:
      continue
    for ii in reversed(r_col):
      if ii>ind:
        r_useable += [ii]
      else:
        break
    if r_useable == []:
      continue

    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
    # print(len(l_useable))
    # print(len(r_useable))
  return n

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

print(n)

Submission status: Time Limit Exceeded

Solution code:
#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N = int(input())
S = input()
result = 0
a = 1
while (a <= N -3):
    b = a + 1
    while (b <= N - 1):
        c = b + 1
        if (S[a - 1] == S[b - 1]):
            b += 1
            continue
        diff = b - a
        while (c <= N):
            if (diff == (c - b) or S[a - 1] == S[c - 1] or S[b - 1] == S[c - 1]):
                c += 1
                continue
            result += 1
            c += 1
        b += 1
    a += 1
print(result)

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)):
        for k in range(j+1, len(Slist)):
            if(Slist[i] == Slist[j]):
                break
            notEqualColor = (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:
import math
import sys
import collections
def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
       if Tr[i]=="G":
           G.append(i)
       if Tr[i]=="B":
           B.append(i)
    cnt=0
    for i in R:
        for j in G:
            
            for k in B:
                if max(i,j,k)-(i+j+k-max(i,j,k)-min(i,j,k))!=(i+j+k-max(i,j,k)-min(i,j,k))-min(i,j,k):
                    cnt+=1

    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
num_R = sum(int(val=="R") for val in s)
num_G = sum(int(val=="G") for val in s)
num_B = sum(int(val=="B") for val in s)
total_triplets = num_R*num_G*num_B
num_missing = 0
for centre_point in range(1, n-1):
  for halfwidth in range(1, 1+min(centre_point, n-centre_point-1)):
    first_possible = s[centre_point-halfwidth] != s[centre_point]
    second_possible = s[centre_point] != s[centre_point+halfwidth]
    third_possible = s[centre_point-halfwidth] != s[centre_point+halfwidth]
    if first_possible and second_possible and third_possible: num_missing += 1
print(total_triplets-num_missing)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
Rlist=[]
Glist=[]
Blist=[]

for i in range(N):
    if S[i] == "R":
        Rlist.append(i)
    elif S[i] == "G":
        Glist.append(i)
    elif S[i] == "B":
        Blist.append(i)

for i in range(len(Rlist)):
    for j in range(len(Glist)):
        for k in range(len(Blist)):
            if abs(Rlist[i]-Glist[j]) != abs(Glist[j]-Blist[k]) and abs(Rlist[i]-Glist[j]) !=abs(Rlist[i]-Blist[k]) and abs(Rlist[i]-Blist[k])!=abs(Glist[j]-Blist[k]):
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

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 = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        i, j = r, g
        if i > j:
            i, j = j, i
        
        s = j-i
        if s%2 == 0:
            if i+s//2 in B:
                ans -= 1
        if i-s in B:
            ans -= 1
        if j+s in B:
            ans -= 1
            
# output
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from math import comb
# def comb(n,r):
#     if r==2:
#         return n*(n-1)//2
#     else:
#         return n*(n-1)*(n-2)//6
n=n0()
s=s0()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=0
for j in range(0,n):
    for i in range(1,(n-j)//2+1):
        if j+2*i>n-1:
            continue
        else:
            ans+=1
            if s[j]==s[j+i]==s[j+2*i]:
                ans-=1
            elif s[j]==s[j+i] or s[j]==s[j+2*i] or s[j+i]==s[j+2*i]:
                ans-=1
            
ans+=comb(r,3)+comb(g,3)+comb(b,3)+comb(r,2)*(g+b)+comb(g,2)*(r+b)+comb(b,2)*(r+g)
print(comb(n,3)-ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    d = {'R': [], 'G': [], 'B': []}

    for i, c in enumerate(s):
        d[c].append(i)

    ans = 0

    perms = (('R', 'G', 'B'), ('R', 'B', 'G'), ('B', 'R', 'G'), ('B', 'G', 'R'), ('G', 'R', 'B'), ('G', 'B', 'R'))

    for p1, p2, p3 in perms:
        for i in d[p1]:
            for j in d[p2]:
                for k in d[p3]:
                    if j > i and k > j and k-j != j-i:
                        ans +=1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)
    
count = 0

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

Submission status: Time Limit Exceeded

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

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

print(res)

Submission status: Time Limit Exceeded

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

for  i in range(n):
    for j in range(i+1,n):
        flag = 0
        if s[i] == s[j]:
            continue
        if 2*j-i < n and s[2*j-i] != s[i] and s[2*j-i] != s[j]:
            flag = 1
        
        s_new = s[j+1:]
        sum += n - j-1 - s_new.count(s[i]) - s_new.count(s[j]) - flag
        #print(s_new, flag, sum)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
from sys import stdin
import sys
sys.setrecursionlimit(10000)

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()


mapR = []
mapG = []
mapB = []

for i in range(len(s)):
    if s[i] == "R":
        mapR.append(i)
    elif s[i] == "G":
        mapG.append(i)
    else:
        mapB.append(i)

#print(mapR, mapG, mapB)


total = len(mapR)*len(mapG)*len(mapB)

for i in range(len(mapR)):
    for j in range(len(mapG)):
        m1, m2 = sorted((mapR[i], mapG[j]))
        for k in range(len(mapB)):
            mm =  mapB[k]
            #print(m1,m2,mm)
            if mm < m1:
                if m1 - mm == m2 - m1:
                    #print("hit lowlow",i,j,k,mapR[i],mapG[j],mapB[k])
                    total -= 1
            elif mm < m2:
                if mm - m1 == m2 - mm:
                    #print("hit middle",i,j,k,mapR[i],mapG[j],mapB[k])
                    total -= 1
            elif mm - m2 == m2 - m1:
                #print("hit highgh",i,j,k,mapR[i],mapG[j],mapB[k])
                total -= 1
            elif mm - m2 > m2 - m1:
                break
print(total)
'''
sum = 0
jpos = 0
kpos = 0
def countAns(map1,map2,map3):
    sum = 0
    jpos=0
    kpos=0
    for i in range(len(map1)):
        for j in range(jpos,len(map2)):
            if map2[j] < map1[i]:
                continue
            for k in range(kpos,len(map3)):
                if map3[k] < map2[j]:
                    continue
                if map2[j] - map1[i] != map3[k] - map2[j]:
                    sum+=1
    return sum


sum += countAns(mapR, mapG, mapB)
sum += countAns(mapR, mapB, mapG)
sum += countAns(mapG, mapR, mapB)
sum += countAns(mapG, mapB, mapR)
sum += countAns(mapB, mapG, mapR)
sum += countAns(mapB, mapR, mapG)
print(sum)

sum = 0
for i in range(len(s)):
    if s[i] == "R":
        map1 = mapG
        map2 = mapB
    elif s[i] == "G":
        map1 = mapR
        map2 = mapB
    else:
        map1 = mapR
        map2 = mapG

    for j in range(jpos,len(map1)):
        if map1[j] < i:
            continue
        for k in range(kpos,len(map2)):
            if map2[k] < i:
                continue
            jj,kk = sorted((map1[j],map2[k]))
            if (i-jj) != (jj-kk):
                #print("hit",i,map1[j],map2[k], i-map1[j], i-map2[k])
                sum+= 1 
print(sum)

sum=0
for i in range(len(mapR)):
    for j in range(len(mapG)):
        for k in range(len(mapB)):
            (ii, jj, kk) = sorted((mapR[i], mapG[j], mapB[k]))
            if jj-ii != kk-jj:
                sum += 1

print(sum)
'''

Submission status: Time Limit Exceeded

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