Problem:


Score : 400 points

Problem Statement

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

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

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

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

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

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

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

total = 0
for i in range(0, n):
    if i == n-2:
        print(total)
        exit()
    for j in range(i+1, n):
        if s[j] == s[i]:
            if j == n-1:
                break
            continue
        for k in range(j+1, n):
            if s[k] == s[i] or s[k] == s[j]:
                continue
            elif j - i != k - j:
                total += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
bc = len(s)*[0]
gc = len(s)*[0]
rc = len(s)*[0]
for i in range(1, len(s)+1):
    mi = len(s) - i
    if s[len(s) - i] == 'B':
        if i == 1:
            bc[mi] = 1
        else:
            bc[mi] = bc[mi+1] + 1
            rc[mi] = rc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'R':
        if i == 1:
            rc[mi] = 1
        else:
            rc[mi] = rc[mi+1] + 1
            bc[mi] = bc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'G':
        if i ==1:
            gc[mi] = 1
        else:
            gc[mi] = gc[mi+1] + 1
            rc[mi] = rc[mi+1]
            bc[mi] = bc[mi+1]
si = 0
for i in range(0, len(s) - 1):
    
    for j in range(i+1, len(s)-1):
        if s[j] != s[i]:
            d = j-i 
            if j+d < n:
                nc = s[j+d]
                if nc in [s[i], s[j]]:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]
                    elif l[0] == 'G':
                        si += gc[j+1] 
                    else:
                        si += bc[j+1]
                else:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]-1
                    elif l[0] == 'G':
                        si += gc[j+1] -1
                    else:
                        si += bc[j+1] - 1
            else:
                l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                if l[0] == 'R':
                    si+= rc[j+1]
                elif l[0] == 'G':
                    si += gc[j+1] 
                else:
                    si += bc[j+1]
print(si)

Submission status: Time Limit Exceeded

Solution code:
# 整数を一つ入力
N = int(input())

# 文字列を一つ入力
S = input()

res = 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] or S[j] == S[k]:
                continue
            res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ss=list(range(n))
cnt=0
from itertools import combinations
for i,j,k in combinations(ss,3):
    if j-i==k-j :
        continue
    if i<j and j<k:
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
count_r = s.count("R")
count_g = s.count("G")
count_b = s.count("B")
sum = count_r * count_g * count_b
minus = 0

for i in range(n):
    for j in range(1,n-i):
        if i+2*j <= n-1:
            tem = s[i]+s[i+j]+s[i+2*j]
            num_r = tem.count("R")
            num_g = tem.count("G")
            num_b = tem.count("B")
            if num_r == 1 and num_g == 1 and num_b == 1:
                minus += 1
            

print(sum - minus)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        elif set([S[i], S[j]]) == {"R", "G"}:
            ans += S[j+1:].count("B")
            if 2*j - i < N and S[2*j - i] == "B":
                ans -= 1
        elif set([S[i], S[j]]) == {"B", "G"}:
            ans += S[j+1:].count("R")
            if 2*j - i < N and S[2*j - i] == "R":
                ans -= 1
        elif set([S[i], S[j]]) == {"R", "B"}:
            ans += S[j+1:].count("G")
            if 2*j - i < N and S[2*j - i] == "G":
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Sl = list(S)
Nl = [int(x) for x in range(1,N+1)]

res =0
for (i,Ni) in zip(Sl,Nl):
    x = i
    Nx = Ni
    for (j,Nj) in zip(Sl,Nl):
        if Nj <= Ni:
            pass
        else:
            y = j
            Ny =Nj 
            for (k,Nk) in zip(Sl,Nl):
                if Nk <= Nj:
                    pass
                else:
                    z = k
                    Nz = Nk
                    Ma = Nz-Ny
                    Mb = Ny-Nx
                    judge = [x,y,z]
                    alllen = len(judge)
                    unilen = len(set(judge))
                    if alllen == unilen:
                        if Ma != Mb:
                            res =res+1
