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

ans = 0

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[]
G=[]
B=[]
count=1
for i in S:
    if i=="R":
        R.append(count)
    if i=="G":
        G.append(count)
    if i=="B":
        B.append(count)
    count+=1
x=0
for j in R:
    for k in G:
        if 2*j-k in B:
            x+=1
        if 2*k-j in B:
            x+=1
        if (k+j)/2==(k+j)//2 and (k+j)//2 in B:
            x+=1
print(len(R)*len(G)*len(B)-x)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N=int(input())
S=input()
R=deque()
G=deque()
B=deque()
count=1
for i in S:
    if i=="R":
        R.append(count)
    if i=="G":
        G.append(count)
    if i=="B":
        B.append(count)
    count+=1
ans=0
for j in R:
    for k in G:
        x=len(B)
        if 2*j-k in B:
            x-=1
        if 2*k-j in B:
            x-=1
        if (k+j)/2==(k+j)//2 and (k+j)//2 in B:
            x-=1
        ans+=x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def answer():
  global S
  count = 0
  for i in range(len(S)-2):
    for j in range(i+1, len(S)-1):
      if S[i] == S[j]: continue
      for k in range(j+1, len(S)):
        if S[k] == S[j] or S[k] == S[i]: continue
        if j - i == k - j: continue
        count += 1
  print(count)

n = int(input())
S = input()
answer()

Submission status: Time Limit Exceeded

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

    R = list()
    G = list()
    B = list()

    for i in range(len(S)):
        if   S[i] == "R":
            R.append(i)
        elif S[i] == "G":
            G.append(i)
        elif S[i] == "B":
            B.append(i)

    ans = 0
    
    for r in R:
        for g in G:
            a, b = sorted([r, g])
            tmp = len(B)

            if ((b + a) % 2 == 0) and ((b + a) // 2) in B:
                tmp -= 1

            if (2*a - b) in B:
                tmp -= 1

            if (2*b - a) in B:
                tmp -= 1

            ans += tmp
                
    print(ans)

if __name__ == "__main__":
    main4()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
s = rr()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
    for j in range(i+1, n):
        k = j + j - i
        if k < n:
            if s[k] != s[j] and s[j] != s[i] and s[k] != s[i]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

    R = list()
    G = list()
    B = list()

    for i in range(len(S)):
        if   S[i] == "R":
            R.append(i)
        elif S[i] == "G":
            G.append(i)
        elif S[i] == "B":
            B.append(i)

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

if __name__ == "__main__":
    main4()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[]
G=[]
B=[]
count=1
for i in S:
    if i=="R":
        R.append(count)
    if i=="G":
        G.append(count)
    if i=="B":
        B.append(count)
    count+=1
ans=0
for j in R:
    for k in G:
        x=len(B)
        if 2*j-k in B:
            x-=1
        if 2*k-j in B:
            x-=1
        if (k+j)/2==(k+j)//2 and (k+j)//2 in B:
            x-=1
        ans+=x
print(ans)

Submission status: Time Limit Exceeded

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

#logging.debug("デバッグスタート") #
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] != s[j]:
            if (s[i] == "R" and s[j]=="G") or (s[i] == "G" and s[j]=="R"):
                ans += s[j + 1 :].count("B")
                if (j - i + j) < n:
                    if s[j - i + j] == "B":
                        ans -= 1
                    continue
            if (s[i] == "R" and s[j]=="B") or (s[i] == "B" and s[j]=="R"):
                ans += s[j + 1 :].count("G")
                if (j - i + j) < n:
                    if s[j - i + j] == "G":
                        ans -= 1
                    continue
            if (s[i] == "B" and s[j]=="G") or (s[i] == "G" and s[j]=="B"):
                ans += s[j + 1 :].count("R")
                if (j - i + j) < n:
                    if s[j - i + j] == "R":
                        ans -= 1
                    continue

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
# RGBの時点で一つ目の条件を満たすものは少ないはず
d = {}
for i in range(N):
    for j in range(i+1, N):
        if S[i] != S[j]:
            d[(i, j)] = 1
