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 = str(input())
r, g, b = 0, 0, 0
for i in range(n):
    if s[i] == 'R':
        r += 1
    if s[i] == 'G':
        g += 1
    if s[i] == 'B':
        b += 1
ans = r*g*b


def rgb(k, i):
    se = set()
    se.add(s[i])
    se.add(s[i+k])
    se.add(s[i+2*k])
    if len(se) == 3:
        return True
    return False


for k in range(1, n):
    for i in range(n):
        if i+2*k >= n:
            break
        if rgb(k, i):
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if 2*j - i >= n:
            break
        
        if string[i] == string[j]: 
            continue
        rgb = set(['R', 'G', 'B'])
        rgb.remove(string[i])
        rgb.remove(string[j])
        k = rgb.pop()
        if 2*j-i < n and string[2*j-i] == k:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if string[i] == string[j]: 
            continue
        rgb = set(['R', 'G', 'B'])
        rgb.remove(string[i])
        rgb.remove(string[j])
        k = rgb.pop()
        if 2*j-i < n and string[2*j-i] == k:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
string = input()
rs = [0]
gs = [0]
bs = [0]
for idx, c in enumerate(string):
    add = [0]*3
    if c == 'R':
        add[0] += 1
    elif c == 'G':
        add[1] += 1
    else:
        add[2] += 1
    for l, s in zip((rs, gs, bs), add):
        l.append(l[idx]+s)

d = {'R':rs, 'G': gs, 'B': bs}
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if string[i] == string[j]: 
            continue
        rgb = set(['R', 'G', 'B'])
        rgb.remove(string[i])
        rgb.remove(string[j])
        k = rgb.pop()
        cumsum = d[k]
        ans += cumsum[-1] - cumsum[j+1]
        if 2*j-i < n and string[2*j-i] == k:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

res = 0
for x in range(N):
    for y in range(x+1, N):
        for z in range(y+1,N):
            if S[x] != S[y] and S[y] != S[z] and S[x] != S[z] and (y - x != z - y):
                res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S.insert(0,'o')
a=0
for i in range(1,N-2):
    #print(i)
    for j in range(i+1,N):
        #print(j)
        if not S[i]==S[j]:
            for k in range(j+1,N+1):
            #print(k)
                if S[i]!=S[k] and S[j]!=S[k] and j-i!=k-j:
                    a+=1
                    
#print(S)        
print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S.insert(0,'o')
a=0
for i in range(1,N-2):
    #print(i)
    
    for j in range(i+1,N):
     #   print(j)
        if not S[i]==S[j]:
          for k in range(j+1,N+1):
            
            #print(k)
            if S[i]!=S[k] and S[j]!=S[k]:
              if j-i!=k-j:
                
                  
                a+=1
                    
#print(S)        
print(a)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    count = 0
    for j in range(N):
        now_j = S[j]
        for i in range(j):
            now_i = S[i]
            if now_i == now_j:
                continue
            for k in range(j+1, N):
                now_k = S[k]
                if now_k == now_i or now_k == now_j or k - j == j - i:
                    continue
                count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
def main():
    n = I()
    s = list(S())
    ans = 0
    for i in range(n-2):
        color = {s[i]}
        for j in range(i+1,n-1):
            if not s[j] in color:
                color.add(s[j])
                for k in range(j+1,n):
                    if not s[k] in color:
                        if k-j != j-i:
                            ans += 1
                color = {s[i]}
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
n = I()
s = list(S())
 
ans = 0
for i in range(n-2):
    color = {s[i]}
    for j in range(i+1,n-1):
        if not s[j] in color:
            color.add(s[j])
            for k in range(j+1,n):
                if not s[k] in color:
                    if k-j != j-i:
                        ans += 1
            color = {s[i]}
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())

n = I()
s = list(S())

ans = 0
for i in range(n-2):
    color = {s[i]}
    for j in range(i+1,n-1):
        if not s[j] in color:
            color.add(s[j])
            for k in range(j+1,n):
                if not s[k] in color:
                    if i+k-2*j:
                        ans += 1
            color = {s[i]}

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())

n = I()
s = list(S())