print(res)

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)

t=0
for i in range(len(r)):
    for j in range(len(g)):
        p=abs(r[i]-g[j])
        for k in range(len(b)):

            if p!=abs(b[k]-g[j]) and abs(r[i]-b[k])!=abs(b[k]-g[j]) and p!=abs(b[k]-r[i]):
                t=t+1

print(t)

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'
count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def main():
	n = int(input())
	s = tuple(input())

	ans = 0

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

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

ans = 0
for j in range(N):
    original = "RGB"
    if S[j] in original:
        for i in range(j):
            obj = original.replace(S[j], "")
            if S[i] in obj:
                for k in range(j+1, N):
                    obj_2 = obj.replace(S[i], "")
                    if S[k] == obj_2:
                      	if (j-i) != (k-j):
                        	ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc162/tasks/abc162_d

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

ans = 0
for i in range(N):
    for j in range(i, N):
        for k in range(j, N):
            if j - i == k - j:
                continue

            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:
n=int(input())
s=input()
ans=0
for i in range(n-2):
  fir=s[i]
  for j in range(i+1,n-1):
    sec=s[j]
    if sec==fir:
      continue
    off=j-i
    for k in range(j+1,n):
      if k-j==off:
        continue
      thir=s[k]
      if (fir!=sec)and(sec!=thir)and(thir!=fir):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# D - RGB Triplets
# 標準入力の取得
N = int(input())
S = input()

# 求解処理
def get_S_k(S_i, S_j):
    S_k = "RGB"
    return S_k.replace(S_i, "").replace(S_j, "")

result = 0
size = len(S)
for i in range(size):
    S_i = S[i]
    for j in range(i+1, size):
        S_j = S[j]
        if S_i == S_j:
            continue
        S_k = get_S_k(S_i, S_j)
        result += S[j+1:].count(S_k)
        if (2*j - i < size) and (S[2*j-i] == S_k):
            result -= 1

# 結果出力
print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 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
        for k in range(j+1,N):
            c = S[k]
            if b==c:
                continue
            if (a!=b)and(b!=c)and(a!=c)and((j-i)!=(k-j)):
                
                cnt += 1


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
List = list(input())
count = 0
for k in range(n - 2):
    for l in range(k + 1, n - 1):
        if List[k] != List[l]:
            for m in range(l + 1, min(2 * l - k, n)):
                if List[k] != List[m] and List[l] != List[m]:
                    count += 1
            for m in range(min(2 * l - k + 1, n), n):
                if List[k] != List[m] and List[l] != List[m]:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

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

count = 0

for i in range (0, N-2):
  colour1 = S[i]
  for j in range (i+1, N-1):
    colour2 = S[j]
    if colour1 == colour2:
      continue
    else:
      for k in range(j+1, N):
        colour3 = S[k]
        if j-i == k-j:
          continue
        elif colour1 != colour3 and colour2 != colour3:
          count+=1
        else:
          continue

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
if N < 4:
    print(0)
    exit()
ans = 0
for i in range(N):
    si = S[i]
    for j in range(i+1, N):
        sj = S[j]
        if si == sj:
            continue
        t = j - i
        for k in range(j+1, N):
            if k == j + t:
                continue
            sk = S[k]
            if sj != sk and si != sk:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input("")
S = input("")
N = int(N)
X = 0

for i in range(N-3):
    j = i+1
    while j < N-1:
        if S[j] != S[i]:
            k = j+1
            while k < N:
                #if S[k] != S[j] and S[k] != S[i] and j-i != k-j:
                if S[k] != S[j] and S[k] != S[i]:
                    if j-i != k-j:
                        X += 1
                k += 1
        j += 1
