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()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    ######### Index
    mix = []
    for i in range(len(S)):
        for j in range(i+1, len(S)):
            for k in range(j+1, len(S)):
                s = ''.join(sorted([S[i], S[j], S[k]]))
                if s == 'BGR':
                    mix.append([i, j, k])
    
    C = 0
    out = 0

    
    # print('----------------')
    for i, j, k in mix:
        # if S[i] != 'R' or S[j] != 'G' or S[k] != 'B':
        # print('aaa:', S[i], S[j], S[k])
        
        if abs(j - i) != abs(k - j):
            if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
            
                
                out += 1
        # else:
        #     print('out:', i, j, k, S[i], S[j], S[k])


    print(out)


    
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
for k in range(N):
    for j in range(k):
        for i in range(j):
            if S[k] != S[j] and S[j] != S[i] and S[k] != S[i] and j - i != k - j:
                cnt = cnt + 1
            else:
                cnt = cnt + 0

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def check(pos, c):
    if pos < 0 or pos >= len(S):
        return False
    return S[pos] == c
N = int(input())
S = input()
R_pos = [i for i, c in enumerate(S) if c == 'R']
G_pos = [i for i, c in enumerate(S) if c == 'G']
B_pos = [i for i, c in enumerate(S) if c == 'B']
B_l = len(B_pos)
res = 0
for i in R_pos:
    for j in G_pos:
        res += B_l
        dis = abs(i - j)
        res -= check(min(i, j) - dis, 'B')
        res -= check(max(i, j) + dis, 'B')
        if dis % 2 == 0:
            res -= check(max(i, j) - dis // 2, 'B')
print(res)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right,bisect_left
N = int(input())
S = input().strip()
R = []
G = []
B = []
for i in range(N):
    if S[i]=="R":
        R.append(i)
    elif S[i]=="G":
        G.append(i)
    else:
        B.append(i)
cnt = 0
for i in range(N):
    if i in R:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(B,indg)
            for k in range(ind1,len(B)):
                if indg-indr!=B[k]-indg:
                    cnt += 1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(G,indb)
            for k in range(ind1,len(G)):
                if indb-indr!=G[k]-indb:
                    cnt += 1
    elif i in G:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(R,indb)
            for k in range(ind1,len(R)):
                if indb-indg!=R[k]-indb:
                    cnt += 1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(B,indr)
            for k in range(ind1,len(B)):
                if indr-indg!=B[k]-indr:
                    cnt += 1
    elif i in B:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(G,indr)
            for k in range(ind1,len(G)):
                if indr-indb!=G[k]-indr:
                    cnt += 1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(R,indg)
            for k in range(ind1,len(R)):
                if indg-indb!=R[k]-indg:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets
import heapq

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

R = [i for i, x in enumerate(s) if x=='R']
G = [i for i, x in enumerate(s) if x=='G']
B = [i for i, x in enumerate(s) if x=='B']

ans = 0

for r in R:
    for g in G:
        for b in B:
            mi = min(r, g, b)
            ma = max(r, g, b)
            if (r+g+b)==mi+ma+(mi+ma)/2:
                continue
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
sn = len(s)

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)

for i in range(1, sn):
    if s[i] == "R":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)

for i in range(1, sn):
    if s[i] == "G":
        g.append(g[-1] + 1)
    else:
        g.append(g[-1])

b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

