Problem:


Score : 400 points

Problem Statement

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

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

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

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

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

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    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 r in R:
        for g in G:
            for b in B:
                rg = abs(r-g)
                gb = abs(g-b)
                br = abs(b-r)
                if rg != gb and gb != br and br != rg:
                    ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

i_range = [int(x) for x in range(1, N-1)]
j_range = [int(x) for x in range(2, N)]
k_range = [int(x) for x in range(3, N+1)]

val = 0

for k in k_range:
    for j in j_range:
        if(j >= k):
            break
        for i in i_range:
            if(i >= j):
                break
            if((S[i-1] != S[j-1]) and (S[j-1] != S[k-1]) and (S[i-1] != S[k-1]) and (j-i != k-j)):
                val = val + 1


print(val)

Submission status: Time Limit Exceeded

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

count=0

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

Submission status: Time Limit Exceeded

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

for k in range(N, 2, -1):
    for j in range(k-1, 1, -1):
        for i in range(j-1, 0, -1):
            if j-i != k-j:
                if S[k-1:k] != S[j-1:j] and S[k-1:k] != S[i-1:i] and S[j-1:j] != S[i-1:i]:
                    ans += 1
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 = len(R) * len(G) * len(B)
for i in R:
    for j in G:
        if abs(j-i) % 2 == 1:
            k1 = min(j, i) - abs(j-i)
            k2 = max(j, i) + abs(j-i)
            if k1 >= 0:
                if S[k1] == "B":
                    ans -= 1
            if k2 <= N-1:
                if S[k2] == "B":
                    ans -= 1
        else:
            k1 = min(j, i) - abs(j-i)
            k2 = max(j, i) + abs(j-i)
            k3 = min(j, i) + abs(j-i)//2
            if k1 >= 0:
                if S[k1] == "B":
                    ans -= 1
            if k2 <= N-1:
                if S[k2] == "B":
                    ans -= 1
            if S[k3] == "B":
                ans -= 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy


def main():
    N = int(input())
    S = input()
    count = 0
    RGB = ["R", "G", "B"]

    for i in range(1, N-1):
        RGB = ["R", "G", "B"]
        RGB.remove(S[i - 1])

        for j in range(i+1, N):
            RGB_2 = copy.deepcopy(RGB)
            if S[i-1] == S[j-1]:
                continue
            #print(f"before_remove {RGB_2}, remove{S[j-1]}")
            RGB_2.remove(S[j-1])
            count += S.count(RGB_2[0], j, N)
            #print(S.count(RGB[0], j, N))
            if 2 * j - i > N:
                pass
            else:
                if S[2 * j - i - 1] == RGB_2[0]:
                    count -= 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
s=input()
x=[]
y=[]
z=[]
for i in range(a):
    if s[i]=="R":
        x+=[i]
    elif s[i]=="B":
        y+=[i]
    else:
        z+=[i]
count=len(x)*len(y)*len(z)
for i in x:
    for j in y:
        p=2*i-j
        q=2*j-i
        r=(i+j)/2
        t=0
        if 0<=p<=a:
            if p in z:
                t+=1
        if 0<=q<=a:
            if q in z:
                t+=1
        if int(r)==r:
            if int(r) in z:
                t+=1
        count-=t
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str, input()))
f=0
i_c = j_c = k_c =1
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k+1>j+1 and
               j+1>i+1 and
               j-i!=k-j and
               S[i]!=S[j] and 
               S[i]!=S[k] and
               S[j]!=S[k]):
                    f+=1
 