ans = 0
for i in range(n-2):
    color = {s[i]}
    for j in range(i+1,n-1):
        if not s[j] in color:
            color.add(s[j])
            for k in range(j+1,n):
                if not s[k] in color:
                    if k-j != j-i:
                        ans += 1
            color = {s[i]}

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())

n = I()
s = list(S())

ans = 0
for i in range(len(s)-2):
    color = {s[i]}
    for j in range(i+1,len(s)-1):
        if not s[j] in color:
            color.add(s[j])
            for k in range(j+1,len(s)):
                if not s[k] in color:
                    if k-j != j-i:
                        ans += 1
            color = {s[i]}

print(ans)

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

n = I()
s = list(S())

ans = 0
for i in range(len(s)-2):
    color = set()
    color.add(s[i])
    for j in range(i+1,len(s)-1):
        if not s[j] in color:
            buff = deepcopy(color)
            buff.add(s[j])
            for k in range(j+1,len(s)):
                if not s[k] in buff:
                    if k-j != j-i:
                        ans += 1

print(ans)

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

n = I()
s = list(S())

s0 = {'R','G','B'}
s1 = deepcopy(s0)
ans = 0
for i in range(len(s)-2):
    s1.remove(s[i])
    for j in range(i+1,len(s)-1):
        s2 = deepcopy(s1)
        if s[j] in s2:
            s2.remove(s[j])
            for k in range(j+1,len(s)):
                if s[k] in s2:
                    if k-j != j-i:
                        ans += 1
    s1 = deepcopy(s0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())

n = I()
s = list(S())
sj = s[1:]

c = 0
for i in s:
    sk = sj[1:]
    if len(sk) <= 0:
        break
    cj = 0
    for j in sj:
        cj += 1
        if len(sk) <= 0:
            break
        if i != j:
            ck = 0
            for k in sk:
                ck += 1
                if i!=k and j!=k and cj != ck:
                    c += 1
        sk.pop(0)
    sj.pop(0)

print(c)

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

n = I()
s = list(S())
sj = deepcopy(s) 

c = 0
for i in s:
    sj.pop(0)
    sk = deepcopy(sj)
    if len(sk) <= 0:
        break
    cj = 0
    for j in sj:
        cj += 1
        sk.pop(0)
        if len(sk) <= 0:
            break
        if i != j:
            ck = 0
            for k in sk:
                ck += 1
                if i!=k and j!=k and cj != ck:
                    c += 1

print(c)

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

n = I()
s = list(S())
sj = deepcopy(s) 

c = 0
for i in s:
    sj.pop(0)
    sk = deepcopy(sj)
    if len(sk) <= 0:
        break
    cj = 0
    for j in sj:
        cj += 1
        sk.pop(0)
        if i != j:
            ck = 0
            for k in sk:
                ck += 1
                if i!=k and j!=k and cj != ck:
                    c += 1

print(c)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

stoi = {'R':0, 'G':1, 'B':2}
itos = {1:'R', 2:'G', 3:'B'}
li = [[0] * N for _ in range(3)]
ans = 0

for ind, s in enumerate(S):
    if(ind == 0):
        li[stoi[s]][ind] = 1
    else:
        for i in range(3):
            li[i][ind] = li[i][ind - 1]
        li[stoi[s]][ind] += 1

for i in range(N):
    for j in range(i + 1, N):
        if(S[i] == S[j]):
            continue
        k = 2 * j - i
        kc = 6 - (stoi[S[i]] + stoi[S[j]] + 2)
        res = li[kc-1][-1] - li[kc-1][j]
        if(k < N and kc == stoi[S[k]] + 1):
            res -= 1
        ans += res

print(ans)

Submission status: Time Limit Exceeded

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

stoi = {'R':0, 'G':1, 'B':2}
itos = {1:'R', 2:'G', 3:'B'}
li = [[0] * 3 for _ in range(N)]
ans = 0

for ind, s in enumerate(S):
    if(ind == 0):
        li[ind][stoi[s]] = 1
    else:
        for i in range(3):
            li[ind][i] = li[ind - 1][i]
        li[ind][stoi[s]] += 1

