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

    r_cnt = [0 for _ in range(n)]
    g_cnt = [0 for _ in range(n)]
    b_cnt = [0 for _ in range(n)]

    for i in reversed(range(n)):
        if s[i] == 'R':
            r_cnt[i] = 1
        if s[i] == 'G':
            g_cnt[i] = 1
        if s[i] == 'B':
            b_cnt[i] = 1

    for i in reversed(range(1, n)):
        r_cnt[i - 1] += r_cnt[i]
        g_cnt[i - 1] += g_cnt[i]
        b_cnt[i - 1] += b_cnt[i]


    ans = 0
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            if s[i] == s[j]:
                continue
            dist = j - i
            if {s[i], s[j]} == {'R', 'G'}:
                ans += b_cnt[j + 1]
                if j + dist < n and s[j + dist] == 'B':
                    ans -= 1
            elif {s[i], s[j]} == {'R', 'B'}:
                ans += g_cnt[j + 1]
                if j + dist < n and s[j + dist] == 'G':
                    ans -= 1
            else:
                ans += r_cnt[j + 1]
                if j + dist < n and s[j + dist] == 'R':
                    ans -= 1
    print(ans)


submit()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
ans = 0
 
for i in range(N):
    first_s = S[i]
    for j in range(i+1, N):
        if S[j] != first_s:
            second_s = S[j]
            for k in range(j+1, N):
                if k == 2*j-i:
                    if (S[k] != first_s) and (S[k] != second_s):
                        continue
                if (S[k] != first_s) and (S[k] != second_s):
                    ans += 1
print(ans)

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

Submission status: Time Limit Exceeded

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

for i in range(N):
    a = S[i]
    for j in range(i+1,N):
        b = S[j]
        if a==b:
            continue
        for k in range(j+1,N):
            c = S[k]
            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:
def main():
    NumString = int(input())
    String = input()
    
    total_ans = 0
    for i in range(NumString):
        for j in range(i, NumString):
            for k in range(j, NumString):
                if String[i] != String[j] and String[j] != String[k] and \
                    String[i] != String[k] and j - i != k - j:
                    total_ans += 1
    
    print(total_ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

cnt = 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[j]!=s[k]:
                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(0, N):
    for j in range(i+1, N):
        if(S[i] != S[j]):
            for k in range(j+1, N):
                if(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:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    R = [0]*N
    G = [0]*N
    B = [0]*N
    for i in range(N-1,-1,-1):
        if S[i] == "R":
            if i == N-1:
                R[i] = 1
            else:
                R[i] = R[i+1] + 1 
                G[i] = G[i+1]
                B[i] = B[i+1]
        elif S[i] == "G":
            if i == N-1:
                G[i] = 1
            else:
                R[i] = R[i+1] 
                G[i] = G[i+1] + 1
                B[i] = B[i+1]
        elif S[i] == "B":
            if i == N-1:
                B[i] = 1
            else:
                R[i] = R[i+1] 
                G[i] = G[i+1]
                B[i] = B[i+1]+1

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

    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

r = [0] * (n)
g = [0] * (n)
b = [0] * (n)
if s[0] == 'R':
  r[0] = 1
elif s[0] == 'G':
  g[0] = 1
else:
  b[0] = 1

for i in range(n-1):
  r[i+1] = r[i]
  g[i+1] = g[i]
  b[i+1] = b[i]
  
  if s[i+1] == 'R':
    r[i+1] += 1
  elif s[i+1] == 'G':
    g[i+1] += 1
  else:
    b[i+1] += 1
    
ans = 0
for i in range(n):
  if s[i] == 'R':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'G':
        ans += b[-1] - b[j]
        if j+j-i < n and s[j+j-i] == 'B':
          ans -= 1
          
      if s[j] == 'B':
        ans += g[-1] - g[j]
        if j+j-i < n and s[j+j-i] == 'G':
          ans -= 1        
        
  if s[i] == 'G':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'R':
        ans += b[-1] - b[j]
        if j+j-i < n and s[j+j-i] == 'B':
          ans -= 1        
        
      if s[j] == 'B':
        ans += r[-1] - r[j]
        if j+j-i < n and s[j+j-i] == 'R':
          ans -= 1        
        
  if s[i] == 'B':
    for j in range(i+1,n):
      if j+1 >= n:
        continue
        
      if s[j] == 'G':
        ans += r[-1] - r[j]
        if j+j-i < n and s[j+j-i] == 'R':
          ans -= 1        
        
      if s[j] == 'R':
        ans += g[-1] - g[j]
        if j+j-i < n and s[j+j-i] == 'G':
          ans -= 1        

        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
s=input()
count=0
for i in range(a-2):
    for j in range(i+1,a-1):
        if s[i]!=s[j]:
            for h in range(j+1,a):
                if s[i]!=s[h] and s[j]!=s[h] and h+i!=2*j:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if(n<3):
    print(0)
    exit()
sum=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[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()
answer = 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[i-1] != S[k-1] and S[j-1] != S[k-1]:
                    answer += 1

print(answer)

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

if n == 1 or n == 2:
    print(0)
    
else:
    x = 0
    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] and j-i != k-j:
                    x += 1
    print(x)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    s = input()

    rgb = defaultdict(list)

    for i in range(n):
        if s[i] == 'R':
            rgb[0].append(i)
        elif s[i] == 'G':
            rgb[1].append(i)
        else:
            rgb[2].append(i)

    ans = 0
    for r in rgb[0]:
        for g in rgb[1]:
            for b in rgb[2]:
                tmp = sorted([r, g, b])
                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()
ans = 0
for p1 in range(n-3):
    for p2 in range(p1+1, n-1):
        for p3 in range(p2+1, n):
            #print(s[p1], s[p2], s[p3])
            if (s[p1] != s[p2]) and (s[p1] != s[p3]) and (s[p2] != s[p3]) and (p1-p2 != p2-p3):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

rel = [1 if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] 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()
sum = 0
if n < 4:
    print(0);exit()
for i in range(n):
    for j in range(i+1,n):
        if s[i] == s[j]:
            continue
        dis = j - i
        for k in range(j+ 1,n):

            if s[k] == s[i] or s[k] == s[j] or k - j == dis:
                continue
            sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    n = int(input())
    s = input()
    index = [i for i in range(n)]
    comb = itertools.combinations(index, 3)
    ans = 0
    for c in comb:
        if c[1] - c[0] == c[2] - c[1]:
            continue
        if s[c[0]] != s[c[1]] and s[c[0]] != s[c[2]] and s[c[1]] != s[c[2]]:
            ans += 1
    return ans


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

Submission status: Time Limit Exceeded

Solution code:
import string
import itertools as it
import math
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))


