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

cnt=0
for i in range(N):
    if str[i] == 'R':
        for j in range(i+1,N):
            if str[j] == 'G':
                for k in range(j+1,N):
                    if str[k] == 'B':
                        if j-i != k-j:
                            cnt+=1

for i in range(N):
    if str[i] == 'R':
        for j in range(i+1,N):
            if str[j] == 'B':
                for k in range(j+1,N):
                    if str[k] == 'G':
                        if j-i != k-j:
                            cnt+=1

for i in range(N):
    if str[i] == 'G':
        for j in range(i+1,N):
            if str[j] == 'R':
                for k in range(j+1,N):
                    if str[k] == 'B':
                        if j-i != k-j:
                            cnt+=1

for i in range(N):
    if str[i] == 'G':
        for j in range(i+1,N):
            if str[j] == 'B':
                for k in range(j+1,N):
                    if str[k] == 'R':
                        if j-i != k-j:
                            cnt+=1

for i in range(N):
    if str[i] == 'B':
        for j in range(i+1,N):
            if str[j] == 'G':
                for k in range(j+1,N):
                    if str[k] == 'R':
                        if j-i != k-j:
                            cnt+=1

for i in range(N):
    if str[i] == 'B':
        for j in range(i+1,N):
            if str[j] == 'R':
                for k in range(j+1,N):
                    if str[k] == 'G':
                        if j-i != k-j:
                            cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

d = {'R': 1, 'G': 2, 'B': 3}

s = [d[i] for i in input()]
a = 0

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

Submission status: Time Limit Exceeded

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

ans = int(0)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    N = int(input())
    S = input().rstrip()

    all = {'R','G','B'}

    res = 0

    for i in range(N-2):
        first = S[i]
        for j in range(i+1,N-1):
            temp = S[j]
            if temp == first:
                continue
            else:
                second = temp
                now = {first,second}
            for k in range(j+1,N):
                if j-i == k-j:
                    continue
                temp = S[k]
                if temp not in now:
                    res += 1

    print(res)




main()

Submission status: Time Limit Exceeded

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

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

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)

for i in range(1, len(s)):
    if s[i] == "R":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)

for i in range(1, len(s)):
    if s[i] == "G":
        g.append(g[-1] + 1)
    else:
        g.append(g[-1])

b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

for i in range(1, len(s)):
    if s[i] == "B":
        b.append(b[-1] + 1)
    else:
        b.append(b[-1])

for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            tmp = [s[i]] + [s[j]]
            target_idx = j + (j - i)

            if "R" not in tmp:
                ans += r[-1] - r[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "R":
                    ans -= 1
            elif "G" not in tmp:
                ans += g[-1] - g[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += b[-1] - b[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "B":
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
N, = [int(s) for s in inputs]

input_line = input()
s = list(input_line)
sl = len(s)

cand = []
for i in range(sl):
    for j in range(i+2,sl):
        if s[i]!=s[j]:
            cand.append((i,j))

ans = 0
for c in cand:
    st = c[0]
    en = c[1]
    for i in range(st+1,en):
        if i-st!=en-i and s[st]!=s[i] and s[i]!=s[en]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input(''))
s=input('')
u=0

l=[]
for a in s:
    l.append(a)

for i in range(0,len(l)):
    for j in range(i+1,len(l)):
        for k in range(j+1,len(l)):
            if j-i!=k-j:
                if l[i]!=l[j] and l[j]!=l[k] and l[k]!=l[i]:
                    u=u+1

print(u)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in range(j+1,N):
            if j-i == k-j:
                continue
            if S[i]==S[k]:
                continue
            if S[j]==S[k]:
                continue
            A += 1
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c_list = [(0, 0, 0) for i in range(n)]
r = 0
g = 0
b = 0
n_list = [0]*n
p = -1
for i in range(n):
  if s[i] == "R":
    r += 1
    p = 0
  elif s[i] == "G":
    g += 1
    p = 1
  else:
    b += 1
    p = 2
  c_list[i] = (r, g, b)
  n_list[i] = p
count = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c1 = n_list[i]
    c2 = n_list[j]
    if c1 != c2:
      if j+j-i < n and 3 - n_list[j+j-i] - c1 - c2 == 0:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2] -1
      else:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2]
print(count)

Submission status: Time Limit Exceeded

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

result = 0

if n <= 2:
    print(0)
    