for i in range(1, sn):
    if s[i] == "B":
        b.append(b[-1] + 1)
    else:
        b.append(b[-1])

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

            if "B" != s[i] and "B" != s[j]:
                ans += b[-1] - b[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "B":
                    ans -= 1
            elif "G" != s[i] and "G" != s[j]:
                ans += g[-1] - g[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += r[-1] - r[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "R":
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

if __name__ == "__main__":
	N = int(input())
	S = tuple(input())

	sum = 0

	for c in combinations(range(N),3):
		S_i = S[c[0]]; S_j = S[c[1]]; S_k = S[c[2]]
		if (c[1] - c[0]) != (c[2] - c[1]):
			if (S_i != S_j) and (S_i != S_k) and (S_j != S_k):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = 0
    r_cnt = s.count('R')
    g_cnt = s.count('G')
    g_cnt = s.count('G')

    for i in range(n):
        for j in range(1, n - i, 1):
            if s[i] != s[i+j]:
                for x in ('R', 'G', 'B'):
                    if x not in (s[i], s[i+j]):
                        tmp = x
                if i+2*j < n and s[i+2*j] == tmp:
                    ans += s[i+j+1:].count(tmp) - 1
                else:
                    ans += s[i+j+1:].count(tmp)

    print(ans)


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 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__":
    #unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
Rlist = []
Glist = []
Blist = []
for x in range(len(s)):
    if s[x] == "R": Rlist.append(x)
    elif s[x] == "G": Glist.append(x)
    elif s[x] == "B": Blist.append(x)

def Cmin(r, g):
    result = -1
    if g < r: result = Cmin(g, r)
    else: result = r - (g - r)
    return result
    
def Cmid(r, g):
    result = -1
    if g < r: result = Cmid(g, r)
    elif (g - r) % 2 != 0: result = -1
    else: result = r + (g - r) // 2
    return result

def Cmax(r, g):
    result = -1
    if g < r: result = Cmax(g, r)
    else: result = g + (g - r)
    return result

def Calc(AList, BList, CList):
    ans = 0
    for r in AList:
        for g in BList:
            ans += len(CList)
            if Cmin(r, g) in CList: ans -= 1
            if Cmid(r, g) in CList: ans -= 1
            if Cmax(r, g) in CList: ans -= 1
    return ans

r = len(Rlist)
g = len(Glist)
b = len(Blist)
ans = 0
if r  <= g  <= b: ans = Calc(Rlist, Glist, Blist)
elif r  <= b  <= g: ans = Calc(Rlist, Blist, Glist)
elif g  <= r  <= b: ans = Calc(Glist, Rlist, Blist)
elif g  <= b  <= r: ans = Calc(Glist, Blist, Rlist)
elif b  <= r  <= g: ans = Calc(Blist, Rlist, Glist)
elif b  <= g  <= r: ans = Calc(Blist, Glist, Rlist)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

def main():
    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 (len(set([S[i],S[j],S[k]]))==3) 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()
sums = 0

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

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


for k in r:
    for p in g:
        for l in b:
            if abs(k-p) == abs(k-l) or abs(p-k) == abs(p-l) or abs(l-k) == abs(l-p):
                sums -=1

sums +=len(r)*len(g)*len(b)
print(sums)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = input()
    ans = 0

    for i in range(n):
        for j in range(i + 1, n):
            if s[i] != s[j]:
                for k in range(j + 1, n): 
                    if s[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=list(input())
from collections import Counter
myc= Counter(s)
val=list(myc.values())
if len(val)!=3:
    print(0)
else:
    ans=val[0]*val[1]*val[2]
    for i in range(n-2):
        for j in range(i+1,n-1):
            if 2*j-i < n:
                if len(set(list((s[i],s[j],s[2*j-i]))))==3:
                    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):
        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=list(input())
count=0
for k in range(1,n+1):
    for j in range(1,k):
        if s[k-1]==s[j-1]:
            continue
        for i in range(1,j):
            if s[k-1]==s[i-1] or s[j-1]==s[i-1] or j-i==k-j:
                continue
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
 
R = []
G = []
B = []
 
for index, color in enumerate(S):
  if color == "R":
    R.append(index)
  if color == "G":
    G.append(index)
  if color == "B":
    B.append(index)
 
result = 0
for r_index in R:
  for g_index in G:
    for b_index in B:
      sorted_index = sorted([r_index, g_index, b_index])
 
      if(sorted_index[1] - sorted_index[0] != sorted_index[2] - sorted_index[1]):
        result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter, deque

N = int(input())
S = deque(input())
s_c = Counter(S)

a = s_c["R"] * s_c["B"] * s_c["G"]

for i in range(N):
    for j in range(i + 1, N):
        for _ in range(j + 1, N):
            if (S[i] != S[j] and S[i] != S[_] and S[j] != S[_]) and \
                not (j - i != _ - j):
                a -= 1
print(a)

Submission status: Time Limit Exceeded

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


A=list(itertools.combinations_with_replacement(range(n), 3))

ans=0
for a,b,c in A:
    if S[a]!=S[b] and S[a]!=S[c] and S[b]!=S[c] and (b-a)!=(c-b):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    s = 0
    Rl = [i for i in range(N) if S[i] == "R"]
    Gl = [i for i in range(N) if S[i] == "G"]
    Bl = [i for i in range(N) if S[i] == "B"]
    for i in Rl:
        for j in Gl:
            if j <= i:
                continue
            elif j > i:
                for k in Bl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1
        for j in Bl:
            if j <= i:
                continue
            elif j > i:
                for k in Gl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1

    for i in Gl:
        for j in Bl:
            if j <= i:
                continue
            elif j > i:
                for k in Rl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1
        for j in Rl:
            if j <= i:
                continue
            elif j > i:
                for k in Bl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1

    for i in Bl:
        for j in Rl:
            if j <= i:
                continue
            elif j > i:
                for k in Gl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1
        for j in Gl:
            if j <= i:
                continue
            elif j > i:
                for k in Rl:
                    if k <= j:
                        continue
                    elif k > j:
                        if k-j != j-i:
                            s += 1


    print(s)

if __name__ == "__main__":
    main()

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):
    for k in range(j,n):
      if 2*j!=i+k and 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())
 
str = input()
sum = 0
ans = []
for i in range(n):
  if str[i] == "R":
    ans.append(2)
  elif str[i] == "G":
    ans.append(3)
  else:
    ans.append(5)

for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      if ans[i] - ans[j] == 0:
        break
      elif j - i == k - j:
        pass
      elif ans[i] + ans[j] + ans[k] == 10:
        sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(n):
    a = s[i]
    for j in range(i,n):
        if s[j] != a:
            b = s[j]
            for k in range(j,n):
                if s[k]!=a and s[k]!=b:
                    if j-i!=k-j:
                        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:
def main():
    n=int(input())
    s=input()
    r,g,b=[],[],[]
    if n<=3:
        print(0)
    else:
        for i in range(n):
            if s[i]=="R":
                r.append(i)
            elif s[i]=="G":
                g.append(i)
            else:
                b.append(i)
        ans=len(r)*len(g)*len(b)
        for i in range(len(r)):
            for j in range(len(g)):
                if (r[i]+g[j])%2==0 and (r[i]+g[j])//2 in b:
                        ans-=1
        for i in range(len(g)):
            for j in range(len(b)):
                if (g[i]+b[j])%2==0 and (g[i]+b[j])//2 in r:
                        ans-=1
        for i in range(len(r)):
            for j in range(len(b)):
                if (r[i]+b[j])%2==0 and (r[i]+b[j])//2 in g:
                        ans-=1
        print(ans)
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
dic = {
  "R":[],
  "G":[],
  "B":[]
}
for i in range(N):
  dic[S[i]].append(i)

res = 0
for a in range(len(dic["R"])):
  for b in range(len(dic["G"])):
    for c in range(len(dic["B"])):
      x1 = dic["R"][a]
      x2 = dic["G"][b]
      x3 = dic["B"][c]
      xsum = x1 + x2 + x3
      if xsum%3!=0:
        res += 1
        continue
      ave = int(xsum/3)
      if x1!=ave and x2!=ave and x3!=ave:
        res += 1
        continue
        
print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
s=input()
r=[]
g=[]
t=[]
for i in range(n):
    if s[i:i+1]=="R":
        r.append(i)
    elif s[i:i+1]=="G":
        g.append(i)
    else:
        t.append(i)
R=len(r)
G=len(g)
T=n-R-G
ans=R*G*T
#print(r)
#print(g)
#print(t)
for a in r:
    for b in g:
        c=collections.Counter(t)
        ans-=c[2*b-a]
        ans-=c[2*a-b]
        if (a+b)%2==0:
            ans-=c[(a+b)//2]
        '''if 2*b-a in t:
            ans-=1
        if 2*a-b in t:
            ans-=1
        if (a+b)%2==0 and (a+b)//2 in t:
            ans-=1'''
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str,list(input())))
c = 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] :
                    if  S[j] != S[k] and ( j-i != k-j) :
                        c += 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    S = input()
    r_inds = []
    g_inds = []
    b_inds = []
    for i in range(len(S)):
        if S[i] == "R":
            r_inds.append(i)
        elif S[i] == "G":
            g_inds.append(i)
        else:
            b_inds.append(i)

    total_comb = len(r_inds)*len(g_inds)*len(b_inds)
    ded = 0
    for i in r_inds:
        for j in g_inds:
            s_ind = min(i, j)
            b_ind = max(i, j)
            diff = b_ind - s_ind
            bigger_ind = b_ind + diff
            smaller_ind = s_ind - diff
            if bigger_ind < len(S) and bigger_ind in b_inds:
                ded += 1
            if smaller_ind >= 0 and smaller_ind in b_inds:
                ded += 1
            if diff % 2 == 0:
                mid_diff = diff // 2
                mid_ind = s_ind + mid_diff
                if mid_ind in b_inds:
                    ded += 1
    print(total_comb - ded)

            
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