def sum_comb(n):
    result = []
    data = range(1,n+1)
    for i in range(len(data)):
        for j in range(i, len(data)):
            for k in range(j, len(data)):
                result.append([data[i], data[j], data[k]])
    return result

N = num()
S = input()

first,second,third = 0,0,0
ans = 0

for i in range(N):
    first = i
    for j in range(first+1, N):
        second = j
        for k in range(second+1, N):
            third = k
            Si,Sj,Sk = S[i],S[j],S[k]
            if Si != Sj and Si != Sk and Sj != Sk and (j - i) != (k - j):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R, G, B = [], [], []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
cnt = 0
for i in R:
    for j in G:
        for k in B:
            a = sorted([i, j, k])
            if a[1]-a[0] == a[2]-a[1]:
                cnt += 1
print (len(R)*len(G)*len(B)-cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(0, n, 1):
    for j in range(i+1, n, 1):
        for k in range(j+1, n, 1):
            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 = input()
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
all = comb(N,3)
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] or s[j-1] == s[k-1] or s[i-1] == s[k-1] or j-i == k-j) :
                all -= 1
print(all)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


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

rs, gs, bs = [], [], []
for l, c in enumerate(s):
  if c == 'R':
    rs.append(l)
  if c == 'G':
    gs.append(l)
  if c == 'B':
    bs.append(l)
sbs = set(bs)

ans = 0
for r in rs:
  for g in gs:
    m = min(r, g)
    M = max(r, g)
    notbs = [m - (M - m), M + (M - m)]
    if (M + m) % 2 == 0:
      notbs.append((M + m) // 2)
    dprint(m, M, notbs, bs)
    a = len(sbs - set(notbs))
    if a > 0:
      ans += a


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


##get int
first_input = list(map(lambda x:int(x), input().split(" ")))
#second_input = list(map(lambda x:int(x), input().split(" ")))

##get float
#first_input = list(map(lambda x:float(x), input().split(" ")))
#second_input = list(map(lambda x:float(x), input().split(" ")))

##get string
#first_input = input().split(" ")
second_input = input().split(" ")

N = first_input[0]
S = second_input[0]

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

print(count)

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 j-i != k-j and 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:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    s = IS()
    cnt = 0
    for i in range(n-2):
        x = s[i]
        for j in range(i+1, n-1):
            y = s[j]
            if x == y: continue
            for k in range(j+1, n):
                z = s[k]
                if x == z: continue
                if y == z: continue
                if j-i==k-j: continue
                cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

print(total)

Submission status: Time Limit Exceeded

Solution code:
#


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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = list(map(ord, ns()))
d = dict()
for x in map(ord, list('RGB')):
    d[x] = s.count(x)
tot = sum(map(ord, list('RGB')))
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = tot - s[i] - s[j]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys

sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N = ii()
S = si()

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)