counter = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if (i, j) in d:
                if (i, k) in d:
                    if (j, k) in d:
                        if j-i != k-j:
                            counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    rr = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']

    r = len(rr) * len(g) * len(b)
    for rre in rr:
        for ge in g:
            for be in b:
                r -= rre * 2 == ge + be
                r -= ge * 2 == rre + be
                r -= be * 2 == ge + rre
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [0] * n
    g = [0] * n
    b = [0] * n
    for i, se in enumerate(s):
        if se == 'R':
            r[i] = 1
        elif se == 'G':
            g[i] = 1
        else:
            b[i] = 1

    res = sum(r) * sum(g) * sum(b)
    minus = 0
    for i1 in range(n):
        for i2 in range(n):
            for i3 in range(n):
                if r[i1] and g[i2] and b[i3]:
                    minus += i1 * 2 == (i2 + i3)
                    minus += i2 * 2 == (i1 + i3)
                    minus += i3 * 2 == (i1 + i2)
    print(res-minus)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += sum(c) * 2 != (min(c) + max(c)) * 3
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = list(input())
c = Counter(s)
ans = c["R"]*c["G"]*c["B"]
for i in range(n-2):
  for j in range(1,n):
    if i+2*j >= n:
      break
    if sorted([s[i],s[i+j],s[i+2*j]]) == ["B","G","R"]:
      ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += c[0] < c[1] < c[2] and c[1] - c[0] != c[2] - c[1]
        r += c[0] < c[2] < c[1] and c[2] - c[0] != c[1] - c[2]
        r += c[2] < c[1] < c[0] and c[0] - c[1] != c[1] - c[2]
        r += c[2] < c[0] < c[1] and c[0] - c[2] != c[1] - c[0]
        r += c[1] < c[0] < c[2] and c[0] - c[1] != c[2] - c[0]
        r += c[1] < c[2] < c[0] and c[2] - c[1] != c[0] - c[2]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

from bisect import bisect_right

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

d = {"R": [], "G": [], "B": []}
for i in range(N):
    d[S[i]].append(i)

ctr = {k: len(v) for k, v in d.items()}
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        if S[i] != S[j]:
            c = (set("RGB") - set([S[i], S[j]])).pop()
            cnt = ctr[c] - bisect_right(d[c], j)

            k = 2 * (j + 1) - (i + 1) - 1
            if j < k < N and c == S[k]:
                cnt -= 1

            ans += max(0, cnt)

print(ans)

Submission status: Time Limit Exceeded

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

    R = list()
    G = list()
    B = list()

    for i in range(len(S)):
        if S[i] == "R":
            R.append(i)
        elif S[i] == "G":
            G.append(i)
        elif S[i] == "B":
            B.append(i)

    ans = 0
    for i in R:
        for j in G:
            for k in B:
                li = sorted([i, j, k])
                a, b, c = li[0], li[1], li[2]

                if abs(a - b) != abs(b - c):
                    ans += 1
    print(ans)

if __name__ == "__main__":
    main3()

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 + 1, N - 1):
            if S[i] == S[j]:
                continue

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
red=[]
blue=[]
green=[]
s_r=0
s_g=0
s_b=0
dic={}
dic['R']=1
dic['G']=2
dic['B']=3
for i in range(len(s)):
    if s[i]=='R':
        s_r+=1
    elif s[i]=='G':
        s_g+=1
    elif s[i]=='B':
        s_b+=1
    red.append(s_r)
    blue.append(s_b)
    green.append(s_g)