L=['R','G','B']
L_b=['R','G','B']
cnt=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]!=S[j]:
            L_b=['R','G','B']
            L_b.remove(S[i])
            L_b.remove(S[j])
            index = S.rfind(L_b[0])
            for k in range(j+1,index+1):
                if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]  and j-i!=k-j:
                    cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

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

R = []
G = []
B = []
ans = []

if n < 4:
  print(0)
  exit(0)

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

if R == [] or G == [] or B == []:
  print(0)
  exit(0)

for r in R:
  for g in G:
    for b in B:
      ijk = sorted([r, g, b])
      if ijk[1] - ijk[0] != ijk[2] - ijk[1]:
        ans.append(ijk)

print(len(get_unique_list(ans)))

Submission status: Time Limit Exceeded

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s):
      if bi < j:
        continue
      for ci, c in enumerate(s):
        if ci < k:
          continue
        if i > j:
          continue
        if j > k:
          continue
        if i < bi < ci:
          if a == b or b == c or a == c:
            continue
          if bi - i == ci - bi:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=0
if n<=3:
    print(0)
    quit()
for i in range(n-2):
    si=s[i]
    for j in range(i+1,n-1):
        sj=s[j]
        if si!=sj:
            for k in range(j+1,n):
                sk=s[k]
                if sk!=si and sk!=sj and j-i!=k-j:
                    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