colors = [
    [R, G, B],
    [R, B, G],
    [G, R, B],
    [G, B, R],
    [B, R, G],
    [B, G, R],
]

cand = []
for C1, C2, C3 in colors:
    for c1 in C1:
        for c2 in C2:
            if c1 > c2:
                continue
            for c3 in C3:
                if c2 > c3:
                    continue
                if c1 > c3:
                    continue
                cand.append((c1,c2,c3))

ans = 0
for c1, c2, c3 in cand:
    if c2 - c1 != c3 - c2:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

# r_num = [0 for i in range(n)]
# g_num = [0 for i in range(n)]
# b_num = [0 for i in range(n)]

nums = [[0 for i in range(n)] for j in range(3)]

ans = 0

befs = [0 for i in range(3)]
for i in range(n):
    if s[i] == "R":
        befs[0] += 1
    elif s[i] == "G":
        befs[1] += 1
    else:
        befs[2] += 1
    for j in range(3):
        nums[j][i] = befs[j]

vac = [[-1,2,1],
       [2,-1,0],
       [1,0,-1]]
tr = {"R":0,"G":1,"B":2}

for i in range(n):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        else:
            v1 = tr[s[i]]
            v2 = tr[s[j]]
            nex = vac[v1][v2]
            # print(nex)
            tp = nums[nex][-1]-nums[nex][j+1]
            if tr[s[j+1]] == nex:
                tp += 1
            if j+j-i <= n-1 and tr[s[j+j-i]] == nex:
                tp -= 1
            ans += tp

print(ans)

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(1,n):
    for j in range(n):
        if(j+i*2>=n):continue
        d1,d2,d3=s[j],s[j+i],s[j+i*2]
        if(not(d1==d2 or d2==d3 or d1==d3)):ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S =input()
count =0
for i in range(0,N-2):
    stack =["R","G","B"]
    stack.remove(S[i])
    for j in range(i,N-1):
        dis = j -i
        if S[i] == S[j]:
            continue
        stack.remove(S[j])
        for k in range(j,N):
            if k -j == dis:
                continue
            if S[k] == stack[0]:
                count += 1
        stack.append(S[j])
print(count)

Submission status: Time Limit Exceeded

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

i = 0
j = 1
k = 2

cnt = 0

while i != N - 2:

	if j - i == k - j:
		pass

	else:
		if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
			cnt = cnt + 1
		else:
			pass
			
	if k != N - 1:
		k = k + 1
	elif j != N - 2:
		j = j + 1
		k = j + 1
	else:
		i = i + 1
		j = i + 1
		k = j + 1

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

sum = 0
for i in range(N):
    for j in range(i+1, N):
        RGB = ['R','G','B']
        if not S[i] == S[j]:
            RGB.remove(S[i])
            RGB.remove(S[j])
            sum += S[(j+1):].count(RGB[0])

            # S[k]
            k = 2 * j - i
            if k < len(S):
                if S[k] == RGB[0]:
                    sum -= 1
print(sum)

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+1)
    elif S[i] == "G":
        G.append(i+1)
    else:
        B.append(i+1)

ans = 0
for r in R:
    for g in G:
        for b in B:
            i, k = min(r, g, b), max(r, g, b)
            j = r+g+b-i-k
            if j-i!=k-j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    N = int(input())
    S = input()
    id_l = [i for i in range(N)]

    ans = 0
    for v in itertools.permutations(id_l, 3):
        if v[0] < v[1] < v[2]:
            if (v[2] - v[1]) != (v[1] - v[0]):
                if (S[v[0]] != S[v[1]]) and (S[v[1]] != S[v[2]]) and (S[v[0]] != S[v[2]]):
                    ans += 1
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(input())
R=[0 for _ in range(n)]
G=[0 for _ in range(n)]
B=[0 for _ in range(n)]
r=0;g=0;b=0
for i in range(n):
    if S[i]=="R":
        r+=1 
    if S[i]=="G":
        g+=1
    if S[i]=="B":
        b+=1
    R[i]=r; G[i]=g; B[i]=b