for i in range(N):
    for j in range(i + 1, N):
        if(S[i] == S[j]):
            continue
        k = 2 * j - i
        kc = 6 - (stoi[S[i]] + stoi[S[j]] + 2)
        res = li[-1][kc-1] - li[j][kc-1]
        if(k<N):
            if(kc == stoi[S[k]] + 1):
                res -= 1
        ans += res

print(ans)

Submission status: Time Limit Exceeded

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

stoi = {'R':0, 'G':1, 'B':2}
itos = {1:'R', 2:'G', 3:'B'}
li = [[0] * 3 for _ in range(N)]
ans = 0

for ind, s in enumerate(S):
    if(ind == 0):
        li[ind][stoi[s]] = 1
    else:
        li[ind] = li[ind - 1].copy()
        li[ind][stoi[s]] += 1

for i in range(N):
    for j in range(i + 1, N):
        if(S[i] == S[j]):
            continue
        k = 2 * j - i
        kc = 6 - (stoi[S[i]] + stoi[S[j]] + 2)
        res = li[-1][kc-1] - li[j][kc-1]
        if(k<N):
            if(kc == stoi[S[k]] + 1):
                res -= 1
        ans += res

print(ans)

Submission status: Time Limit Exceeded

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

##変数名 for 変数名 in 元のリスト if 条件式
##enumerate(文字列 or リスト, インデックスの始まりの数字)
##リストや文字列のインデックスと要素を同時に取得 i=インデックス, x=要素
##ここではx=要素を条件式で指定しているので、そのインデックスのみが取得できる
r = list([i for i ,x in enumerate(s,1) if x == "R"])
g = list([i for i ,x in enumerate(s,1) if x == "G"])
b = list([i for i ,x in enumerate(s,1) if x == "B"])
all = len(r) * len(g) * len(b)

##j,bの平均がrに含まれている数をカウント
count_r = 0
for i in range(len(g)):
    for j in range(len(b)):
        if (g[i] + b[j]) / 2 in r:
            count_r +=  1

count_g = 0
for i in range(len(r)):
    for j in range(len(b)):
        if (r[i] + b[j]) / 2 in g:
            count_g += 1

count_b = 0
for i in range(len(r)):
    for j in range(len(g)):
        if (r[i] + g[j]) / 2 in b:
            count_b += 1

print(all - (count_r + count_g + count_b))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
color = [[], [], []]
for i in range(N):
    if S[i] == 'R':
        color[0].append(i)
    elif S[i] == 'G':
        color[1].append(i)
    elif S[i] == 'B':
        color[2].append(i)
rn = len(color[0])
gn = len(color[1])
bn = len(color[2])
mi = 0
for i in color[0]:
    for j in color[1]:
        for k in color[2]:
            if k == 2*j-i:
                mi += 1
            if k == (j-i)/2+i:
                mi += 1
            if k == 2*i-j:
                mi += 1
print(rn*gn*bn-mi)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
color = [[], [], []]
for i in range(N):
    if S[i] == 'R':
        color[0].append(i)
    elif S[i] == 'G':
        color[1].append(i)
    elif S[i] == 'B':
        color[2].append(i)
rn = len(color[0])
gn = len(color[1])
bn = len(color[2])
mi = 0
for i in color[0]:
    for j in color[1]:
        if 2*j-i in color[2]:
            mi += 1
        if (j-i)/2+i in color[2]:
            mi += 1
        if 2*i-j in color[2]:
            mi += 1
print(rn*gn*bn-mi)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s = list(input())
rgb = {'R','G','B'}
ans = 0

for i in range(n):
    for j in range(n-i-2):
        h = i+j+1
        if s[i] == s[h]:
            continue
        else:
            litl = list(rgb-{s[i]}-{s[h]})
            lit = litl[0]
            if h+h-i <= n-1:
                if s[h+h-i] != lit:
                    ans += s[h+1:].count(lit)
                    
                else:
                    ans += s[h+1:].count(lit)-1
            
            else:
                ans += s[h+1:].count(lit)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s = list(input())
rgb = {'R','G','B'}
ans = 0

