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

Submission status: Time Limit Exceeded

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

r=s.count('R')
g=s.count('G')
b=s.count('B')

ng=0

all=r*g*b

#NGパターン数の洗い出し
for i in range(0,n-2):
  for j in range(i,n-1):
    for k in range(j,n):
#      print(i,j,k)
      if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
        if j-i==k-j:
          ng+=1
print(all-ng)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            if S[i]!=S[j]:
                for l in S[j+1:]:
                    if l!=S[i] and l!=S[j]:
                        ans+=1
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)
main()

Submission status: Time Limit Exceeded

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

Rs = 0
Gs = 0
Bs = 0
for il in S:
    if il == 'R':
        Rs += 1
    if il == 'G':
        Gs += 1
    if il == 'B':
        Bs += 1
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(Rs*Gs*Bs - num)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            lis=["R","G","B"]
            if S[i]!=S[j]:
                lis=["R","G","B"]
                lis.remove(S[i])
                lis.remove(S[j])
                k=S[j+1:].count(lis[0])
                ans=ans+k
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            lis=["R","G","B"]
            if S[i]!=S[j]:
                lis=["R","G","B"]
                lis.remove(S[i])
                lis.remove(S[j])
                k=S[j+1:].count(lis[0])
                ans=ans+k
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)
 
main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N = list(map(int, (stdin.readline().strip().split())))[0]
S= list(stdin.readline().strip())
ans=0
for i in range(1,N+1):
    for j in range(i+1,N+1):
        for k in range(j+1,N+1):
            if j-i !=k-j:
                if S[i-1] != S[j-1] and S[j-1] !=S[k-1] and S[i-1] != S[k-1]:
                    ans+=1
print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()
S = input()

"""
    Conditions:
        1. S[i] \ne S[j] and S[j] \ne S[k] and S[k] \ne S[i]
        2. j - i \ne k - j
"""

r = S.count('R')
g = S.count('G')
b = S.count('B')
# 条件1を満たす組は rgb 個。それから「条件1を満たしてかつ条件 2 を満たさない」組を引く。条件を満たさないもののほうが多い。


cnt = 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: continue # 条件2を満たす場合は break
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                cnt += 1

print(r * g * b - cnt)

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

print(ans)

Submission status: Time Limit Exceeded

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

#output
from collections import Counter
answer = 0
if N > 3:
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if S[i] == "R":
                if S[j] == "G":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
            elif S[i] == "G":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
            elif S[i] == "B":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
                elif S[j] == "G":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
                
print(answer)

Submission status: Time Limit Exceeded

Solution code:
str_len = int(input().rstrip())
str = input().rstrip()

rgb_nums_dict = {'R':str.count('R'), 'G':str.count('G'), 'B':str.count('B')}
total = 0
for i in range(str_len-2):
    for j in range(i+1,str_len-1):
        first_str = str[i]
        second_str = str[j]
        if first_str==second_str:
            continue
        else:
            if 2*j-i<str_len and second_str!=str[2*j-i]!=first_str:
                total-=1
                


for i in range(str_len):
    first_str = str[i]
    if first_str=='R':
        total += rgb_nums_dict['G']*rgb_nums_dict['B']
        
    elif first_str=='G':
        total += rgb_nums_dict['R']*rgb_nums_dict['B']
        
    else:#'B'
        total += rgb_nums_dict['R']*rgb_nums_dict['G']
        
    rgb_nums_dict[first_str]-=1
    if rgb_nums_dict[first_str]==0:
        break
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
str_len = int(input().rstrip())
str = input().rstrip()

rgb_nums_dict = {'R':str.count('R'), 'G':str.count('G'), 'B':str.count('B')}
total = 0
for i in range(str_len-2):
    for j in range(i+1,str_len-1):
        first_str = str[i]
        second_str = str[j]
        if first_str==second_str:
            continue
        else:
            if 2*j-i<str_len and second_str!=str[2*j-i]!=first_str:
                total-=1
                

for letter in str:
    first_str = letter
    
    if first_str=='R':
        total += rgb_nums_dict['G']*rgb_nums_dict['B']
        
    elif first_str=='G':
        total += rgb_nums_dict['R']*rgb_nums_dict['B']
        
    else:#'B'
        total += rgb_nums_dict['R']*rgb_nums_dict['G']
        
    rgb_nums_dict[first_str]-=1
    if rgb_nums_dict[first_str]==0:
        break

