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 main():
    n = int(input())
    s = input()
    if n < 4:
        print(0)
        return
    ans = 0

    for i, si in enumerate(s[:-2]):
        for j, sj in enumerate(s[i+1:-1]):
            if si == sj:
                continue
            for k, sk in enumerate(s[i+j+2:]):
                if j == k:
                    continue
                if si != sk != sj:
                    ans += 1

    print(ans)


if __name__ == '__main__':
    main()

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r, g, b = s.count("R"), s.count("G"), s.count("B")
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        try:
            k = j + (j - i)
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt += 1
        except:
            pass
print(r * g * b - cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=str(input())
r=S.count('R')
g=S.count('G')
b=S.count('B')
cnt=r*g*b
for i in range(n):
    for a in range(1,n):
        j=i+a
        k=j+a
        if k==n:
            break
        if k<n:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                cnt-=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=str(input())
r=S.count('R')
g=S.count('G')
b=S.count('B')
cnt=r*g*b
for i in range(n):
    for a in range(1,n):
        j=i+a
        k=j+a
        if k<n:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                cnt-=1
        elif k==n-1:
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

def main():
    n = int(input())
    s = input()
    cnt = 0
    for i in range(len(s)):
        x = s[i]
        for j in range(i+1, len(s)):
            y = s[j]
            if x == y:
                continue
            for k in range(j+1, len(s)):
                z = s[k]
                if z == x or z == y:
                    continue
                if j - i == k - j:
                    continue
                cnt += 1
                # print(f"{i} {j} {k}")
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
from functools import reduce
from functools import lru_cache


# \n
def input():
    return sys.stdin.readline().rstrip()

def main():

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

    count =0
    for i in range(N):
        I =S[i]

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


    print(count)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = str(input())
r = []
g = []
b = []
for i in range(n):
    if S[i] == 'R':
        r.append(i)
    elif S[i] == 'G':
        g.append(i)
    else:
        b.append(i)
#print(r, g, b)
ans = 0
for R in r:
    for G in g:
        i = min(R, G)
        j = max(R, G)
        for B in b:
            if j < B:
                if j-i != B-j:
                    ans += 1
            elif B < i:
                if j-i != i-B:
                    ans += 1
            else:
                if j-B != B-i:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = str(input())
r = []
g = []
b = []
for i in range(n):
    if S[i] == 'R':
        r.append(i)
    elif S[i] == 'G':
        g.append(i)
    else:
        b.append(i)
#print(r, g, b)
ans=0
for R in r:
    for G in g:
        #print(R,G)
        for B in b:
            l=[R,G,B]
            l.sort()
            if l[1]-l[0]!=l[2]-l[1]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = input()
RGB = {'R': [], 'G': [], 'B': []}

for i in range(N):
  RGB[S[i]].append(i)
  
count = 0

for r in RGB['R']:
  for g in RGB['G']:
    for b in RGB['B']:
      rgb = sorted([r, g, b])
      if rgb[1] - rgb[0] != rgb[2] - rgb[1]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

res=0
for i in range(N):
    for j in range(N):
        for k in range(N):
            if not i<j<k:
                continue
            if len(set([S[i],S[j],S[k]]))==3 and 2*j!=i+k:
                res+=1
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
cnt=0
for i in range(n-2):
    for j in range(i+1, n-1):
        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]):
                cnt += 1
print(r*g*b-cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    r_list, g_list, b_list = [], [], []

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

    cnt = 0
    for r in r_list:
        for g in g_list:
            for b in b_list:
                if r < g < b and g - r != b - g:
                    cnt += 1
                elif r < b < g and b - r != g - b:
                    cnt += 1
                elif g < r < b and r - g != b - r:
                    cnt += 1
                elif g < b < r and b - g != r - b:
                    cnt += 1
                elif b < r < g and r - b != g - r:
                    cnt += 1
                elif b < g < r and g - b != r - g:
                    cnt += 1

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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)

cnt = len(r)*len(g)*len(b)
for i in range(len(r)):
  for j in range(len(g)):
    if min(r[i],g[j]) - abs(r[i]-g[j]) in b:
      cnt -= 1
    if (r[i]+g[j]) / 2 in b:
      cnt -= 1
    if max(r[i],g[j]) + abs(r[i]-g[j]) in b:
      cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

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)