res = 0
for j in range(N):
    rgb = ['R', 'G', 'B']
    second = S[j]
    rgb.remove(second)
    rest_1 = rgb.pop()
    rest_2 = rgb.pop()

    first_1, first_2 = [], []
    for i in range(j):
        if S[i] == second:
            continue
        if S[i] == rest_1:
            first_1.append([rest_1, i])
        if S[i] == rest_2:
            first_2.append([rest_2, i])

    last_1, last_2 = [], []
    for k in range(j + 1, N):
        if S[k] == second:
            continue
        if S[k] == rest_1:
            last_2.append([rest_1, k])
        if S[k] == rest_2:
            last_1.append([rest_2, k])

    for first, i in first_1:
        for last, k in last_1:
            if j - i != k - j:
                # print(f"i = {i}, j = {j}, k = {k}")
                # print(f"first = {first}, second = {second}, last = {last}")
                if i < j < k:
                    res += 1
    for first, i in first_2:
        for last, k in last_2:
            if j - i != k - j:
                # print(f"i = {i}, j = {j}, k = {k}")
                # print(f"first = {first}, second = {second}, last = {last}")
                if i < j < k:
                    res += 1

print(res)

Submission status: Time Limit Exceeded

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

count=0

for index1, val1 in enumerate(S):
  for index2, val2 in enumerate(S[index1+1:]):
    if val1 != val2:
      for index3, val3 in enumerate(S[index1+index2+2:]):
        if val3 not in [val1, val2] and index2 != index3:
          count+=1
          
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import collections
N=int(input())
Tr=list(input())
R=[]
G=[]
B=[]
for i in range(N):
   if Tr[i]=="R":
       R.append(i)
   if Tr[i]=="G":
       G.append(i)
   if Tr[i]=="B":
       B.append(i)
cnt=0
for i in R:
    for j in G:
        for k in B:
            if abs(i-j)!=abs(j-k) and abs(j-k)!=abs(k-i) and abs(j-i)!=abs(i-k):
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(n-2):
    for j in range(1,(n-i)//2 + 1):
        if i+2*j <= n-1:
            tem = s[i]+s[i+j]+s[i+2*j]
            if tem[0] != tem[1] and tem[0] != tem[2] and tem[1] != tem[2]:
                minus += 1
            

print(sum - minus)

Submission status: Time Limit Exceeded

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    center = j - i
    if n < j + 1 + center:
        center = 0

    for k in range(j+1+center, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1+center)] = total

    for k in range(j+1, j+center):
        if a != s[k] and b != s[k]:
            total += 1
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        center = j - i
        if n < j + 1 + center:
            if a+str(i) in check:
                total += check[a+str(i)]
                break
        if a != s[j]:
            total += get_pattern2(i, j)
    check[a+str(i)] = total
    return total

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if len(set([S[i], S[j]])) == 2:
            sk = ({"R", "G", "B"} - set([S[i], S[j]])).pop()
            if 2*j - i < N and S[2*j - i] == sk:
                ans += Counter(S[j+1:])[sk] - 1
            else:
                ans += Counter(S[j+1:])[sk]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
c=[[0,0,0] for _ in range(n+1)]
R=G=B=0
for j,i in enumerate(s):
    if i=="R":
        R+=1
    elif i=="G":
        G+=1
    else:B+=1
    c[j+1]=[R,G,B]
a=0
for i,m in enumerate(s):
    for j in range(i):
        if m!=s[j]:
            k=3-"RGB".index(m)-"RGB".index(s[j])
            a+=c[-1][k]-c[i][k]
            if 2*i-j<=n-1:
                if s[2*i-j]=="RGB"[k]:
                    a-=1
print(a)