x=(1^(2^3))
ans=0
for i in range(len(s)):
    for k in range(i+2,len(s)):
        if s[k]!=s[i]:
            a=x^dic[s[k]]^dic[s[i]]
            if a==1:
                ans+=(red[k-1]-red[i])
            if a==2:
                ans+=(green[k-1]-green[i])
            if a==3:
                ans+=(blue[k-1]-blue[i])
            if (k+i)%2==0 and dic[s[(i+k)//2]]==a:
                ans-=1
print (ans)

Submission status: Time Limit Exceeded

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

c=0
for k in range(n):
    for j in range(n):
        if j>=k:
            break
        for i in range(n):
            if i>=j:
                break
            if (s[i]!=s[j]) and (s[j]!=s[k]) and (s[i]!=s[k]) and ((j-i)!=(k-j)):
                c+=1

print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s1=""

for i in s:
    if i=="R":s1+="1"
    elif i=="G":s1+="2"
    elif i=="B":s1+="4"


l1=int(s1,16)
l2=int(s1,16)

ans=0

l2*=16

for i in range(2,n):
    l2*=16
    l3=int(s1,16)
    for j in range(1,i):
        l3*=16
        if j==i-j:
            continue
        tasi=l1+l2+l3
        ans+=hex(tasi).count("7")

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s1=""

for i in s:
    if i=="R":s1+="1"
    elif i=="G":s1+="2"
    elif i=="B":s1+="4"

ans=0

import itertools
for seq in itertools.combinations(range(1,n),2):
    if seq[0]==seq[1]-seq[0]:
        continue
    l1=int(s1,16)
    l2=int(s1,16)*16**seq[0]
    l3=int(s1,16)*16**seq[1]

    tasi=l1+l2+l3

    ans+=hex(tasi).count("7")

print(ans)

Submission status: Time Limit Exceeded

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

import itertools
for seq in itertools.combinations(range(n),3):
    if s[seq[0]]!=s[seq[1]] and s[seq[1]]!=s[seq[2]] and s[seq[2]]!=s[seq[0]] and seq[1]-seq[0]!=seq[2]-seq[1]:
        ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
import itertools
S=input()
Ns=[i for i in range(N)]
ans=0
#RGBの選び方の問題
#長さは4000.4000*C3だから間に合うのでは？？？
for v in itertools.combinations(Ns, 3):
    if v[1]-v[0]!=v[2]-v[1]:
        x=str(S[v[0]])+str(S[v[1]])+str(S[v[2]])
        if ("R" in x) and ("G" in x) and ("B" in x):
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

# 組み合わせ総数
ans = S.count('R') * S.count('G') * S.count('B')

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
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[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 = str(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] 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:
from itertools import *
N=int(input())
S=str(input())
R=[]
G=[]
B=[]
kot=0
for a in range(N):
    for b in range(a+1,N):
                B=[a,b]
                R.append(B)

for s in range(len(R)):
    for a in range(R[s][1]+1,N):
        if abs(a-R[s][1])!=abs(R[s][1]-R[s][0]):
          
            if S[R[s][0]]!=S[R[s][1]]and S[R[s][0]]!=S[a] and S[a]!=S[R[s][1]]:
                kot+=1
print(kot)
"""for a in range(len(S)):
    if S[a]=="R":
        R.append(int(a))
    elif S[a]=="G":
        G.append(int(a))
    else:
        B.append(int(a))

for a in R:
    for g in G:
        for b in B:
            if abs(a-g)!=abs(b-g) and abs(a-b)!=abs(g-b) and abs(a-g)!=abs(a-b):
                kot+=1
print(kot)"""

"""i=0
com=[]
kot=0
for _ in range(N):
   com.append(i)
   i+=1
for v in combinations(com,3):
    if v[1]-v[0]!=v[2]-v[1]:
        if S[v[0]]!=S[v[1]] and S[v[1]]!=S[v[2]] and S[v[0]]!=S[v[2]]:
            kot+=1
print(kot)
#for b in range(N):"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import *
N=int(input())
S=str(input())
R=[]
G=[]
B=[]
kot=0
for a in range(N):
    for b in range(a+1,N):
        for c in range(b+1,N):
            if c-b!=b-a:
                B=[a,b,c]
                R.append(B)

for s in range(len(R)):
    if S[R[s][0]]!=S[R[s][1]]and S[R[s][0]]!=S[R[s][2]] and S[R[s][2]]!=S[R[s][1]]:
        kot+=1
print(kot)
"""for a in range(len(S)):
    if S[a]=="R":
        R.append(int(a))
    elif S[a]=="G":
        G.append(int(a))
    else:
        B.append(int(a))

for a in R:
    for g in G:
        for b in B:
            if abs(a-g)!=abs(b-g) and abs(a-b)!=abs(g-b) and abs(a-g)!=abs(a-b):
                kot+=1
print(kot)"""

"""i=0
com=[]
kot=0
for _ in range(N):
   com.append(i)
   i+=1
for v in combinations(com,3):
    if v[1]-v[0]!=v[2]-v[1]:
        if S[v[0]]!=S[v[1]] and S[v[1]]!=S[v[2]] and S[v[0]]!=S[v[2]]:
            kot+=1
print(kot)
#for b in range(N):"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import *
N=int(input())
S=str(input())
R=[]
G=[]
B=[]
kot=0
for a in range(len(S)):
    if S[a]=="R":
        R.append(int(a))
    elif S[a]=="G":
        G.append(int(a))
    else:
        B.append(int(a))

for a in R:
    for g in G:
        for b in B:
            if abs(a-g)!=abs(b-g) and abs(a-b)!=abs(g-b) and abs(a-g)!=abs(a-b):
                kot+=1
print(kot)

"""i=0
com=[]
kot=0
for _ in range(N):
   com.append(i)
   i+=1
for v in combinations(com,3):
    if v[1]-v[0]!=v[2]-v[1]:
        if S[v[0]]!=S[v[1]] and S[v[1]]!=S[v[2]] and S[v[0]]!=S[v[2]]:
            kot+=1
print(kot)
#for b in range(N):"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import *
N=int(input())
S=str(input())
i=0
com=[]
kot=0
for _ in range(N):
   com.append(i)
   i+=1
for v in combinations(com,3):
    if v[1]-v[0]!=v[2]-v[1]:
        if S[v[0]]!=S[v[1]] and S[v[1]]!=S[v[2]] and S[v[0]]!=S[v[2]]:
            kot+=1
print(kot)
#for b in range(N):

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

num = len(s)
ans = 0
for i in range(num):
    a = [s[i]]
    for j in range(i+1, num):
        if s[j] in set(a):
            continue
        a.append(s[j])
        for k in range(j+1, num):
            if s[k] in set(a) or j-i == k-j:
                continue
            ans += 1
        a.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
S = list(input())
A=list(itertools.combinations(list(range(N)),3))
ans =0
for i in A:
    if(i[1]-i[0] != i[2]-i[1]):
        if((S[i[0]]!=S[i[1]]) and (S[i[0]]!=S[i[2]]) and (S[i[1]] != S[i[2]])):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = input()
R_ls = [0] * n
G_ls = [0] * n
B_ls = [0] * n
for i in range(n):
    if s[i] == 'R':
        R_ls[i] = 1
    elif s[i] == 'G':
        G_ls[i] = 1
    else:
        B_ls[i] = 1
R_csum = np.cumsum(R_ls)
G_csum = np.cumsum(G_ls)
B_csum = np.cumsum(B_ls)
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if (s[i] == 'R' and s[j] == 'G') or (s[i] == 'G' and s[j] == 'R'):
            now = B_csum[-1] - B_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'B':
                now -= 1
        elif (s[i] == 'R' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'R'):
            now = G_csum[-1] - G_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'G':
                now -= 1
        elif (s[i] == 'G' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'G'):
            now = R_csum[-1] - R_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'R':
                now -= 1
        else:
            now = 0
        ans += now
print(ans)

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

Submission status: Time Limit Exceeded

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

def str_finditer(S,s):
    n = 0
    while True:
        n = S.find(s,n)
        if n == -1:
            break
        yield n
        n += 1

R = list(str_finditer(S, 'R'))
G = list(str_finditer(S, 'G'))
B = list(str_finditer(S, 'B'))

ans = 0
for i in R:
    for j in G:
        for k in B:
            if len(set([abs(i - j), abs(j - k), abs(k - i)])) == 3:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

def str_finditer(S,s):
    n = 0
    while True:
        n = S.find(s,n)
        if n == -1:
            break
        yield n
        n += 1

R = list(str_finditer(S, 'R'))
G = list(str_finditer(S, 'G'))
B = list(str_finditer(S, 'B'))

ans = len(R) * len(G) * len(B)

for i in R:
    for j in G:
        for k in B:
            L = sorted((i,j,k))
            if L[2] == L[1] + L[1] - L[0]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

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

R = [m.span()[0] for m in re.finditer('R', S)]
G = [m.span()[0] for m in re.finditer('G', S)]
B = [m.span()[0] for m in re.finditer('B', S)]

ans = len(R) * len(G) * len(B)

for i in R:
    for j in G:
        for k in B:
            L = sorted((i,j,k))
            if L[2] == L[1] + L[1] - L[0]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

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

R = [m.span()[0] for m in re.finditer('R', S)]
G = [m.span()[0] for m in re.finditer('G', S)]
B = [m.span()[0] for m in re.finditer('B', S)]

ans = 0

for i in R:
    for j in G:
        for k in B:
            if S[i] == S[j]:
                continue
            if S[i] == S[k]:
                continue
            if S[j] == S[k]:
                continue
            L = sorted([i,j,k])
            if L[1] - L[0] == L[2] - L[1]:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

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

ans = 0
for mr in re.finditer('R', S):
    i = mr.span()[0]
    for mg in re.finditer('G', S):
        j = mg.span()[0]
        for mb in re.finditer('B', S):
            k = mb.span()[0]
            if S[i] == S[j]:
                continue
            if S[i] == S[k]:
                continue
            if S[j] == S[k]:
                continue
            L = sorted([i,j,k])
            if L[1] - L[0] == L[2] - L[1]:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

import collections
c = collections.Counter(s)

ans = c["R"] * c["B"] * c["G"]

for i in range(n-2):
    for j in range(i+1,n-1):
        x = s[i]
        y = s[j]
        
        if x != y:
            if "R" not in [x,y]:
                if 2*j-i < n:
                    if s[2*j-i] == "R":
                        ans -= 1
                    
            elif "G" not in [x,y]:
                if 2*j-i < n:
                    if s[2*j-i] == "G":
                        ans -= 1
                    
            else:
                if 2*j-i < n:
                    if s[2*j-i] == "B":
                        ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

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

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

r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == "R":
        r += 1
        R[i] = r
    elif s[i] == "G":
        g += 1
        G[i] = g
    else:
        b += 1
        B[i] = b

for i in range(n-2):
    for j in range(i+1,n-1):
        x = s[i]
        y = s[j]
        
        if x != y:
            if "R" not in [x,y]:
                ans += R[n-1] - R[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "R":
                        ans -= 1
                    
            if "G" not in [x,y]:
                ans += G[n-1] - G[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "G":
                        ans -= 1
                    
            if "B" not in [x,y]:
                ans += B[n-1] - B[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "B":
                        ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

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

d = dict()
d["R"] = [0 for _ in range(N)]
d["G"] = [0 for _ in range(N)]
d["B"] = [0 for _ in range(N)]

r, g, b = 0, 0, 0
for i in range(N)[::-1]:
    if S[i] == "R":
        r += 1
    if S[i] == "G":
        g += 1
    if S[i] == "B":
        b += 1
    d["R"][i] = r
    d["G"][i] = g
    d["B"][i] = b

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        a = ["R", "G", "B"]
        a.remove(S[i])
        a.remove(S[j])
        x = a[0]
        ans += d[x][j]
        k = j + (j-i)
        if k < N and S[k] == x:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
for i in range(n):
    if s[i]=="R":
        s[i]=1
    elif s[i]=="G":
        s[i]=2
    else:
        s[i]=3
ans=0
for i in range(n-3):
    for j in range(i+1,n-1):
        if s[i]!=s[j]:
            num1=6-(s[i]+s[j])
            num2=j+(j-i)
            for k in range(j+1,n):
                if s[k]==num1 and k!=num2:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
s = 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)

ans = len(r)*len(g)*len(b)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

def f1(v):
    i, j, k = v
    return j - i != k - j


s = b''
def f2(v):
    i, j, k = v
    si = s[i - 1]
    sj = s[j - 1]
    sk = s[k - 1]
    return si != sj and sj != sk and sk != si


def resolve(in_):
    n = int(next(in_))
    global s
    s = next(in_)

    
    it = ((i, j, k) for i in range(1, n + 1) for j in range(i, n + 1) for k in range(j, n + 1))
    it = filter(f1, it)
    it = filter(f2, it)
    ans = sum(1 for _ in it)

    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
s = 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)

l = list(itertools.product(r,g,b))

ans = len(l)
for i in range(len(l)):
    ll = sorted(l[i])
    if ll[1] - ll[0] == ll[2] - ll[1]:
        ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
s = 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)

l = list(itertools.product(r,g,b))

ans = len(l)
for i in range(len(l)):
    ll = sorted(l[i])
    if ll[1] - ll[0] == ll[2] - ll[1]:
        ans -= 1

print(ans)