cnt = len(r)*len(g)*len(b)
for i in range(len(r)):
  for j in range(len(g)):
    for k in range(len(b)):
      if b[k] == min(r[i],g[j]) - abs(r[i]-g[j]):
        cnt -= 1
      if b[k] == (r[i]+g[j]) / 2:
        cnt -= 1
      if b[k] == max(r[i],g[j]) + abs(r[i]-g[j]):
        cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

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)

cnt = 0
for i in range(len(r)):
  for j in range(len(g)):
    for k in range(len(b)):
      l = [r[i],g[j],b[k]]
      l.sort(reverse=True)
      if l[1]-l[0] != l[2]-l[1]:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []

for i in range(n):
  x = s[i]
  if x == 'R':
    r.append(i)
  elif x == 'G':
    g.append(i)
  else:
    b.append(i)

cnt = 0

for i in r:
  for j in g:
    for s in b:
      cmb = [i, j, s]
      if 3*(max(cmb) + min(cmb)) - 2*sum(cmb) != 0:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []

for i in range(n):
  x = s[i]
  if x == 'R':
    r.append(i)
  elif x == 'G':
    g.append(i)
  else:
    b.append(i)

cnt = 0

for i in r:
  for j in g:
    for s in b:
      cmb = [i, j, s]
      if 3*(max(cmb) + min(cmb)) - 2*sum(cmb) == 0:
        continue
      else:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    R = set()
    G = set()
    B = set()
    for i,s in enumerate(S):
        if s == "R":
            R.add(i)
        elif s == "B":
            B.add(i)
        else:
            G.add(i)
    ans = 0
    for r in R:
        for b in B:
            for g in G:
                k = max(r, b, g)
                i = min(r, b, g)
                j = r + b + g - k - i
                if j - i != k - j:
                    ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

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

C = coun(S)
ans = C["R"] * C["G"] * C["B"]
for i, j in comb(range(N), 2):
  k = 2 * j - i
  if k < N:
    A, B, C = S[i], S[j], S[k]
    if A != B and B != C and C != A: ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = S.count("R") * S.count("G") * S.count("B")
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k < N:
      A, B, C = S[i], S[j], S[k]
      if A != B and B != C and C != A: ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#from itertools import combinations as c

import sys
input = sys.stdin.readline

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

ans = S.count("R") * S.count("G") * S.count("B")
#for i, j in c(range(N), 2):
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2 * j - i
    if k < N:
      A, B, C = S[i], S[j], S[k]
      if A != B and B != C and C != A: ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c

import sys
input = sys.stdin.readline

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

ans = S.count("R") * S.count("G") * S.count("B")
for i, j in c(range(N), 2):
  k = 2 * j - i
  if k < N:
    A, B, C = S[i], S[j], S[k]
    """
    if A == B or B == C or C == A: continue
    ans -= 1
    """
    if A != B and B != C and C != A: ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

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

"""
C = coun(S)
ans = C["R"] * C["G"] * C["B"]
"""
ans = S.count("R") * S.count("G") * S.count("B")
for i, j in comb(range(N), 2):
  k = 2 * j - i
  if k < N:
    a, b, c = S[i], S[j], S[k]
    if a == b or b == c or c == a: continue
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as coun
from itertools import combinations as comb
"""
import sys
input = sys.stdin.readline
"""
N = int(input())
S = input()

C = coun(S)
ans = C["R"] * C["G"] * C["B"]

for i, j in comb(range(N), 2):
  k = 2 * j - i
  if 0 <= k < N:
    a, b, c = S[i], S[j], S[k]
    if a == b or b == c or c == a: continue
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

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

C = coun(S)
ans = C["R"] * C["G"] * C["B"]

for i, j in comb(range(N), 2):
  k = 2 * j - i
  if 0 <= k < N:
    a, b, c = S[i], S[j], S[k]
    if a == b or b == c or c == a: continue
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

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

C = coun(S)
r, g = C["R"], C["G"]
b = N - r - g
ans = r * g * b