print(X)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sentense = input()
ans = 0
for i, s_i in enumerate(sentense):
    for j, s_j in enumerate(sentense):
        if j <= i:
            continue
        else:
            if s_j == s_i:
                continue
            else:
                for k, s_k in enumerate(sentense):
                    if k <= j:
                        continue
                    else:
                        if s_k ==s_i or s_k==s_j:
                            continue
                        else:
                            if j-i != k-j:
                                ans += 1
print(ans)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(0,N):
    for j in range(i,N):
        if S[i] != S[j]:
            str = 'RGB'.replace(S[i],'').replace(S[j],'')
            ans += S.count(str, j+1, N+1)
            if j+j-i < N:
                if S[j+j-i] == str:
                    ans -=1
print(ans)

Submission status: Time Limit Exceeded

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

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)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        l = set()
        if (r[i] + g[j]) % 2 == 0 and (r[i] + g[j]) // 2 in b:
            l.add((r[i] + g[j]) / 2)
        if 2 * r[i] - g[j] in b:
            l.add(2 * r[i] - g[j])
        if 2 * g[j] - r[i] in b:
            l.add(2 * g[j] - r[i])
        cnt += len(l)
print(ans-cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8


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

    n_right_RGB = [None] * N

    n_r = 0
    n_g = 0
    n_b = 0
    for i in range(N):
        if S[i] == "R":
            n_r += 1
        elif S[i] == "G":
            n_g += 1
        else:
            n_b += 1
    
    for i in range(N):
        if S[i] == "R":
            n_r -= 1
        elif S[i] == "G":
            n_g -= 1
        else:
            n_b -= 1

        n_right_RGB[i] = [n_r, n_g, n_b]
    
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == "R" and S[j] == "G":
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == "B":
                    ans -= 1
            elif S[i] == "R" and S[j] == "B":
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == "G":
                    ans -= 1
            elif S[i] == "G" and S[j] == "B":
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == "R":
                    ans -= 1
            elif S[i] == "G" and S[j] == "R":
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == "B":
                    ans -= 1
            elif S[i] == "B" and S[j] == "R":
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == "G":
                    ans -= 1
            elif S[i] == "B" and S[j] == "G":
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == "R":
                    ans -= 1
    
    return ans


print(main())

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
txt=input()
count=0
for i in range(len(txt)-2):
  a=txt[i]
  for j in range(i+1,len(txt)-1):
    b=txt[j]
    if a==b:
      continue
    for k in range(j+1,len(txt)):
      c=txt[k]
      if c==a:
        continue
      if c==b:
        continue
      if k-j==j-i:
        continue
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
if n <= 3:
    sum = 0
else:
    sum = 0
    for i in range(n-3):
        if s[i] == "R":
            j = i + 1
            while j <= n-2:
                if s[j] == "G":
                    for k in range(j,n):
                        if s[k] == "B":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    for k in range(j,n):
                        if s[k] == "G":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                j += 1
        if s[i] == "G":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    for k in range(j,n):
                        if s[k] == "B":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    for k in range(j,n):
                        if s[k] == "R":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
        if s[i] == "B":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    for k in range(j,n):
                        if s[k] == "G":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                if s[j] == "G":
                    for k in range(j,n):
                        if s[k] == "R":
                            sum += 1
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
print(sum)

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n-2):
    if s[i] == "R":
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "B":
                        if j - i != k - j:
                            ans += 1


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

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

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

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

print(z)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
for i, s_i in enumerate(S[:-2]):
    for j, s_j in enumerate(S[i + 1: -1]):
        j += i + 1
        for k, s_k in enumerate(S[j + 1:]):
            k += j + 1
            if j - i != k - j:
                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()

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

Submission status: Time Limit Exceeded

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

ri = [n+1 for n, v in enumerate(s) if v == 'R']
gi = [n+1 for n, v in enumerate(s) if v == 'G']
bi = [n+1 for n, v in enumerate(s) if v == 'B']

ans = 0