for i in range(n):
    for j in range(n-i-2):
        h = i+j+1
        if s[i] == s[h]:
            continue
        else:
            litl = list(rgb-{s[i]}-{s[h]})
            lit = litl[0]
            if h+h-i <= n-1:
                if s[h+h-i] != lit:
                    ans += s[h+1:].count(lit)
                    
                else:
                    ans += s[h+1:].count(lit)-1
            
            else:
                ans += s[h+1:].count(lit)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
cnt = 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):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dict={}
dict["R"]=[]
dict["G"]=[]
dict["B"]=[]
res=0
for i in range(n):
  if s[i]=="R":
    dict["R"].append(i)
  elif s[i]=="G":
    dict["G"].append(i)
  elif s[i]=="B":
    dict["B"].append(i)
  else:
    raise Exception
for r in dict["R"]:
  for g in dict["G"]:
      for b in dict["B"]:
        if 2*r!=g+b and 2*g!=r+b and 2*b!=r+g:
          res+=1           
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dict={}
dict["R"]=[]
dict["G"]=[]
dict["B"]=[]
res=set()
for i in range(n):
  if s[i]=="R":
    dict["R"].append(i)
  elif s[i]=="G":
    dict["G"].append(i)
  elif s[i]=="B":
    dict["B"].append(i)
  else:
    raise Exception
for r in dict["R"]:
  for g in dict["G"]:
      for b in dict["B"]:
        if 2*r!=g+b and 2*g!=r+b and 2*b!=r+g:
            tup=frozenset([r,g,b])
            res.add(tup)             
print(len(res))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dict={}
dict["R"]=[]
dict["G"]=[]
dict["B"]=[]
res=set()
for i in range(n):
  if s[i]=="R":
    dict["R"].append(i)
  elif s[i]=="G":
    dict["G"].append(i)
  elif s[i]=="B":
    dict["B"].append(i)
  else:
    raise Exception
#dict["R"]=sorted(dict["R"])
#dict["G"]=sorted(dict["G"])
#dict["B"]=sorted(dict["B"])
#print(dict["B"])
for r in dict["R"]:
  for g in dict["G"]:
      for b in dict["B"]:
        tup=tuple(sorted((r,g,b)))
        if 2*tup[1]!=tup[0]+tup[2]:
            res.add(tup)             
print(len(res))

Submission status: Time Limit Exceeded

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

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

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

sum = 0


for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            index = [r[i],g[j],b[k]]
            index.sort()
            if index[1] - index[0] != index[2] - index[1]:
                sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
total = 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:
                total = total + 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial as f
N=int(input())
S=input()

s=list(S)
D={}
for i in range(N):
    D[i]=s[i]

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)

total=0
for j in R:
    for k in G:       
        if (2*k-j in B)==True:
            total+=1
        if (2*j-k in B)==True:
            total+=1
        if ((j+k)/2 in B)==True:
            total+=1

print(len(R)*len(G)*len(B)-total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = list([i for i ,x in enumerate(s,1) if x == "R"])
g = list([i for i ,x in enumerate(s,1) if x == "G"])
b = list([i for i ,x in enumerate(s,1) if x == "B"])
all = len(r) * len(g) * len(b)


count_r = 0
for i in range(len(g)):
    for j in range(len(b)):
        if (g[i] + b[j]) / 2 in r:
            count_r = count_r + 1

count_g = 0
for i in range(len(r)):
    for j in range(len(b)):
        if (r[i] + b[j]) / 2 in g:
            count_g = count_g + 1

count_b = 0
for i in range(len(r)):
    for j in range(len(g)):
        if (r[i] + g[j]) / 2 in b:
            count_b = count_b + 1

print(all - (count_r + count_g + count_b))

Submission status: Time Limit Exceeded

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

for j in range(N):
  for k in range(N):
    i=2*j-k
    if 0<=i<j<k<=N-1 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())
s = input()

#s = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
#n = len(s)
s = list(s)
count = 0
for i in range(n - 2):
    set_j_k = set(['R', 'G', 'B']) - set(s[i])
    for j in range(i + 1, n - 1):
        if s[j] != s[i]:
            j_i = j - i
            set_k = set_j_k - set(s[j])
            char_k = list(set_k)[0]
            # print(set_k)
            for k in range(j + 1, n):
                if s[k] in set_k and j_i != k - j:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

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