for i, j in comb(range(N), 2):
  k = 2 * j - i
  if 0 <= k < N:
    a, b, c = S[i], S[j], S[k]
    if a == b or b == c or c == a: continue
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

N = int(input())
S = input()
C = coun(S)
r, g, b = C["R"], C["G"], C["B"]
count = r * g * b

for i, j in comb(range(N), 2):
  k = 2 * j - i
  if 0 <= k < N:
    a, b, c = S[i], S[j], S[k]
    if a != b and b != c and c != a: count -= 1
print(count)

Submission status: Time Limit Exceeded

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

r, g, b = S.count("R"), S.count("G"), S.count("B")
count = r * g * b

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c
N = int(input())
S = list(list(input()))

count = 0
for i, j, k in c(range(N), 3):
  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:
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)
    elif S[i] == 'B':
        B.append(i+1)

cnt = 0
for r in R:
    for g in G:
        up = max(r, g)
        down = min(r, g)
        diff = up - down
        
        ng = []
        if up + diff <= N:
            ng.append(up+diff)
        if down-diff >= 1:
            ng.append(down-diff)
        if diff%2 == 0:
            ng.append(int(up-diff/2))
        chk = 0
        for i in ng:
            if S[i-1] == 'B':
                chk += 1
        cnt += len(B) - chk

print(cnt)

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

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c
N = int(input())
S = input()

count = 0
for i, j, k in c(range(N), 3):
  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
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ
from operator import mul

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def prod(list): return R(mul, list, 1)
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()
rgb = S()

cnt = CNT(list(rgb))
ans = prod([cnt[c] for c in ['R', 'G', 'B']])

for i, r in enumerate(rgb):
    for j, g in enumerate(rgb[i+1:], start=i+1):
        k = j + (j - i)
        if k < len(rgb):
            if r != g and g != rgb[k] and r != rgb[k]:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    n = int(input())
    s = np.array(list(read().rstrip()))

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

    def f(s):
        ret = 0
        for i1 in range(n):
            for i2 in range(i1 + 1, n):
                i3 = i2 * 2 - i1
                if i3 <= n - 1:
                    if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                        ret += 1
        return ret
    
    res = r * g * b - f(s)
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = [None for _ in range(n)]
    rgb_acc = [None for _ in range(n)]
    rgb_st = {"R","G","B"}
    rgb_lst = ["R","G","B"]
    for i, char in enumerate(s):
        if char == "R":
            rgb[i] = [1,0,0]
        elif char == "G":
            rgb[i] = [0,1,0]
        else:
            rgb[i] = [0,0,1]
    
    rgb_acc[0] = rgb[0]
    for i in range(1, n):
        rgb_acc[i] = rgb_acc[i-1][:]
        rgb_acc[i][rgb[i].index(1)] += 1
    
    ans = 0
    for i in range(n-2):
        for j in range(i+1,n-1):
           if s[i]!=s[j]:
                ex = rgb_st - {s[i], s[j]}
                ex = ex.pop()
                index = rgb_lst.index(ex)
                cnt = rgb_acc[n-1][index]-rgb_acc[j][index]
                if j+1 <= 2*j-i < n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt

    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil as C, floor as F, gcd as G, sqrt
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data)

  
ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

_ = I()
xs = list(S())

rs, gs, bs = [], [], []
for i, x in enumerate(xs, 1):
    if x == 'R':
        rs.append(i)
    if x == 'G':
        gs.append(i)
    if x == 'B':
        bs.append(i)

ans = 0
seen = D(lambda: False)
s = sorted([rs, gs, bs], key=lambda x: len(x))
for a in s[0]:
    for b in s[1]:
        ans += len(s[2])

        if a < b:
            i, j = a, b
        else:
            j, i = a, b

        if i - (j-i) in s[2]:
            if not seen[(i - (j-i), i, j)]:
                ans -= 1
                seen[(i - (j-i), i, j)] = True
        if j + (j-i) in s[2]:
            if not seen[(i, j, j+(j-i))]:
                ans -= 1
                seen[(i, j, j+(j-i))] = True
        if (i+j)/2 in s[2]:
            if not seen[(i, (i+j)/2, j)]:
                ans -= 1
                seen[(i, (i+j)/2, j)] = True

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data)

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

