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()

Lis=[[] for _ in range(3)]
RGB="RGB"

for i in range(N):
    idx=RGB.index(S[i])
    Lis[idx].append(i+1)

Ng=len(Lis[2])
ans=0
for r in Lis[0]:
    for g in Lis[1]:
        n1=2*r-g
        n2=2*g-r
        n3=(r+g)//2
        Su=Ng
        if n1 in Lis[2]:
            Su-=1
        if n2 in Lis[2]:
            Su-=1
        if (g-r)%2==0:
            if n3 in Lis[2]:
                Su-=1
        ans+=Su
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = input()
Sc = Counter(S)
rgb = Sc["R"] * Sc["G"] * Sc["B"]
for j in range(1, N-1):
    for i in range(j):
        k = j + (j-i)
        assert i < j < k
        if k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                rgb -= 1
print(rgb)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=list(input())
sr=set()
sg=set()
sb=set()
for ii in range(n):
    if s[ii]=="R":
        sr.add(ii)
    if s[ii]=="G":
        sg.add(ii)
    if s[ii]=="B":
        sb.add(ii)
r=0
for ii in sr:
  for jj in sg:
    for kk in sb:
      if abs(ii-jj)!=abs(jj-kk) and abs(jj-kk)!=abs(kk-ii) and abs(kk-ii)!=abs(ii-jj):
        r+=1
print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=list(input())
sr=[]
sg=[]
sb=[]
for ii in range(n):
    if s[ii]=="R":
        sr.append(ii)
    if s[ii]=="G":
        sg.append(ii)
    if s[ii]=="B":
        sb.append(ii)
sr.sort()
sg.sort()
sb.sort()
r=0
for ii in range(len(sr)):
  for jj in range(len(sg)):
    for kk in range(len(sb)):
      sl=set([abs(sr[ii]-sg[jj]),abs(sg[jj]-sb[kk]),abs(sb[kk]-sr[ii])])
      #print(sl)
      if len(sl)==3:
        r+=1