print(total)

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):
            
            if j-i != k-j and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

lst = list(product(r,g,b))
ans = len(lst)
for i in range(len(lst)):
    tmp = sorted(lst[i])
    if tmp[1]-tmp[0] == tmp[2]-tmp[1]:
        ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
cnt=0
for i in range(n):
  if s[i]=='R':
    r.append(i)
  if s[i]=='G':
    g.append(i)   
  if s[i]=='B':
    b.append(i)   
if len(r)*len(g)*len(b)==0:
  print(0)
else:
  tmpa=0
  tmpb=0
  tmpc=0
  cnt=len(r)*len(g)*len(b)
  for i in r:
    for j in g:
      tmpa=min(i,j)
      tmpb=max(i,j)
      tmpc=tmpb-tmpa
      if (tmpc+tmpb) in b:
        cnt=cnt-1
  for i in r:
    for j in b:
      tmpa=min(i,j)
      tmpb=max(i,j)
      tmpc=tmpb-tmpa
      if (tmpc+tmpb) in g:
        cnt=cnt-1
  for i in g:
    for j in b:
      tmpa=min(i,j)
      tmpb=max(i,j)
      tmpc=tmpb-tmpa
      if (tmpc+tmpb) in r:
        cnt=cnt-1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

    rgb = {'R': [], 'G': [], 'B': []}
    for i, s in enumerate(S):
        rgb[s].append(i)

    def count_(s1, s2):
        ret = 0
        x = bisect(rgb[s1], i)
        for j in rgb[s1][x:]:
            y = bisect(rgb[s2], j)
            d = j - i
            if j + d < N and S[j+d] == s2:
                ret += len_[s2] - y - 1
            else:
                ret += len_[s2] - y
        return ret

    ans = 0
    len_ = {'R': len(rgb['R']), 'G': len(rgb['G']), 'B': len(rgb['B'])}
    for i, s in enumerate(S):
        if s == 'R':
            for s1, s2 in (('G', 'B'), ('B', 'G')):
                ans += count_(s1, s2)
        elif s == 'G':
            for s1, s2 in (('R', 'B'), ('B', 'R')):
                ans += count_(s1, s2)
        else:
            for s1, s2 in (('R', 'G'), ('G', 'R')):
                ans += count_(s1, s2)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

    rgb = {'R': [], 'G': [], 'B': []}
    apnd_r = rgb['R'].append
    apnd_g = rgb['G'].append
    apnd_b = rgb['B'].append
    for i, s in enumerate(S):
        if s == 'R':
            apnd_r(i)
        elif s == 'G':
            apnd_g(i)
        else:
            apnd_b(i)

    def count_(s1, s2):
        ret = 0
        x = bisect(rgb[s1], i)
        for j in rgb[s1][x:]:
            y = bisect(rgb[s2], j)
            d = j - i
            if j + d < N and S[j+d] == s2:
                ret += len_[s2] - y - 1
            else:
                ret += len_[s2] - y
        return ret

    ans = 0
    len_ = {'R': len(rgb['R']), 'G': len(rgb['G']), 'B': len(rgb['B'])}
    for i, s in enumerate(S):
        if s == 'R':
            for s1, s2 in (('G', 'B'), ('B', 'G')):
                ans += count_(s1, s2)
        elif s == 'G':
            for s1, s2 in (('R', 'B'), ('B', 'R')):
                ans += count_(s1, s2)
        else:
            for s1, s2 in (('R', 'G'), ('G', 'R')):
                ans += count_(s1, s2)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

    rgb = defaultdict(list)
    len_ = defaultdict(int)
    for i, s in enumerate(S):
        rgb[s].append(i)
        len_[s] += 1

    def count_(s1, s2):
        ret = 0
        x = bisect(rgb[s1], i)
        for j in rgb[s1][x:]:
            y = bisect(rgb[s2], j)
            d = j - i
            if j + d < N and S[j+d] == s2:
                ret += len_[s2] - y - 1
            else:
                ret += len_[s2] - y
        return ret

    ans = 0
    for i, s in enumerate(S):
        if s == 'R':
            for s1, s2 in (('G', 'B'), ('B', 'G')):
                ans += count_(s1, s2)
        elif s == 'G':
            for s1, s2 in (('R', 'B'), ('B', 'R')):
                ans += count_(s1, s2)
        else:
            for s1, s2 in (('R', 'G'), ('G', 'R')):
                ans += count_(s1, s2)
    print(ans)