_ = I()
xs = list(S())


rs, gs, bs = [], [], []
for i, x in enumerate(xs, 1):
    if x == 'R':
        rs.append(i)
    if x == 'G':
        gs.append(i)
    if x == 'B':
        bs.append(i)

ans = 0
seen = D(lambda: False)
s = sorted([rs, gs, bs], key=lambda x: len(x))
for a in s[0]:
    for b in s[1]:
        ans += len(s[2])

        if a < b:
            i, j = a, b
        else:
            j, i = a, b

        if i - (j-i) in s[2]:
            if not seen[(i - (j-i), i, j)]:
                ans -= 1
                seen[(i - (j-i), i, j)] = True
        if j + (j-i) in s[2]:
            if not seen[(i, j, j+(j-i))]:
                ans -= 1
                seen[(i, j, j+(j-i))] = True
        if (i+j)/2 in s[2]:
            if not seen[(i, (i+j)/2, j)]:
                ans -= 1
                seen[(i, (i+j)/2, j)] = True

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import os
from operator import mul
import bisect

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
S = list(_S())
ans = 0

r = []
g = []
b = []

for i,s in enumerate(S):
    if s == 'R':
        r.append(i)
    if s == 'G':
        g.append(i)
    if s == 'B':
        b.append(i)

# j = bisect.bisect_right(<list>,<value>)
# print(r)
# print(g)
# print(b)

for i in r:
    for j in g:
        for k in b:
            tmp = sorted([i,j,k])
            if not tmp[1]-tmp[0]==tmp[2]-tmp[1]:
                ans += 1

# for i in range(N):
#     for j in range(i,N):
#         for k in range(j,N):
#             if S[i]==S[j]:
#                 continue
#             if S[i]==S[k] or S[j]==S[k]:
#                 continue
#             if (j-i) == (k-j):
#                 continue
#             ans += 1

print(ans)

# R G B
# R B G
# G B R
# G R B
# B G R
# B R G

# i   j   k
#  - != -

# RBRBGRBGGB
# 1122233333
# 0000111233
# 0112223334

# B*3
# G*4

# GB系: (2,4), (4,5)
# RB系: (1,3),

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
S = list(_S())
ans = 0

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

print(ans)

# R G B
# R B G
# G B R
# G R B
# B G R
# B R G

# i   j   k
#  - != -

# RBRBGRBGGB
# 1122233333
# 0000111233
# 0112223334

# B*3
# G*4

Submission status: Time Limit Exceeded

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

c = set(['R', 'G', 'B'])
ans = 0
for i in range(n-2):
    d = set(s[i])
    for j in range(i+1, n-1):
        if s[i] != s[j]:
            e = d.copy()
            e.add(s[j])
            p = c-e
            for k in p:
                ans += s[j+1:].count(k)
                if j+j-i < n:
                    if s[j+j-i] == 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):
    for j in range(i, n-1):
        if s[i] != s[j]:
            ji = j - i
            for k in range(j, n):
                if s[i] != s[k] and s[j] != s[k]:
                    if ji != k - j:
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

I = []
J = []
K = []

for n in range(1,N+1):
  if S[n-1] == "R":
    I.append(n)
  elif S[n-1] == "G":
    J.append(n)
  else:
    K.append(n)
    
cnt = 0
for a in I:
  for b in J:
    for c in K:
      ls = [a,b,c]
      ls.sort()
      if ls[1] - ls[0] != ls[2] - ls[1]:
        cnt += 1
    
print(cnt)

Submission status: Time Limit Exceeded

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

ls = []
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:
        ls.append((i,j,k))
        
cnt = 0
for i,j,k in ls:
  if S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[k-1] != S[i-1]:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
if n<=2:
    print(0)
    exit()
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans=r*g*b
def moji(in1,in2):
    l=['R','G','B']
    l.remove(in1)
    l.remove(in2)
    return l[0]
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        m=moji(s[i],s[j])
        if 2*j-i<=n-1:
          if s[2*j-i]==m:
            ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[str(i) for i in input()]
cnt=0

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[str(i) for i in input()]
cnt=0

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

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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