for i in range(n-2):
    if s[i] == "R":
        for j in range(i+1, n-1):
            if s[j] == "G":
                for k in range(j+1, n):
                    if s[k] == "B":
                        if j-i != k-j:
                            result += 1
            elif s[j] == "B":
                for k in range(j+1, n):
                    if s[k] == "G":
                        if j-i != k-j:
                            result += 1
    if s[i] == "G":
        for j in range(i+1, n-1):
            if s[j] == "R":
                for k in range(j+1, n):
                    if s[k] == "B":
                        if j-i != k-j:
                            result += 1
            elif s[j] == "B":
                for k in range(j+1, n):
                    if s[k] == "R":
                        if j-i != k-j:
                            result += 1
    if s[i] == "B":
        for j in range(i+1, n-1):
            if s[j] == "R":
                for k in range(j+1, n):
                    if s[k] == "G":
                        if j-i != k-j:
                            result += 1
            elif s[j] == "G":
                for k in range(j+1, n):
                    if s[k] == "R":
                        if j-i != k-j:
                            result += 1
print(result)

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 collections import deque
from bisect import bisect_left
def resolve():
    N = ir()
    nS = sr()
    q = deque([])
    S = []
    R = []
    G = []
    B = []
    for i in range(N):
        if nS[i] == 'R':
            S.append(2)
            R.append(i)
        elif nS[i] == 'G':
            S.append(3)
            G.append(i)
        else:
            S.append(5)
            B.append(i)
        q.append([S[i], i])
    ans = 0
    for n in R:
        x = bisect_left(G, n)
        for i in G[x:]:
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in R:
        x = bisect_left(B, n)
        for i in B[x:]:
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n 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[x:]:
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n 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:
# -*- coding: utf-8 -*-
n = int(input())
s = input()
#a = [int(i) for i in input().split()]

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

Submission status: Time Limit Exceeded

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

ans = 0
list1 = ["", "", ""]
for i in range(n):
    for j in range(n):
        if i < j:
            for k in range(n):
                if j < k:
                    if s[i] != s[j]:
                        if s[j] != s[k]:
                            if s[i] != s[k]:
                                if j - i != k - j:
                                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
n=int(input())
a=input()


cnt=0
r=[]
g=[]
b=[]
def main():
	cnt=0
	for i in range(n):
		r_a=r.append
		b_a=b.append
		g_a=g.append
		if a[i]=="R":
			r_a(i)
		if a[i]=="B":
			b_a(i)
		if a[i]=="G":
			g_a(i)


	for i1 in r:
		for i2 in b:
			for i3 in g:
				if abs(i1-i2)==abs(i2-i3) or abs(i1-i3)==abs(i2-i3) or abs(i1-i3)==abs(i2-i1):
					cnt+=0
				else:
					cnt+=1
			
	print(cnt)
	
if __name__=="__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(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] and S[j] != S[k] and 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()
s = "0" + s
ans = 0