for r in ri:
    for g in gi:
        for b in bi:
            if abs(r-g) == abs(b-g):
                continue
            if r < g and g < b:
                if g - r != b - g:
                    ans += 1
            elif g < r and r < b:
                if r - g != b - r:
                    ans += 1
            elif r < b and b < g:
                if b - r != g - b:
                    ans += 1
            elif g < b and b < r:
                if b - g != r - b:
                    ans += 1
            elif b < r and r < g:
                if r - b != g - r:
                    ans += 1
            elif b < g and g < r:
                if g - b != r - g:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_list = list(s)#文字列をリストに
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
R=[]
G=[]
B=[]
for i in range(N):
  if S[i]=="R":
    R.append(i)
  if S[i]=="G":
    G.append(i)
  if S[i]=="B":
    B.append(i)
for r in R:
  for g in G:
    for b in B:
      if r<g and g<b:
        if b-g!=g-r:
          count+=1
      elif r<b and b<g:
        if g-b!=b-r:
          count+=1
      elif g<r and r<b:
        if b-r!=r-g:
          count+=1
      elif g<b and b<r:
        if r-b!=b-g:
          count+=1
      elif b<r and r<g:
        if g-r!=r-b:
          count+=1
      elif b<g and g<r:
        if r-g!=g-b:
          count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

retval=0

Rs=[0]
Gs=[0]
Bs=[0]
for s in S[::-1]:
    if s=='R':
        Rs.append(Rs[-1]+1)
        Gs.append(Gs[-1])
        Bs.append(Bs[-1])
    elif s=='G':
        Rs.append(Rs[-1])
        Gs.append(Gs[-1]+1)
        Bs.append(Bs[-1])
    else:
        Rs.append(Rs[-1])
        Gs.append(Gs[-1])
        Bs.append(Bs[-1]+1)



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

        rest=list(set(['R','G','B'])-set([S[i],S[j]]))[0]

        if rest=='R':
            retval+=Rs[N-j]
        elif rest=='G':
            retval+=Gs[N-j]
        else:
            retval+=Bs[N-j]
        if 2*j-i<N and S[j+(j-i)]==rest:
            retval-=1

print(retval)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import itertools
import math
N = int(input())
S = list(input())

R = [0]*N
G = [0]*N
B = [0]*N

for i in range(N):
    if S[i] == 'R':
        R[i] += 1
    elif S[i] == 'G':
        G[i] += 1
    else:
        B[i] += 1

for i in range(N-1):
    R[i+1] += R[i]
    G[i+1] += G[i]
    B[i+1] += B[i]
ans = 0
#print(R,G,B)
for i in range(N-2):
    for j in range(i+1,N-1):
        if len(set([S[i],S[j]])) == 2:
            s = list(set(['R','G','B']) ^ set([S[i],S[j]]))[0]
            if s == 'R':
                ans += R[N-1] - R[j]
                if 2*j-i<=N-1 and S[2*j-i] == 'R':
                    ans -= 1
            if s == 'G':
                ans += G[N-1] - G[j]
                if 2*j-i <= N-1 and S[j+j-i] == 'G':
                    ans -= 1
            if s == 'B':
                ans += B[N-1] - B[j]
                if 2*j-i <= N-1 and S[j+j-i] == 'B':
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
if N < 4:
    print(0)
    exit()
ans = 0
for i in range(N):
    si = S[i]
    for j in range(i+1, N):
        sj = S[j]
        if si == sj:
            continue
        t = j - i
        for k in range(j+1, N):
            if k == j + t:
                continue
            sk = S[k]
            if sj != sk and si != sk:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_list = []
g_list = []
b_list = []
cnt = 0
for i, j in enumerate(s):
    if j == 'R':
        r_list.append(i + 1)
    if j == 'G':
        g_list.append(i + 1)
    if j == 'B':
        b_list.append(i + 1)
for r in r_list:
    for g in g_list:
        for b in b_list:
            if r * 2 != g + b and g * 2 != r + b and b * 2 != r + g:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def RGBto123(c):
    if c == 'R':
        return 1
    elif c == 'G':
        return 2
    return 4