print(r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
import numpy as np
r=0
for ii in range(n-2):
  for jj in np.arange(ii+1,n-1):
    if s[ii]!=s[jj]:
      for kk in range(jj+1,n):
        if ii-jj!=jj-kk:
          if s[jj]!=s[kk] and s[ii]!=s[kk]:
            r+=1
print(r)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
from sys import stdin

def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
def LS(): return list(stdin.readline().rstrip().split())
def LI(): return list(map(int,stdin.readline().rstrip().split()))
def R(x):
    r = round(x)
    if r+0.5 <= x:
        r += 1
    return r

n = I()
s = S()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = list(input()[:-1].decode())
    r = []
    g = []
    b = []
    for i in range(n):
        if s[i] == 'R':
            r.append(i)
        elif s[i] == 'G':
            g.append(i)
        elif s[i] == 'B':
            b.append(i)
    ans = len(r) * len(g) * len(b)

    def check(n):
        return b.count(n) > 0

    for ri in r:
        for gi in g:
            if check(2 * gi - ri):
                ans -= 1
            if check(2 * ri - gi):
                ans -= 1
            if (ri + gi) % 2 == 0 and check((ri + gi) // 2):
                ans -= 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Time Limit Exceeded

Solution code:
from numba import njit

N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B

@njit
def calc(ans):
    for i in range(N-2):
        for j in range(i+1, N-1):
            if 0<=j+j-i<N:
                si=S[i]
                sj=S[j]
                sk=S[2*j-i]
                if si!=sj and sj!=sk and sk!=si:
                    ans-=1
    return ans
print(calc(ans))

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B

@jit
def calc(ans):
    for i in range(N-2):
        for j in range(i+1, N-1):
            if 0<=j+j-i<N:
                si=S[i]
                sj=S[j]
                sk=S[2*j-i]
                if si!=sj and sj!=sk and sk!=si:
                    ans-=1
    return ans
print(calc(ans))

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

@jit
def main():

    N=int(input())
    S=input()
    R=0
    G=0
    B=0
    for i in range(N):
        s=S[i]
        if s=="R":
            R+=1
        elif s=="G":
            G+=1
        else:
            B+=1
    ans=R*G*B
    for i in range(N-2):
        for j in range(i+1, N-1):
            if 0<=j+j-i<N:
                si=S[i]
                sj=S[j]
                sk=S[2*j-i]
                if si!=sj and sj!=sk and sk!=si:
                    ans-=1
    print(ans)
        
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B
for i in range(N-2):
    for j in range(i+1, N-1):
        if 0<=j+j-i<N:
            si=S[i]
            sj=S[j]
            sk=S[2*j-i]
            if si!=sj and sj!=sk and sk!=si:
                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+1,N):
    if S[i] != S[j]:
      for k in range(j+1,N):
        if S[i] != S[k] and S[j] != S[k]:
          if j-i != k-j:
            #print(i,j,k)
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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


R = 0
G = 0
B = 0

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

count = R * G * B


for i in range(N):
    for j in range(1, N//2 + 1):
        if i + j*2 >= N:
            break
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


R = 0
G = 0
B = 0

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

count = R * G * B


for i in range(N):
    for j in range(1, N//2 + 1):
        if(i + j*2 >= N):
            break
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


R = 0
G = 0
B = 0

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

count = R * G * B


for i in range(N):
    for j in range(N//2 + 1):
        if(i + j*2 >= N):
            break
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


#S_R = []
#S_G = []
#S_B = []
R = 0
G = 0
B = 0

for i in range(N):
    s = S[i]
    if (s == 'R'):
        #_R.append(i)
        R +=1
    if (s == 'G'):
        #S_G.append(i)
        G +=1
    if (s == 'B'):
        #S_B.append(i)
        B +=1

count = R * G * B

#count = len(S_R) * len(S_G) * len(S_B)
#rgb = {'R', 'G', 'B'}


#for i in range(N):
    #for j in range(i):
        #x = S[j::i]
        #for a, b, c in zip(x, x[1:], x[2:]):
            #if({a,b,c} == rgb):
                #count -=1

for i in range(N):
    for j in range(N//2 + 1):
        if(i + j*2 >= N):
            break
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


#S_R = []
#S_G = []
#S_B = []
R = 0
G = 0
B = 0

for i in range(N):
    s = S[i]
    if (s == 'R'):
        #_R.append(i)
        R +=1
    if (s == 'G'):
        #S_G.append(i)
        G +=1
    if (s == 'B'):
        #S_B.append(i)
        B +=1

count = R * G * B

#count = len(S_R) * len(S_G) * len(S_B)
#rgb = {'R', 'G', 'B'}


#for i in range(N):
    #for j in range(i):
        #x = S[j::i]
        #for a, b, c in zip(x, x[1:], x[2:]):
            #if({a,b,c} == rgb):
                #count -=1

for i in range(N):
    for j in range(N//2 + 1):
        if(i + j*2 >= N):
            continue
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


#S_R = []
#S_G = []
#S_B = []
R = 0
G = 0
B = 0

for i in range(N):
    s = S[i]
    if (s == 'R'):
        #_R.append(i)
        R +=1
    if (s == 'G'):
        #S_G.append(i)
        G +=1
    if (s == 'B'):
        #S_B.append(i)
        B +=1

count = R * G * B

#count = len(S_R) * len(S_G) * len(S_B)
#rgb = {'R', 'G', 'B'}


#for i in range(N):
    #for j in range(i):
        #x = S[j::i]
        #for a, b, c in zip(x, x[1:], x[2:]):
            #if({a,b,c} == rgb):
                #count -=1

for i in range(N):
    for j in range(N):
        if(i + j*2 >= N):
            continue
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = len(S_R) * len(S_G) * len(S_B)
#rgb = {'R', 'G', 'B'}


#for i in range(N):
    #for j in range(i):
        #x = S[j::i]
        #for a, b, c in zip(x, x[1:], x[2:]):
            #if({a,b,c} == rgb):
                #count -=1

for i in range(N):
    for j in range(N):
        if(i + j*2 >= N):
            continue
        if((S[i] != S[i + j]) & (S[i + j] != S[i + 2 * j]) & (S[i + 2 * j] != S[i])):
            count -= 1




print(count)

Submission status: Time Limit Exceeded

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


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = len(S_R) * len(S_G) * len(S_B)
rgb = {'R', 'G', 'B'}


for i in range(N):
    for j in range(i):
        x = S[j::i]
        for a, b, c in zip(x, x[1:], x[2:]):
            if({a,b,c} == rgb):
                count -=1


print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from itertools import product
    n, s = int(input()), input()
    ans = 0
    rgb = {"R", "G", "B"}
    for c in range(1, n - 1):
        for l in range(0, c):
            for r in range(c + 1, n):
                f = (l - c) != (c - r)
                g = rgb == {s[c], s[r], s[l]}
                if f and g:
                    ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = input()
a = a.replace('R','1')
a = a.replace('G','2')
a = a.replace('B','3')
a = tuple(map(int,list(a)))
b = collections.Counter(a)
ans =b[3]*b[1]*b[2]
for i in range(n-2):
    for j in range(i+1,n-1):
        if a[i] == a[j]:
            continue
        c = j*2 -i
        if c < n:
            if sum([a[i],a[j],a[c]])==6:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(input())
b = collections.Counter(a)
ans =b['R']*b['G']*b['B']
for i in range(n-2):
    for j in range(i+1,n-1):
        if a[i] == a[j]:
            continue
        c = j*2 -i
        if c < n:
            if a[i] != a[j] and a[i] != a[c] and a[j] != a[c]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
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:
        continue
      if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
        cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

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

N = int(input())
S = input().strip()
l = {'R', 'G', 'B'}
r = set()
g = set()
b = set()
for i, x in enumerate(S):
    if x == 'R':
        r.add(i)
    elif x == 'G':
        g.add(i)
    else:
        b.add(i)

ans = 0
for i, x in enumerate(S):
    for j, y in enumerate(S[i + 1:]):
        if x == y:
            continue
        else:
            j += i + 1
            s = (l - {x, y}).pop()
            if s == 'R':
                n = len(list(filter(lambda x: x > j, r)))
                if j + (j - i) in r:
                    ans += n - 1
                else:
                    ans += n
            elif s == 'G':
                n = len(list(filter(lambda x: x > j, g)))
                if j + (j - i) in g:
                    ans += n - 1
                else:
                    ans += n
            else:
                n = len(list(filter(lambda x: x > j, b)))
                if j + (j - i) in b:
                    ans += n - 1
                else:
                    ans += n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
tmp = input().split()
N = int(tmp[0])
S = list(input().split())[0]
count = 0

for n in range(N)[1:N]:
    FS = S[n]
    for i in range(n):
        for j in range(N-n-1):
            if j == i:
                continue
            if not S[n-1-i] == FS and not S[n+1+j] == FS and not S[n-1-i] == S[n+1+j]:
                count = count + 1
print(count)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)
        
cnt = 0
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        for b_idx in b:
            if b_idx > j:
                if b_idx - j != j - i:
                    cnt += 1
            elif i > b_idx:
                if i - b_idx != j - i:
                    cnt += 1
            else:
                if j - b_idx != b_idx - i:
                    cnt += 1
print(cnt)

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 not(k - 2*j + i == 0) :
        if not(S[i] == S[j] or S[j] == S[k] or S[i] == S[k]):
          cnt += 1
      
ans = cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

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


def main():
    N = int(input())
    S = input()
    R = []
    G = []
    B = []
    for i in range(N):
        if i == 0:
            if S[0] == "R":
                R.append(1)
                B.append(0)
                G.append(0)
            elif S[0] == "B":
                R.append(0)
                B.append(1)
                G.append(0)
            else:
                R.append(0)
                B.append(0)
                G.append(1)
            continue
        if S[i] == "R":
            R.append(R[i - 1] + 1)
            B.append(B[i - 1])
            G.append(G[i - 1])
        elif S[i] == "G":
            R.append(R[i - 1])
            B.append(B[i - 1])
            G.append(G[i - 1] + 1)
        else:
            R.append(R[i - 1])
            B.append(B[i - 1] + 1)
            G.append(G[i - 1])

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

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


def main():
    N = int(input())
    S = input()
    answer = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if (S[i], S[j]) == ("G", "R") or (S[i], S[j]) == ("R", "G"):
                answer += S[j + 1 :].count("B")
                if 2 * j - i < N:
                    if S[2 * j - i] == "B":
                        answer -= 1
            elif (S[i], S[j]) == ("G", "B") or (S[i], S[j]) == ("B", "G"):
                answer += S[j + 1 :].count("R")
                if 2 * j - i < N:
                    if S[2 * j - i] == "R":
                        answer -= 1
            elif (S[i], S[j]) == ("B", "R") or (S[i], S[j]) == ("R", "B"):
                answer += S[j + 1 :].count("G")
                if 2 * j - i < N:
                    if S[2 * j - i] == "G":
                        answer -= 1
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# abc162_d.py
import sys
import time
import unittest
from io import StringIO
import itertools


def resolve():
    N = int(input())
    S = input()
    sum = 0
    for i, j, k in itertools.combinations(range(N), 3):
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k] and j - i != k - j:
            sum += 1
    print(sum)


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

    def assertTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    # def test_入力例_n(self):
    #     input = """9999992"""
    #     self.assertTLE(input)

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

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

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


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == "R":
        r.append(i)
    if s[i] == "G":
        g.append(i)
    if s[i] == "B":
        b.append(i)
        
cnt = 0
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        for b_idx in b:
            if b_idx > j:
                if b_idx - j != j - i:
                    cnt += 1
            elif i > b_idx:
                if i - b_idx != j - i:
                    cnt += 1
            else:
                if j - b_idx != b_idx - i:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(n):
    for j in range(i+1, n):
        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 = str(input())


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = 0

for i in S_R[::1]:
    for j in S_G[::1]:
        abs_ij =abs(i-j)
        for k in S_B[::1]:
            abs_jk = abs(k-j)
            abs_ki = abs(k-i)
            if((abs_ij!=abs_jk) & (abs_jk!=abs_ki)& (abs_ki!=abs_ij)):
                count+= 1

print(count)

Submission status: Time Limit Exceeded

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


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = 0

for i in S_R[::1]:
    for j in S_G[::1]:
        abs_ij =abs(i-j)
        for k in S_B[::1]:
            if((abs_ij!=abs(j-k)) & (abs(j-k)!=abs(k-i))& (abs(k-i)!=abs_ij)):
                count+= 1

print(count)

Submission status: Time Limit Exceeded

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


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = 0

for i in S_R[::1]:
    for j in S_G[::1]:
        for k in S_B[::1]:
            if((abs(i-j)!=abs(j-k)) & (abs(j-k)!=abs(k-i)) & (abs(k-i)!=abs(i-j))):
                count+= 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import math

def main():
    N = int(input())
    S = input()
    
    r = 0
    for i in range(N):
        for j in range(i, N):
            for k in range(j, N):
                if (i+k)/2 == j:
                    continue
                if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                    r += 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = 0

for i in S_R[::1]:
    for j in S_G[::1]:
        for k in S_B[::1]:
            if(i < j < k):
                if(j - i != k - j):
                    count += 1
            if (i < k < j):
                if (k - i != j - k):
                    count += 1
            if (j < i < k):
                if (i - j != k - i):
                    count += 1
            if (j < k < i):
                if (k - j != i - k):
                    count += 1
            if (k < i < j):
                if (i - k != j - i):
                    count += 1
            if (k < j < i):
                if (j - k != i - j):
                    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
S=list(S)
ans=S.count('R')*S.count('G')*S.count('B')

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = np.array(list(input()))
RGB_select = {
    'RG': 'B', 'GR': 'B',
    'GB': 'R', 'BG': 'R',
    'RB': 'G', 'BR': 'G'}
RGB = {
    'R':np.cumsum(s == 'R'),
    'G':np.cumsum(s == 'G'),
    'B':np.cumsum(s == 'B')
}
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
                continue
        color = RGB_select[s[i]+s[j]]
        ans += RGB[color][-1] - RGB[color][j]
        if j + (j - i) < n:
            ans -= s[j + (j-i)] == color
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i, char_i in enumerate(s):
    list_j = list(range(i, n))
    for j, char_j in enumerate(s[i:]):
        if char_i==char_j:
            continue
        for k, char_k in enumerate(s[i+j:]):
            if i+j-i != i+j+k-(i+j) and char_i!=char_k and char_j!=char_k:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i, char_i in enumerate(s):
    list_j = list(range(i, n))
    for j, char_j in enumerate(s[i:]):
        if char_i==char_j:
            continue
        for k, char_k in enumerate(s[i+j:]):
            if i+j-i != i+j+k-(i+j) and char_i!=char_k and char_j!=char_k:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i, char_i in enumerate(s):
    list_j = list(range(i, n))
    for j in list_j:
        if char_i==s[j]:
            continue
        char_j = s[j]
        list_k = list(range(j, n))
        for k in list_k:
            if j-i != k-j and char_i!=s[k] and char_j!=s[k]:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0

if N >= 3:

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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

if N >= 3:

    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 S[i] == S[k] or S[j] == S[k]:
                    continue
                if j - i != k - j:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

if N >= 3:

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    n=int(input())
    s=input().rstrip('\n')
    from collections import Counter
    a=Counter(s)
    #print(a)
    num=a["R"]*a["G"]*a["B"]
    #print(num)
    for i in range(n-2):
        for j in range(1,n//2+1):
            if i+2*j<n:
                #print([i,i+j,i+2*j,s[i], s[i+j], s[i+2*j]])
                if set([s[i],s[i+j],s[i+2*j]])==set(["R","G","B"]):
                    num-=1
    print(num)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    n=int(input())
    s=input().rstrip('\n')
    from collections import Counter
    a=Counter(s)
    #print(a)
    num=a["R"]*a["G"]*a["B"]
    #print(num)
    for i in range(n-2):
        for j in range(1,n):
            if i+2*j<n:
                #print([i,i+j,i+2*j,s[i], s[i+j], s[i+2*j]])
                if set([s[i],s[i+j],s[i+2*j]])==set(["R","G","B"]):
                    num-=1
    print(num)

if __name__ == "__main__":
    main()