main()

Submission status: Time Limit Exceeded

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

R = S.count('R')
G = S.count('G')
B = S.count('B')

ans = R * G * B

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

    rgb = defaultdict(list)
    for i, s in enumerate(S):
        rgb[s].append(i)

    def count_(s1, s2):
        ret = 0
        x = bisect(rgb[s1], i)
        for j in rgb[s1][x:]:
            y = bisect(rgb[s2], j)
            d = j - i
            if j + d < N and S[j+d] == s2:
                ret += len_[s2] - y - 1
            else:
                ret += len_[s2] - y
        return ret

    ans = 0
    len_ = {'R': len(rgb['R']), 'G': len(rgb['G']), 'B': len(rgb['B'])}
    for i, s in enumerate(S):
        if s == 'R':
            for s1, s2 in (('G', 'B'), ('B', 'G')):
                ans += count_(s1, s2)
        elif s == 'G':
            for s1, s2 in (('R', 'B'), ('B', 'R')):
                ans += count_(s1, s2)
        else:
            for s1, s2 in (('R', 'G'), ('G', 'R')):
                ans += count_(s1, s2)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i, x in enumerate(s):
  if x == 'R':
    r.append(i)
  elif x == 'G':
    g.append(i)
  else:
    b.append(i)
ans = 0
for i in r:
  for j in g:
    for k in b:
      if abs(i - j) != abs(j - k) and abs(j - k) != abs(k - i) and abs(k - i) != abs(i - j):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = input()
ans = 0
r=0
g=0
b=0
G = []
R = []
B = []
for i in range(0,N):
    if(X[i] == 'G'):
        G.append(i)
        g+=1
for k in range(0,N):
    if(X[k] == 'R'):
        R.append(k)
        r+=1
for j in range(0,N):
    if(X[j] == 'B'):
        B.append(j)
        b+=1
ans = r*b*g
for i in G:
    for k in R:
        for j in B:
            if(i<k):
                if(k<j):
                    if(j-k == k-i):
                        ans -=1
                else:
                    if(i<j):
                        if(k-j == j-i):
                            ans -=1
                    else:
                        if(k-i == i-j):
                            ans -= 1
            else:
                if(i<j):
                    if(j-i == i-k):
                        ans -=1
                else:
                    if(k<j):
                        if(i-j == j-k):
                            ans -= 1
                    else:
                        if(i-k == k-j):
                            ans -= 1
print(ans)

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):
    for k in range(j+1, N):
      # 条件1(全要素が異なる文字判定)
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        # 条件2判定
        if j - i != k - j:
          cnt += 1

print(cnt)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
 
for k in range(1, N+1):
  for j in range(1, k):
    for i in range(1, j):
      if j-i != k-j:
        if S[i-1] != S[j-1] != S[k-1] != S[i-1]:
          ans += 1
#end of for
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

'''
R_index = list()
G_index = list()
B_index = list()

for i in range(N):
    if S[i] == "R":
        R_index.append(i+1)
    elif S[i] == "G":
        G_index.append(i+1)
    if S[i] == "B":
        B_index.append(i+1)
'''

#組 (i, j, k) (1≤i<j<k≤N) の数を求めてください。
#Si≠Sj かつ Si≠Sk かつ Sj≠Sk である
#j−i≠k−jの条件下で組み合わせる
#j−i=k−j → 2j−i=kはNGということ。
ans = 0

#愚直解
#for R in R_index:
#    for G in G_index:
#        for B in B_index:
#            #この段階で文字の不一致はクリア
#            #並べ方はRGB,RBG,GRB,GBR,BGR,BRGの6パターン
#            for comb in [(R,G,B),(R,B,G),(G,R,B),(G,B,R),(B,G,R),(B,R,G)]:
#                i = comb[0]
#                j = comb[1]
#                k = comb[2]

#                if j - i !=  k - j and i < j and j < k:
#                    ans += 1

#高速解
#文字の並べ方 6通り
#i,jの選び方 2**10**6

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