cnt=0
for i in range(n-2):
    for j in range(i,n-1):
        if S[i]==S[j]:
            continue
        if (S[i]=="R" or S[i]=="G") and (S[j]=="R" or S[j]=="G"):
            cnt+=B[-1]-B[j]
            try:
                if S[2*j-i]=="B":
                    cnt-=1
            except:
                pass
        if (S[i]=="G" or S[i]=="B") and (S[j]=="G" or S[j]=="B"):
            cnt+=R[-1]-R[j]
            try:
                if S[2*j-i]=="R":
                    cnt-=1
            except:
                pass
        if (S[i]=="B" or S[i]=="R") and (S[j]=="B" or S[j]=="R"):
            cnt+=G[-1]-G[j]
            try:
                if S[2*j-i]=="G":
                    cnt-=1
            except:
                pass
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if n<=3:
    print(0)
    exit()
ans=0
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:
import itertools as it

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

s = [int(i) for i in s.replace('R', '0').replace('G', '1').replace('B', '2')]
r_indeces = [i for i in range(n) if s[i] == 0]
g_indeces = [i for i in range(n) if s[i] == 1]
b_indeces = [i for i in range(n) if s[i] == 2]

indeces = [sorted(list(i))
           for i in it.product(r_indeces, g_indeces, b_indeces)]
indeces = [i for i in indeces if i[1] - i[0] != i[2] - i[1]]
print(len(indeces))

Submission status: Time Limit Exceeded

Solution code:
import sys,math
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
S = list(input())

ANS = 0

for i in range(N):
    f = S[i]
    for j in ['R','G','B']:
        if j==f:
            continue
        #残りの二文字のどっちかに一致するものを探す
        for k in range(i+1,N):
            elm = S[k]
            if elm!=j:
                continue
            s = elm
            #距離
            dist = k-i
            for l in range(k+1,N):
                if S[l]!=s and S[l]!=f:
                    if dist != l-k:
                        ANS += 1
print(ANS)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

N = int(input())
S = input()
count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

sum = 0
for k in range(2, n):
    for j in range(1, k):
        for i in range(j):
            if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
                continue
            if j - i == k - j:
                continue
            sum += 1

print(sum)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
seq = input()
ans = 0
for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        if seq[i] == seq[j]:
            continue
        for k, val in enumerate(seq[j+1:], j+1):
            if seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
d = int(input())
s = input()
R, G, B = [], [], [] 

for i, c in enumerate(s):
    if c == "R":
        R.append(i)
    elif c == "G":
        G.append(i)
    else:
        B.append(i)

ans = 0
for r in R:
    for g in G:
        for b in B:
            x = sorted([r, g, b])
            if x[1] - x[0] != x[2] - x[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
# i: 0～N-2
for i in range(0, N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        for k in range(j+1, N):
            if j == (k + i) / 2:
                continue
            if S[j] == S[k]:
                continue
            elif S[i] == S[k]:
                continue

            else:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

ans = 0
R, G, B = [], [], []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            if g - r != b - g and r < g < b:
                # L.append((r,g,b))
                ans += 1

for r in R:
    for b in B:
        for g in G:
            if b - r != g - b and r < b < g:
                # L.append((r,g,b))
                ans += 1
                
for g in G:
    for b in B:
        for r in R:
            if b - g != r - b and g < b < r:
                # L.append((r,g,b))
                ans += 1

for g in G:
    for r in R:
        for b in B:
            if r - g != b - r and g < r < b:
                # L.append((r,g,b))
                ans += 1

for b in B:
    for g in G:
        for r in R:
            if g - b != r - g and b < g < r:
                # L.append((r,g,b))
                ans += 1

for b in B:
    for r in R:
        for g in G:
            if r - b != g - r and b < r < g:
                # L.append((r,g,b))
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

R = [v for v,i in enumerate(S) if i == 'R']
G = [v for v,i in enumerate(S) if i == 'G']
B = [v for v,i in enumerate(S) if i == 'B']

c = 0
for i in R:
    for j in G:
        for k in B:
            t = [i,j,k]
            t.sort()
            if not t[0] + t[2] == 2 * t[1]:
                c += 1

print(c)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=0
for i in range(n):
    for j in range(i+1,n):
        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:
                    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

com=[]
li = com.append
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):
                li([i-1, j-1, k-1])

count =0
for st in com:
    if (S[st[0]] != S[st[1]]) and (S[st[1]] != S[st[2]]) and (S[st[0]] != S[st[2]]):
        count +=1

print(count)