print(f)

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    N = int(_input[0])
    S = _input[1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    for i,r in enumerate(R):
       if r:
           for j,g in enumerate(G[i+1:]):
               if g:
                   for k,b in enumerate(B[i+j+2:]):
                       if b and j != k:
                           cnt += 1
           for j,g in enumerate(B[i+1:]):
               if g:
                   for k,b in enumerate(G[i+j+2:]):
                       if b and j !=k:
                           cnt += 1
    for i,r in enumerate(G):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                   for k,b in enumerate(B[i+j+2:]):
                       if b and j !=k:
                           cnt += 1
           for j,b in enumerate(B[i+1:]):
               if b:
                   for k,g in enumerate(R[i+j+2:]):
                       if g and j !=k:
                           cnt += 1
    for i,r in enumerate(B):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                   for k,b in enumerate(G[i+j+2:]):
                       if b and j !=k:
                           cnt += 1
           for j,b in enumerate(G[i+1:]):
               if b:
                   for k,g in enumerate(R[i+j+2:]):
                       if g and j !=k:
                           cnt += 1
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  rg,ans=r+g,0
  b=[i for i in range(n) if not i in rg]
  R,G=len(r),len(g)
  for i in r:
    for j in g:
      ans+=n-R-G +(-1 if (i+j)/2 in b else 0) 
      x,y=((i+j)+abs(i-j)*3)/2,((i+j)-abs(i-j)*3)/2
      ans+=-1 if x in b else 0
      ans+=-1 if y in b else 0
  print(ans)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:

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

cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=input()
c=0
for i in range(N-2):
 B2=A[i+1]
 B1B=0
 B1R=0
 B1G=0
 for j in range(i+1):
  if A[j]=="B":
   B1B+=1
  elif A[j]=="R":
   B1R+=1
  else:
   B1G+=1
 for k in range(N-i-2):
  B3=A[i+2+k]
  if (B2=="B" and B3=="R")or (B2=="R" and B3=="B"):
   c+=B1G
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="G":
     c-=1
  if (B2=="G" and B3=="R")or (B2=="R" and B3=="G"):
   c+=B1B
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="B":
     c-=1
  if (B2=="B" and B3=="G")or (B2=="G" and B3=="B"):
   c+=B1R
   if (i+2+k)<=(i+1)*2:
    if A[(i+1)*2-(i+2+k)]=="R":
     c-=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
X = int(input())
S = str(input())
a = 0

for i in range(X-1):
   for j in range(i+1,X):
      Sj = S[j]
      Si = S[i]
      if Sj != Si:
        z = j - i
        for k in range(j+1,X):
          if z != (k - j) and S[k] != Si and S[k] != Sj:
            a = a + 1
        
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#
from collections import deque


def main():
    n = int(input())
    # これだとTLE
    # s = [s for s in input()]
    # count = 0
    # ps = ""
    # pc = 0
    # for j in range(1, n):
    #     if ps == s[j]:
    #         count += pc
    #         continue
    #     pc = 0
    #     for i in range(j - 1, -1, -1):
    #         if s[i] != s[j]:
    #             offset = j - i
    #             for k in range(j + 1, n):
    #                 if s[k] != s[j] and s[k] != s[i]:
    #                     if k - j != offset:
    #                         pc += 1
    #     count += pc
    # print(count)
    # これもTLEだけどちょっと効率いい
    s = deque()
    r = deque()
    g = deque()
    b = deque()
    for i, c in enumerate(input()):
        s.append(c)
        if c == "R":
            r.append(i)
        elif c == "G":
            g.append(i)
        else:
            b.append(i)
    count = 0
    t = []
    x = r
    for w in range(3):
        if w == 0:
            x = r
            t = [g, b]
        elif w == 1:
            x = g
            t = [r, b]
        else:
            x = b
            t = [r, g]
        for j in x:
            if s[j] == "R":
                t = [g, b]
            elif s[j] == "G":
                t = [r, b]
            else:
                t = [r, g]
            for z in range(2):
                for i in t[z]:
                    if i >= j:
                        break
                    offset = j - i
                    for k in t[1 - z]:
                        if k <= j:
                            continue
                        if offset != k - j:
                            count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    ans = 0
    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2
            central = (max_i + min_i) / 2

            tgt_b_arr = max_arr[::]

            if minmin in tgt_b_arr:
                tgt_b_arr.remove(minmin)
            if maxmax in tgt_b_arr:
                tgt_b_arr.remove(maxmax)
            if exists_central == 0:
                if central in tgt_b_arr:
                    tgt_b_arr.remove(central)
            ans += len(tgt_b_arr)

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from copy import copy
  
def main():
    n = int(input())
    s = input()
    RGB = ['R', 'G', 'B']
    
    rgb_cnt = [0,0,0]
    for i in range(n-1, -1, -1):
        if s[i] == 'R':
            rgb_cnt[0] += 1
        if s[i] == 'G':
            rgb_cnt[1] += 1
        if s[i] == 'B':
            rgb_cnt[2] += 1
            
    sumv = 0
    rgb_cnt_cp = copy(rgb_cnt)
    for i in range(n-2):
        
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb_cnt_cp[rgb_idx] -= 1
        rgb_cnt_cp2 = copy(rgb_cnt_cp)
        
        #print(rgb_cnt_cp2)
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb_idx2 = rgb.index(c2)
            rgb_cnt_cp2[rgb_idx2] -= 1
            #print(rgb_idx2)
            
            rgb2 = copy(RGB)
            rgb2.pop(rgb2.index(c1))
            rgb2.pop(rgb2.index(c2))
            c3 = rgb2[0]
            
            c3idx = RGB.index(c3)
            #print(rgb_cnt_cp2[c3idx])
            sumv += rgb_cnt_cp2[c3idx]
            
            
        #print(rgb_cnt_cp2)
        
    for i in range(n-1):
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb.pop(rgb.index(c1))
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb2 = copy(rgb)
            rgb2.pop(rgb.index(c2))
            c3 = rgb2[0]
            
            k = j + (j-i)
            
            if k > n - 1:
                break
            if c3 == s[k]:
                #print(i,j,k)
                sumv -= 1
            
        
    print(sumv)
    #print(rgb_cnt)
        
  
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

count = 0
for s in S:
    if s == "R":
        R.append(count)
    elif s == "G":
        G.append(count)
    elif s == "B":
        B.append(count)
    count += 1

def checker(i,E,F):
    wa =0
    EE = [x for x in E if x > i]
    FF = [x for x in F if x > i]
    for e in EE:
        for f in FF:
            if i>e or i > f:
                pass
            else:
                if f > e and e-i != f-e:
                    wa += 1
                elif e > f and f-i != e-f:
                    wa += 1
    return(wa)

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

print(goukei)

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)
if max(len(r),len(g),len(b)) == len(b):
  for i in g:
    for j in r:
      res += len(b) - int((i + j) / 2 in b) - int(2 * i - j in b) - int(2 * j - i in b)