'''
for R in R_index:
    #RGBのパターン
    OK_G = G_index[bisect.bisect_right(G_index,R):]
    for G in OK_G:
        OK_B = B_index[bisect.bisect_right(B_index,G):]
        ans += len(OK_B)
        if 2*G - R in set(OK_B):
            ans -= 1
    #RBGのパターン
    OK_B = B_index[bisect.bisect_right(B_index,R):]
    for B in OK_B:
        OK_G = G_index[bisect.bisect_right(G_index,B):]
        ans += len(OK_G)
        if 2*B - R in set(OK_G):
            ans -= 1

for G in G_index:
    #GRBのパターン
    OK_R = R_index[bisect.bisect_right(R_index,G):]
    for R in OK_R:
        OK_B = B_index[bisect.bisect_right(B_index,R):]
        ans += len(OK_B)
        if 2*R - G in set(OK_B):
            ans -= 1
    #GBRのパターン
    OK_B = B_index[bisect.bisect_right(B_index,G):]
    for B in OK_B:
        OK_R = R_index[bisect.bisect_right(R_index,B):]
        ans += len(OK_R)
        if 2*B - G in set(OK_R):
            ans -= 1    

for B in B_index:
    #BGRのパターン
    OK_G = G_index[bisect.bisect_right(G_index,B):]
    for G in OK_G:
        OK_R = R_index[bisect.bisect_right(R_index,G):]
        ans += len(OK_R)
        if 2*G - B in set(OK_R):
            ans -= 1
    #BRGのパターン
    OK_R = R_index[bisect.bisect_right(R_index,B):]
    for R in OK_R:
        OK_G = G_index[bisect.bisect_right(G_index,R):]
        ans += len(OK_G)
        if 2*R - B in set(OK_G):
            ans -= 1    
'''

print(ans)

Submission status: Time Limit Exceeded

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

# 方針：r, g, bのすべての組み合わせから、j-i=k-jを引く

r, g, b = 0, 0, 0
for c in S:
  if c == "R": r += 1
  if c == "G": g += 1
  if c == "B": b += 1

total = r*g*b

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

    k = 2*j - i
    if k >= N: continue
    if len(set((S[i], S[k], S[j]))) == 3:
      total -= 1

print(total)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(n-2):
	lis = []
	for j in range(i+1,n-1):
		lis = [s[i]]
		if not s[j] in lis :
			for k in range(j+1,n):
				lis = [s[i],s[j]]
				if not s[k] in lis :
					if j-i != k-j:
						count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

counter = Counter(S)

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