for i in range(1, n - 1):
    for j in range(i + 1 , n):
        if s[i] == s[j]:
            continue
        elif (s[i] == "R" and s[j] == "G") or (s[i] == "G" and s[j] == "R"):
            ans += s.count("B", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "B":
                    ans -=1
        elif (s[i] == "R" and s[j] == "B") or (s[i] == "B" and s[j] == "R"):
            ans += s.count("G", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "G":
                    ans -=1
        elif (s[i] == "B" and s[j] == "G") or (s[i] == "G" and s[j] == "B"):
            ans += s.count("R", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "R":
                    ans -=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
from collections import deque
R = deque([])
G = deque([])
B  = deque([])
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    elif s=='B':
        B.append(i)

def res(X,Y,S,i,a,b):
  x = X.copy()
  ret = 0
  for y in Y:
    if len(x)!=0:
      while x[0]<y:
        x.popleft()
        if len(x)==0:
          break
    ret+=len(x)
    tmp = 2*y-i
    if tmp>=S_l:
      continue
    if S[tmp]==a:
      ret-=1
  
  y = Y.copy()
  for x in X:
    if len(y)!=0:
      while y[0]<x:
        y.popleft()
        if len(y)==0:
          break
    ret+=len(y)
    tmp = 2*x-i
    if tmp>=S_l:
      continue
    if S[tmp]==b:
      ret-=1

      
  return ret
    
for i in range(len(S)):
    if S[i]=='R':
      r = R.popleft()
      ans += res(G,B,S,i,'G','B')
    elif S[i]=='G':
      g = G.popleft()
      ans += res(R,B,S,i,'R','B')
    else:
      b = B.popleft()
      ans += res(R,G,S,i,'R','G')
      
print(ans)

Submission status: Time Limit Exceeded

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

ans=0
for i in range(n):
  si=S[i]
  for j in range(i+1,n):
    if S[j]!=si:
      sj=S[j]
      for k in range(j+1,n):
        if S[k]!=si and S[k]!=sj:
          sk=S[k]
          if j-i!=k-j:
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s_list = list(map(str,input()))
R_list=[];G_list=[];B_list=[];ans=0
 
for i in range(n):
    if s_list[i]=="R":
        R_list.append(i)
    elif s_list[i]=="G":
        G_list.append(i)
    else:
        B_list.append(i)
 
for a in R_list:
    for b in G_list:
        for c in B_list:
            if a+b==2*c or a+c==2*b or b+c==2*a:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from pprint import pprint
import sys
from itertools import combinations

N = int(input())
S = sys.stdin.readline().rstrip()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
# N = 4
# S = 'RRGB'

C = combinations(range(1, N + 1), 3)
C1 = filter(lambda x: x[1]-x[0] != x[2]-x[1], C)
C2 = filter(lambda x: len(set([S[i-1] for i in x])) == 3, C1)
print(len(list(C2)))

Submission status: Time Limit Exceeded

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

import numpy as np

snp=np.array(S)
R=np.where(snp=='R')[0]
G=np.where(snp=='G')[0]
B=np.where(snp=='B')[0]

ans=0
for r in R:
    for g in G:
        for b in B:
            if len(set(np.diff(np.sort(np.array([r,g,b])))))==2:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R,G,B = [],[],[]
r1,g1,b1 = 0,0,0
for i in range(N):
    if S[i] == 'R':
        R.append(i)
        r1 += 1
    elif S[i] == 'G':
        G.append(i)
        g1 += 1
    elif S[i] == 'B':
        B.append(i)
        b1 += 1
ans = r1*g1*b1
for r in R:
    for g in G:
        if abs(r-g) + max([r,g]) in B:
            ans -= 1
        if min([r,g]) - abs(r-g) in B:
            ans -= 1
        if abs(r-g) % 2 == 0 and int(min([r,g]) + abs(r-g) * 0.5) in B:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

def Word(i, a, b):
    if S[i] != S[i + a] and S[i] != S[i + a + b] and S[i + a] != S[i + a + b]:
        return True
    else:
        return False

ans = 0
for i in range(0, N - 1):
    for a in range(1, N - i - 1): #a=j-i
        for b in range(1, a): #b=k-j
            if a + b + i >= N:
                break
            if Word(i, a, b):
                ans += 1
            if Word(i, b, a):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = list(input())
S_len = len(S)
ans = 0
i = 0
for _ in range(S_len):
  for j in range(len(S)-i-1):
    if S[i] == S[i+j+1]:
      continue
    for k in range(len(S)-i-j-2):
      if j != k and S[i] != S[i+j+1] and S[i] != S[i+k+j+2] and S[i+k+j+2] != S[i+j+1]:
          ans+=1
  S  = S[1:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    whole = (n*(n-1)*(n-2))//6
    num = n-2
    cnt = 0
    for i in range(n-3):
        for j in range(i+1,n-1):
            if j == i+1:
                if s[i] == s[j]:
                    cnt += (n-(j+2))
                    continue
                for k in range(j+2,n):
                    if (j-i) == (k-j) or s[j] == s[k] or s[i] == s[k]:
                        cnt += 1
            else:
                if s[i] == s[j]:
                    cnt += (n-(j+1))
                    continue
                for k in range(j+1,n):
                    if (j-i) == (k-j) or s[j] == s[k] or s[i] == s[k]:
                        cnt += 1
    print(whole-num-cnt)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

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

Submission status: Time Limit Exceeded

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

RGB = list(S)
count = 0
for a in range(1, N -1):
    for b  in range(a+1, N):
        if not RGB[a-1] == RGB[b-1]: 
            for c in range(b+1, N+1):
                if  RGB[a-1] != RGB[b-1] and RGB[b-1] != RGB[c-1] and RGB[c-1] != RGB[a-1]:
                    if not b-a == c-b:
                        count +=1
        else:
            continue
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
Rlist = []
Glist = []
Blist = []
for x in range(len(s)):
    if s[x] == "R": Rlist.append(x)
    elif s[x] == "G": Glist.append(x)
    elif s[x] == "B": Blist.append(x)

def Bmin(r, g):
    result = -1
    if g < r: result = Bmin(g, r)
    else: result = r - (g - r)
    return result
    
def Bmid(r, g):
    result = -1
    if g < r: result = Bmid(g, r)
    elif (g - r) % 2 != 0: result = -1
    else: result = r + (g - r) // 2
    return result

def Bmax(r, g):
    result = -1
    if g < r: result = Bmax(g, r)
    else: result = g + (g - r)
    return result

ans = 0
for r in Rlist:
    for g in Glist:
        ans += len(Blist)
        if Bmin(r, g) in Blist: ans -= 1
        if Bmid(r, g) in Blist: ans -= 1
        if Bmax(r, g) in Blist: ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = list(input())
cr = [0] * n
cg = [0] * n
cb = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'R':
            cr[i] += 1
        elif s[i] == 'G':
            cg[i] += 1
        else:
            cb[i] += 1
    else:
        cr[i] = cr[i-1]
        cg[i] = cg[i-1]
        cb[i] = cb[i-1]
        if s[i] == 'R':
            cr[i] += 1
        elif s[i] == 'G':
            cg[i] += 1
        else:
            cb[i] += 1
# cr = np.cumsum(r)
# cg = np.cumsum(g)
# cb = np.cumsum(b)
ans = 0
for i in range(n-2):
    a = s[i]
    for j in range(i+1, n-1):
        b = s[j]
        if a == b:
            continue
        diff = j - i
        c = {'R', 'G', 'B'} - {a, b}
        c = list(c)[0]
        if c == 'R':
            ans += cr[-1] - cr[j]
        elif c == 'G':
            ans += cg[-1] - cg[j]
        else:
            ans += cb[-1] - cb[j]
        if j + diff < n:
            if s[j + diff] == c:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

# 1を満たす数
# 1,~2を満たす数を求める

# def checkone(a,b,c):
# 	if (a!=b and b!=c and c!=a):
# 		return True
# 	else:
# 		return False

def check(i,j,N): # i,j,2j-iは違ったらTrue
	# i < jは保証
				# (0 > (2*i-j) or (2*i-j) >= N or checkone(S[i], S[j], S[2*i-j])) and
              	# (0 > (2*j-i) or (2*j-i) >= N or checkone(S[i], S[j], S[2*j-i])) #and
			  	# ( ((i+j)%2!=0) or checkone(S[i], S[j], S[abs(int((i+j)/2))]))
	if (0 > (2*j-i) or (2*j-i) >= N):
		# print("out of range")
		return False
	else:
		return (S[j] != S[2*j-i] and S[2*j-i] != S[i])
		# return checkone(S[i], S[j], S[2*j-i])

n_equal = 0
for i in range(N):
	for j in range(i,N):
		if( S[i]==S[j]):
			continue
		else:
			if(check(i,j,N)==True):
				n_equal+=1
				# print(i, j, 2*j-i)
				# print(S[i],S[j],S[2*j-i])
				# print("toukankaku")

# print("n_equal=",n_equal)

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

n_all = n_R*n_G*n_B
# print("n_all=",n_all)

print(n_all-n_equal)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            x = min(r[i], g[j], b[k])
            y = max(r[i], g[j], b[k])
            if r[i] != x and r[i] != y:
                if r[i]-x != y-r[i]:
                    cnt += 1
            elif g[j] != x and g[j] != y:
                if g[j]-x != y-g[j]:
                    cnt += 1
            else:
                if b[k]-x != y-b[k]:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
l = [len(L[i]) for i in range(3)]
ans = l[0] * l[1] * l[2]
bb = []
if l[0] <= l[1] <= l[2]:
    bb = [0, 1, 2]
elif l[0] <= l[2] <= l[1]:
    bb = [0, 2, 1]
elif l[1] <= l[0] <= l[2]:
    bb = [1, 0, 2]
elif l[1] <= l[2] <= l[0]:
    bb = [1, 2, 0]
elif l[2] <= l[0] <= l[1]:
    bb = [2, 0, 1]
elif l[2] <= l[1] <= l[0]:
    bb = [2, 1, 0]
for i in L[bb[0]]:
  for j in L[bb[1]]:
    p = 2 * i - j
    q = 2 * j - i
    r = (i + j) / 2
    for k in L[bb[2]]:
      if k > max(p, q, r):
        break
      if k == p:
        ans = ans - 1
      if k == q:
        ans = ans - 1
      if k == r:
        ans = ans - 1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
out = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        tmpstr = s[j + 1 :  n]
        remain = "RBG".replace(s[i],"").replace(s[j],"")
        if len(remain) == 1:
            num = tmpstr.count(remain) 
            if (j + (j - i)) < len(s):
                if  s[j + (j - i)]  == remain:
                    num = num - 1
            out += num
print(out)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for i in rs:
        for j in reversed(gs):
            if not (i < j):
                break
            for k in reversed(bs):
                if not (j < k):
                    break
                if (j - i) != k - j:
                    res += 1
    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(rs, bs, gs) + f(gs, rs, bs) + f(gs, bs, rs) + f(bs, rs, gs) + f(bs, gs, rs))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

count_R = [0]
count_G = [0]
count_B = [0]
d = {'R': count_R, 'G': count_G, 'B': count_B}
for c in s:
    count_R.append(count_R[-1]+1) if c == 'R' else count_R.append(count_R[-1])
    count_G.append(count_G[-1]+1) if c == 'G' else count_G.append(count_G[-1])
    count_B.append(count_B[-1]+1) if c == 'B' else count_B.append(count_B[-1])

res = 0
for i in range(n-2):
    for k in range(i+1, n-1):
        if s[i] == s[k]:
            continue
        st = {'R', 'G', 'B'}
        st.discard(s[i])
        st.discard(s[k])
        c = st.pop()
        cnt = d[c]
        res += cnt[-1] - cnt[k]
        if k+k-i < n and s[k+k-i] == c:
            res -= 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():  
    N=int(input())
    S=input()
    r,g,b=[],[],[]
    for i,s in enumerate(S):
        if s=="R":r.append(i)
        elif s=="B":b.append(i)
        else: g.append(i)
    #print(r,g,b)
    #RGBのリストが作れたので、このうち１個ずつとっていけば
    #<1>の条件を満たすことができる（ijkの順序が決まっていることから、
    #最後にこれらが<2>を満たすことを確認する？または、満たさないものを除く
    #<2> i<=j<=k　に対して　j-i = k-j でない
    #"この式を整理すると"k-j*2+i=0ではない:i+j+k=j*3ではない
    #逆にこの式を満たす（平均の関係になっているなら、jは必ず真ん中になる
    
    cnt=0
    for R in r:
        for G in g:
            t=0
            for B in b:
                temp= R+G+B
                
                if temp==R*3 or temp==G*3 or temp == B*3:
                    t+=1
                if t==3:
                    cnt+=t
                    break
                
            if t<3:
                cnt+=t   
                    #print(R,G,B)
    print(len(r)*len(g)*len(b)-cnt)
        
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じ,るの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)


if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0,n-3):
  ii = s[i]
  for j in range(i+1, n-1):
    jj = s[j]
    for k in range(j+1,n):
      if ii == jj:
        break
      kk=s[k]
      if j-i == k-j:
        continue
      if jj != kk and kk!=ii:
        count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
S = input()
S_num = S.replace("R","1")
S_num = S_num.replace("G","2")
S_num = S_num.replace("B","4")
ans=0
for i in range(N):
  for j in range(N):
    if(i>=j):
      continue
    for k in range(N):
      if(j>=k):
        continue
      if((j-i != k-j) and (int(S_num[i]) + int(S_num[j]) + int(S_num[k]))==7):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
for i in range(0,n):
 for j in range(i+1,n):
  for k in range(j+1,n):
   if s[i]!=s[j] and s[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 = input()
s = [S[i] for i in range(len(S))]
R_list = []
G_list = []
B_list = []
for i in range(N):
    if s[i] == "R":
        R_list.append(i+1)
    elif s[i] == "G":
        G_list.append(i+1)
    elif s[i] == "B":
        B_list.append(i+1)
ans = 0
for i in range(len(R_list)):
    for j in range(len(G_list)):
        for k in range(len(B_list)):
            list = [R_list[i],G_list[j],B_list[k]]
            list.sort()
            if list[1] - list[0] != list[2] - list[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
data = list(stdin.readline().rstrip().split()[0])

count = 0

c_list = [["R", "G", "B"], ["R", "B", "G"], ["G", "R", "B"], ["G", "B", "R"], ["B", "R", "G"], ["B", "G", "R"]]

for temp_list in c_list:
    for i in range(n):
        if data[i] != temp_list[0]:
            continue
        else:
            for j in range(i+1, n):
                if data[j] != temp_list[1]:
                    continue
                else:
                    for k in range(j+1, n):
                        if data[k] == temp_list[2]:
                            if (k - j) != (j - i):
                                count += 1
                                
print(count)