S = [0] * (2 * N + 1)
for i in range(N):
    if SS[i] == "R":
        S[i] = 1

    if SS[i] == "G":
        S[i] = 2

    if SS[i] == "B":
        S[i] = 4

cnt = [[0] * 3 for i in range(N)]
if S[0] == 1:
    cnt[0][0] = 1
elif S[0] == 2:
    cnt[0][1] = 1
else:
    cnt[0][2] = 1


for i in range(1, N):
    cnt[i][0] = cnt[i - 1][0]
    cnt[i][1] = cnt[i - 1][1]
    cnt[i][2] = cnt[i - 1][2]
    if S[i] == 1:
        cnt[i][0] += 1

    if S[i] == 2:
        cnt[i][1] += 1

    if S[i] == 4:
        cnt[i][2] += 1


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

            if to == 3:
                ans += cnt[N - 1][2] - cnt[j][2]
                if S[j + j - i] == 4:
                    ans -= 1

            if to == 5:
                ans += cnt[N - 1][1] - cnt[j][1]
                if S[j + j - i] == 2:
                    ans -= 1

            if to == 6:
                ans += cnt[N - 1][0] - cnt[j][0]
                if S[j + j - i] == 1:
                    ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = s.count("R") * s.count("G") * s.count("B")
for i in range(1, n-1):
  for j in range(i+1, n):
    if(2*j-i-1 <n):
      if(len({s[i-1], s[j-1], s[2*j-i-1]}) == 3):
        sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = s.count("R") * s.count("G") * s.count("B")
for i in range(1, n-1):
  for j in range(i+1, n):
    if(s[i-1] == s[j-1] or 2*j-i-1 >= n):
      continue
    k = {"R", "G", "B"} - {s[i-1], s[j-1]}
    k = k.pop()
    if(s[2*j-i-1] == k):
      sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i in range(1, n-1):
  h = {"R", "G", "B"} - {s[i-1]}
  sum += s[i:].count(h.pop()) * s[i:].count(h.pop())
  for j in range(i+1, n):
    if(s[i-1] == s[j-1]):
      continue
    k = {"R", "G", "B"} - {s[i-1], s[j-1]}
    k = k.pop()
    if(2*j-i-1 < n):
      if(s[2*j-i-1] == k):
        sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i in range(1, n-1):
  for j in range(i+1, n):
    if(s[i-1] == s[j-1]):
      continue
    k = {"R", "G", "B"} - {s[i-1], s[j-1]}
    k = k.pop()
    sum += s[j:].count(k)
    if(2*j-i-1 < n):
      if(s[2*j-i-1] == k):
        sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
s = ns()

ans = s.count('R') * s.count('G') * s.count('B')

for i in range(10000):
    if 2 + 2 * i >= n: break
    for j in range(10000):
        if 2 + 2 * i + j >= n: break
        if not(s[j] == s[i+1+j] or s[i+1+j] == s[2*i+2+j] or s[2*i+2+j] == s[j]):
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
from collections import deque
C =[deque([]),deque([]),deque([])]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = 0
l = len(C[2])
for i in C[0]:
    for j in C[1]:
        count += l
        if max(i,j) + abs(j-i) in C[2]:
            count -= 1
        if min(i,j) - abs(j-i) in C[2]:
            count -= 1
        if (i + j)%2 == 0 and (i + j)//2 in C[2]:
            count -= 1


print(count)

Submission status: Time Limit Exceeded

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

C =[[],[],[]]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = 0
for i in C[0]:
    for j in C[1]:
        count += len(C[2])
        if max(i,j) + abs(j-i) in C[2]:
            count -= 1
        if min(i,j) - abs(j-i) in C[2]:
            count -= 1
        if (i + j)%2 == 0 and (i + j)//2 in C[2]:
            count -= 1
        

print(count)

Submission status: Time Limit Exceeded

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

C =[[],[],[]]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = 0
for i in C[0]:
    for j in C[1]:
        for k in C[2]:
            ls = [i,j,k]
            ls.sort()
            if ls[1]-ls[0] != ls[2]-ls[1]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