print(counter["R"]*counter["G"]*counter["B"] - 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 = (S == "B").sum()

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

print(r*g*b - cnt)

Submission status: Time Limit Exceeded

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

import numpy as np

S = np.array(S)

r = (S == "R").sum()
g = (S == "G").sum()
b = (S == "B").sum()

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

print(r*g*b - cnt)

Submission status: Time Limit Exceeded

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

import numpy as np

r = (np.array(S) == "R").sum()
g = (np.array(S) == "G").sum()
b = (np.array(S) == "B").sum()

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

print(r*g*b - cnt)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(target, left, right, nums):
    if(left > right):
        return -1
    mid = (right-left)//2 + left
    if(nums[mid] == target):
        return mid
    elif(nums[mid] < target):
        return binary_search(target, mid+1, right, nums)
    else:
        return binary_search(target, left, mid-1, nums)
def Main(N, S):
    ans = 0
    d = {'R':[], 'G':[], 'B':[]}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    lenb = len(d['B'])
    for i in d['R']:
        for j in d['G']:
            sub = 0
            lenb = len(d['B'])
            di = abs(i-j)
            mi = min(i,j)
            ma = max(i,j)
            a = mi-di
            b = ma+di
            x = binary_search(a, 0, lenb-1, d['B'])
            y = binary_search(b, 0, lenb-1, d['B'])
            if x>=0 and y>=0:
                sub = 2
            elif x>=0 or y>=0: 
                sub = 1
            ci = (i+j)//2
            cf = (i+j)/2
            if ci == cf:
                z = binary_search(ci, 0, lenb-1, d['B'])
                if z>=0:
                    sub += 1
            ans += lenb - sub
    return ans
        
    

def main():
    N = int(input())
    S = str(input())
    print(Main(N,S))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

rlst, glst, blst = [], [], []

for i, s in enumerate(S):
    if s=='R':rlst.append(i)
    elif s=='G':glst.append(i)
    else: blst.append(i)
rlst.sort()
glst.sort()
blst.sort()
      
ans = 0

for r in rlst:
    for g in glst:
        rg = abs(r - g)
        if min(r, g) - rg in blst: ans += 1
        if max(r, g) + rg in blst: ans += 1
        if rg % 2 == 0 and (r + g) // 2 in blst: ans += 1
print(len(rlst) * len(glst) * len(blst) - ans)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    
    for i in a1[0]:
        for j in a1[1]:
            if -abs(i-j)+min(i,j) in a1[2]:
                res-=1
            if abs(i-j)+max(i,j) in a1[2]:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a1[2]:
                res-=1
    print(res)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    
    for i in a1[0]:
        for j in a1[1]:
            if -abs(i-j)+min(i,j) in a1[2]:
                res-=1
            if abs(i-j)+max(i,j) in a1[2]:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a1[2]:
                res-=1
    print(res)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets

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

r, g, b = 0, 0, 0

for s in S:
	if s == 'R':
		r += 1
	elif s == 'G':
		g += 1
	else:
		b += 1

ans = r * g * b

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
d = input()
s = input()
 
r = s.count('R')
g = s.count('G')
b = s.count('B')
 
res = r*g*b
for i in range(len(s)-2):
    for j in range(i,len(s)-1):
        for k in range(j, len(s)):
            if (j - i) == (k - j):
                if s[i] != s[k] and s[i] != s[j] and s[j] != s[k]:
                    res -= 1
print(res)

Submission status: Time Limit Exceeded

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

r = s.count('R')
g = s.count('G')
b = s.count('B')

res = r*g*b
for i in range(len(s)-2):
    for j in range(i,len(s)-1):
        k = 2 * j - i
        if k < len(s) and s[i] != s[k] and s[i] != s[j] and s[j] != s[k]:
            res -= 1
print(res)

Submission status: Time Limit Exceeded

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



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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
input()
str = input()
lst_R = []
lst_G = []
lst_B = []
ans = 0

for i in range(len(str)):
    if str[i] == 'R':
        lst_R.append(i)

    elif str[i] == 'G':
        lst_G.append(i)

    else:
        lst_B.append(i)

def core():
    global ans
    for i in range(len(all[0])):

        for j in range(len(all[1])):
            if all[1][j] < all[0][i]:                   # condition is broken
                continue

            for k in range(len(all[2])):
                if all[2][k] < all[1][j]:
                    continue
                else:
                    if all[2][k] - all[1][j] != all[1][j] - all[0][i]:
                        ans += 1

all = [lst_R, lst_G, lst_B]

for i in range(3):      # len(all)

    # iterate process in order
    core()

    all[1], all[2] = all[2], all[1]  # swap list order

    # iterate process in order
    core()

    if i != 2:
        all[0], all[2] = all[2], all[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
S = input()

dic = defaultdict(list)
for i in range(N):
    dic[S[i]].append(int(i))

'''count = 0
print(dic["R"])
print(dic["G"])
print(dic["B"])

for i in dic["R"]:
    for j in dic["G"]:
        for k in dic["B"]:
            if i < j and j < k and (j-i != k-j):
                count += 1

print(count)
'''
count = 0
for i in range(N-1):
    for j in range(i+1,N):
        if S[i] == "R" and S[j] == "G":
            for k in dic["B"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "R" and S[j] == "B":
            for k in dic["G"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "G" and S[j] == "B":
            for k in dic["R"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "G" and S[j] == "R":
            for k in dic["B"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "B" and S[j] == "R":
            for k in dic["G"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "B" and S[j] == "G":
            for k in dic["R"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
S = input()

dic = defaultdict(list)
for i in range(N):
    dic[S[i]].append(int(i))

count = 0
for i in range(N-1):
    for j in range(i+1,N):
        if S[i] == "R" and S[j] == "G":
            for k in dic["B"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "R" and S[j] == "B":
            for k in dic["G"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "G" and S[j] == "B":
            for k in dic["R"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "G" and S[j] == "R":
            for k in dic["B"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "B" and S[j] == "R":
            for k in dic["G"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

        elif S[i] == "B" and S[j] == "G":
            for k in dic["R"]:
                if i < j and j < k and (j-i != k-j):
                   count += 1

print(count)