N = int(input())
S = list(map(RGBto123, input()))

rel = [1 if S[i]+S[j]+S[k]==7 and i+k!=2*j  else 0 for i in range(0, N-2) for j in range(i+1, N-1) for k in range(j+1, N) ]



print(sum(rel))

Submission status: Time Limit Exceeded

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

def c(s1, s2, s3, i, s):
  cnt = 0
  L = S[:i]
  R = S[i+1:]
  # print(s1, s2, s3, L, R)
  if s == s1:
    lc = 0
    for l in L:
      if l == s2:
        lc += 1
    rc = 0
    for r in R:
      if r == s3:
        rc += 1
    cnt += lc * rc 
  for i, j in zip(R, L[::-1]):
    if s1 == s and i == s3 and j == s2:
      cnt -= 1
  # print(cnt)
  return cnt

ans = 0
for i, s in enumerate(S):
  ans += c("R", "B", "G", i, s) 
  ans += c("R", "G", "B", i, s) 
  ans += c("B", "R", "G", i, s) 
  ans += c("B", "G", "R", i, s) 
  ans += c("G", "R", "B", i, s) 
  ans += c("G", "B", "R", i, s) 

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = 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)

    res = 0
    for r in R:
        for g in G:
            for b in B:
                x = max(r, g, b)
                y = min(r, g, b)
                z = r + g + b - x - y
                if x + y != z * 2:
                    res += 1
    print(res)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
s = np.array(list(input()))

a = np.where(s == 'R')[0]
b = np.where(s == 'G')[0]
c = np.where(s == 'B')[0]

ans = 0
for i in a:
    for j in b:
        for k in c:
            l = sorted([i, j, k])
            if l[1] - l[0] != l[2] - l[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N

for i, a in enumerate(S):
    if a=='R':
        h[i] = 0
    elif a=='G':
        h[i] = 1
    else:
        h[i] = 9

cnt = -1
ans = 0
for i in range(N):
    cnt = h[i]
    for j in range(i+1, N):
        num = cnt + h[j]
        if num == 1 or num == 9 or num==10:
            for k in range(j+1, N):
                if num + h[k] == 10 and k!=2*j-i:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
lista = [[[0 for i in range(N)] for j in range(N)] for k in range(3)]
for i in range(N-2):
    a = S[i]
    for j in range(i+1,N-1):
        if a != S[j]:
            if a != 'R' and S[j] != 'R':
                lista[0][j-i][j] += 1
            elif a != 'G' and S[j] != 'G':
                lista[1][j-i][j] += 1
            elif a != 'B' and S[j] != 'B':
                lista[2][j-i][j] += 1
for k in range(2,N):
    for j in range(k):
        if S[k] == 'R':
            ans += sum(lista[0][j][:k]) - lista[0][j][k-j]
        if S[k] == 'G':
            ans += sum(lista[1][j][:k]) - lista[1][j][k-j]
        if S[k] == 'B':
            ans += sum(lista[2][j][:k]) - lista[2][j][k-j]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ss=list(range(n))
cnt=0
from itertools import combinations
for i,j,k in combinations(ss,3):
    if j-i==k-j :
        continue
    if i<j and j<k:
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
count=0
S=input()
for i in range(N):
    for j in range(i,N):
        if S[i]==S[j]:
            continue
        for k in range(j,N):
            if j-i!=k-j:
                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:
# input
N = int(input())
S = list(input())

# initialization
ans = 0

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

# output
print(ans)

Submission status: Time Limit Exceeded

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

    if N <= 3 :
        print("0")
    else :
        count = 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 S[i-1] != S[k-1] and S[j-1] != S[k-1] and (j-i) != (k-j) :
                            count = count + 1
                        #print(i,S[i-1])
                        #print(j,S[j-1])
                        #print(k,S[k-1])
        #print("count")
        print(count)

if __name__ == '__main__':
    main()