red = 0
r,g,b = 0,0,0
for i in range(N):
  if(S[i] == 'R'): 
    r += 1
    # i     j    j+(j-i) : R  G  B  (or)  R  B  G (or) ... である場合 ans -= 1   
    for j in range(i+1,N-1):
     i1,i2,i3 = i,j,j+(j-i)
     if(i3 <= N-1):
       tmp = S[i1]+S[i2]+S[i3]
       if((tmp == 'RBG')or(tmp == 'RGB')):
         red += 1

  elif(S[i] == 'G'): 
     g += 1
     # i     j    j+(j-i) : R  G  B  (or)  R  B  G (or) ... である場合 ans -= 1   
     for j in range(i+1,N-1):
       i1,i2,i3 = i,j,j+(j-i)
       if(i3 <= N-1):
         tmp = S[i1]+S[i2]+S[i3]
         if((tmp == 'GBR')or(tmp == 'GRB')):
           red += 1
        
  elif(S[i] == 'B'): 
    b += 1 
    for j in range(i+1,N-1):
     i1,i2,i3 = i,j,j+(j-i)
     if(i3 <= N-1):
       tmp = S[i1]+S[i2]+S[i3]
       if((tmp == 'BGR')or(tmp == 'BRG')):
         red += 1
        
ans = r*g*b - red
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
r,g,b = 0,0,0
for i in range(N):
  if(S[i] == 'R'): r += 1
  elif(S[i] == 'G'): g += 1  
  elif(S[i] == 'B'): b += 1 
  
  # i     j    j+(j-i) : R  G  B  (or)  R  B  G (or) ... である場合 ans -= 1   
  for j in range(i+1,N-1):
     i1,i2,i3 = i,j,j+(j-i)
     if(i3 <= N-1):
       tmp = [S[i1],S[i2],S[i3]]
       tmp.sort()
       tmp = tmp[0]+tmp[1]+tmp[2]
       if(tmp == 'BGR'):
         ans -= 1

ans += r*g*b
print(ans)

Submission status: Time Limit Exceeded

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

r, g, b = set(), set(), set()
for i in range(len(s)):
    if s[i] == "R":
        r.add(i)
    elif s[i] == "G":
        g.add(i)
    else:
        b.add(i)
        
ans = len(r) * len(g) * len(b)
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in set([j + diff_ji, i - diff_ji, (j + i) / 2]):
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)

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)
        
ans = len(r) * len(g) * len(b)
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in [j + diff_ji, i - diff_ji, (j + i) / 2]:
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)

Submission status: Time Limit Exceeded

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

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 j-i == k-j:
        pass
      else:
        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=input()

R=S.count("R")
G=S.count("G")
B=S.count("B")

ans=R*G*B
if ans==0:
    print(0)
    exit()
    
    
for i in range(1,N-1):
    for j in range(i+1,N):
        if S[i-1]==S[j-1]:continue
        n=2*j-i
        if n>=N+1:continue
        if S[i-1]!=S[n-1] and S[j-1]!=S[n-1]:
            ans-=1

print(ans)

Submission status: Time Limit Exceeded

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

R=S.count("R")
G=S.count("G")
B=S.count("B")

ans=R*G*B
for i in range(1,N-1):
    for j in range(i+1,N):
        if S[i-1]==S[j-1]:continue
        n=2*j-i
        if n>=N+1:continue
        if S[i-1]!=S[n-1] and S[j-1]!=S[n-1]:
            ans-=1

print(ans)

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)

if len(Lis[0])==0 or len(Lis[1])==0 or len(Lis[2])==0:
    print(0)
    exit()

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 N>=n1>0:
            if n1 in Lis[2]:
                Su-=1
        if N>=n2>0:
            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:
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)

if len(Lis[0])==0 or len(Lis[1])==0 or len(Lis[2])==0:
    print(0)
    exit()

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>0:
            if n1 in Lis[2]:
                Su-=1
        if n2>0:
            if n2 in Lis[2]:
                Su-=1
        if (g-r)%2==0:
            if n3 in Lis[2]:
                Su-=1
        ans+=Su
print(ans)