else:
  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=input()
sum=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[k] and S[j]!=S[k]:
                    if j-i!=k-j:
                        sum =sum+1
print(sum)

Submission status: Time Limit Exceeded

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

for a in range(N-2):
  for b in range(a+1,N-1):
    if S[a] != S[b]:
        for c in range(b+1,N):
          if a-b != b-c:
            if S[b] != S[c] and S[c] != S[a]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

counter = 0

for i in range(n-3+1):
  w1_list  = []
  w1_list.append(s[i])
  
  for j in range(i+1, n-3+2):
    w2_list  = w1_list.copy()
    if s[j] not in w1_list:
      w2_list.append(s[j])
      
      for k in range(j+1, n-3+3):
        if s[k] not in w2_list and (j-i)!=(k-j):
          counter += 1
    else:
      continue

print(counter)

Submission status: Time Limit Exceeded

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

R_inds = [0] * n
G_inds = [0] * n
B_inds = [0] * n

for i in range(n):
    if s[i] == 'R':
        R_inds[i] = i
    elif s[i] == 'G':
        G_inds[i] = i
    else:
        B_inds[i] = i
R_inds = list(set(R_inds))
G_inds = list(set(G_inds))
B_inds = list(set(B_inds))

if s[0] == 'R':
    G_inds.remove(0)
    B_inds.remove(0)
elif s[0] == 'G':
    R_inds.remove(0)
    B_inds.remove(0)
else:
    R_inds.remove(0)
    G_inds.remove(0)

G_len = len(G_inds)
B_len = len(B_inds)
R_len = len(R_inds)

if G_len >= B_len and G_len >= R_len:
    largest = G_inds
    a_inds = B_inds
    b_inds = R_inds
elif B_len >= G_len and B_len >= R_len:
    largest = B_inds
    a_inds = G_inds
    b_inds = R_inds
else:
    largest = R_inds
    a_inds = G_inds
    b_inds = B_inds
    
ans = 0
for r in a_inds:
    for g in b_inds:
        big = max(r,g)
        small = min(r,g)
        bigger = big + (big - small)
        smaller = small - (big - small)
        if (big - small) % 2 == 0:
            middle = (big + small) // 2
        else:
            middle = -1
        
        subans = 0
        subans += len(largest)
        if smaller in largest:
            subans -= 1
        if bigger in largest:
            subans -= 1
        if middle in largest:
            subans -= 1
        if subans > 0:
            ans += subans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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)
    elif S[i] == "B":
        B.append(i)
    #if i / 2 in R or i / 2 in G or i / 2 in B
count = len(R)*len(G)*len(B)


for a in R:
    for b in G:
        for c in B:
            num = [a, b, c]
            num.sort()
            if 2 * num[1] - num[0] == num[2]:
                count -= 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())  # N<= 4000 6*10^10なので、削減しないとだめ
S = list(input())


# Si Sj Skがどれも別のもの。ただしi<j<k
# また、j-i != k-j
# 1,2,3はだめ1,2,4はok
# 1,3,5もだめ
# この組の数を求める

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


R_indexes = []
G_indexes = []
B_indexes = []
for i, char in enumerate(S):
    if char == 'R':
        R_indexes.append(i + 1)
    elif char == 'G':
        G_indexes.append(i + 1)
    else:
        B_indexes.append(i + 1)
# print(permutation_hash)
count = 0
R_indexes_count = len(R_indexes)
G_indexes_count = len(G_indexes)
B_indexes_count = len(B_indexes)
for R_index in R_indexes:
    for G_index in G_indexes:
        middle_index = (R_index + G_index) / 2
        remove_conut = 0
        if middle_index != R_index and middle_index != G_index:
            if middle_index in B_indexes:
                remove_conut += 1
        div = abs(G_index - R_index)
        if G_index < R_index:
            if G_index - div in B_indexes:
                remove_conut += 1
            if R_index + div in B_indexes:
                remove_conut += 1
        else:
            if G_index + div in B_indexes:
                remove_conut += 1
            if R_index - div in B_indexes:
                remove_conut += 1
        count += (B_indexes_count - remove_conut)

print(count)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n-1):
  for p in range(1,n):
    if i + p > n-1:
      break
      
    for q in range(1,n):
      if i+ p+ q > n-1:
        break
      elif p != q:
        j = i + p
        k = j + q
        
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
          count += 1

print(count)

Submission status: Time Limit Exceeded

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

out = 0

def rgb (tmpstr, j, i, s):
    if s[j] != s[i]:
        tmpstr = s[j + 1 :  n]
        tmpset = set(["R","G","B"])
        tmpset.remove(s[j])
        tmpset.remove(s[i])
        remain = list(tmpset)[0]
        num = tmpstr.count(remain) 
        if (j + (j - i)) < len(s):
            if  s[j + (j - i)]  == remain:
                num = num - 1
        return num
    else:
        return 0
    
    
for i in range(n-2):
    out += sum([rgb(s[j + 1 :  n], j, i, s) for j in range(i+1, n-1)])
print(out)

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
 
for i in range(len(R)):
  for j in range(len(G)):
    temp = 0
    if 2*R[i] - G[j] in B:
      temp += 1
    if 2*G[j] - R[i] in B:
      temp += 1
    if (R[i]+G[j])/2 in B:
      temp += 1
    ans += len(B)- temp
 
print(ans)

Submission status: Time Limit Exceeded

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

rList = []
gList = []
bList = []

for indx in range(N):
    if S[indx] == 'R':
        rList.append(indx)
    elif S[indx] == 'G':
        gList.append(indx)
    elif S[indx] == 'B':
        bList.append(indx)

NoSum = 0
fList = rList
sList = gList
remain = 'B'

for fItem in fList:
    for sItem in sList:
        if ((2 * max(fItem, sItem) - min(fItem, sItem)) <= N-1):
            if (S[(2 * max(fItem, sItem) - min(fItem, sItem))] == remain):
                NoSum += 1
        
        if ((max(fItem, sItem) - min(fItem, sItem)) % 2 == 0):
            if (S[int((max(fItem, sItem) + min(fItem, sItem))/2)] == remain):
                NoSum += 1
        
        if ((2 * min(fItem, sItem) - max(fItem, sItem)) >= 0):
            if  (S[(2 * min(fItem, sItem) - max(fItem, sItem))] == remain):
                NoSum += 1

print(len(rList) * len(gList) * len(bList) - NoSum)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets

n = int(input())
s = str(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']

ans = 0

for r in R:
    for g in G:
        for b in B:
            mi = min(r, g, b)
            ma = max(r, g, b)
            if (r+g+b)==mi+ma+(mi+ma)/2:
                continue
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from numba import njit

# @njit
# def regular_intervals(r,g,b):
#     i,j,k = sorted([r,b,g])
#     if k-j == j-i:
#         return True
#     else:
#         return False


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

Rids = []
Gids = []
Bids = []

for i,c in enumerate(S):
    if c == 'R':
        Rids.append(i)
    elif c == 'G':
        Gids.append(i)
    elif c == 'B':
        Bids.append(i)

# print(sum(not regular_intervals(r,g,b) for r in Rids
#               for g in Gids
#               for b in Bids))

# print(sum(abs(r-b) != abs(b-g) and abs(b-g) != abs(g-r) and abs(g-r) != abs(r-b)
#           for r in Rids
#           for g in Gids
#           for b in Bids))

# print(sum(len(set((abs(r-b), abs(b-g), abs(g-r)))) == 3
#           for r in Rids
#           for g in Gids
#           for b in Bids))

ans = 0
for r in Rids:
    for g in Gids:
        diff_rg = abs(r-g)
        for b in Bids:
            if (diff_rb := abs(r-b)) == diff_rg:
                continue
            elif (diff_gb := abs(g-b)) == diff_rg:
                continue
            # elif abs(r-b) == abs(g-b):
            elif diff_rb == diff_gb:
                continue
            else:
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

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

#19ms

Submission status: Time Limit Exceeded

Solution code:
import bisect
#import time
#start = time.time()

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)
#process_time = time.time() - start
#print(process_time)

Submission status: Time Limit Exceeded

Solution code:
n = int( input() )
r = []
g = []
b = []
string = input()
for i , j in enumerate(string):
    if j == "R":
        r.append(i)
    
    if j == "G":
        g.append(i)
    
    if j == "B":
        b.append(i)

total = 0 

for i in r :
    
    for j in g:
        
        if j > i:
            continue
        
        for k in b:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 

for i in r : 
    
    for j in b:
        
        if j > i:
            continue
        
        for k in g:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 
for i in g: 
    
    for j in b:
        
        if j > i:
            continue
        
        for k in r:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 

for i in g: 
    
    for j in r:
        
        if j > i:
            continue
        
        for k in b:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 

for i in b: 
    
    for j in g:
        
        if j > i:
            continue
        
        for k in r:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 
            

for i in b: 
    
    for j in r:
        
        if j > i:
            continue
        
        for k in g:
            if k > j:
                continue 
            if (j - i) != (k - j):
                total += 1 
 
print(total)

Submission status: Time Limit Exceeded

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

ri = -1
gi = -1
bi = -1

count =  0

lri = [i for i, rgb in enumerate(s) if rgb == 'R']
lgi = [i for i, rgb in enumerate(s) if rgb == 'G']
lbi = [i for i, rgb in enumerate(s) if rgb == 'B']

for r in lri:
    for g in lgi:
        for b in lbi:
            l = sorted([r, g, b])
            if l[2] - l [1] != l[1] - l[0]:
                count += 1

print(count)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
s = input()

count = 0

for i in range(0, n-2):
    for j in range(i, n-1):
        j_i = j - i
        s_i = s[i]
        s_j = s[j]

        if (s_i != s_j):
            for k in range(j, n):
                k_j = k - j
                s_k = s[k]

                if (s_i != s_k) and (s_j != s_k) and (j_i != k_j):
                    count = count + 1

print(count)

Submission status: Time Limit Exceeded

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

ans = 0

for x in range(n):
  for y in range(n):
    if not x < y:
      continue
    if s[x] == s[y]:
      continue
    for z in range(n):
      if not x < y < z:
        # print("SKIP: ", x, y, z)
        continue

      if y-x == z-y:
        # print("SKIP: ", x, y, z)
        continue

      if not ((s[x] != s[y])  and (s[x] != s[z]) and (s[y] != s[z])):
        # print("SKIP: ", x, y, z)
        continue

      ans += 1
      # print(x, y, z)
      # print(s[x], s[y], s[z])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

i = 0
for r in np.arange(S.size)[S == "R"]:
    for g in np.arange(S.size)[S == "G"]:
        for b in np.arange(S.size)[S == "B"]:
            if len(set(np.diff(sorted([r, g, b])))) == 1:
                continue
            i += 1
print(i)