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 = list(input())

R = []
G = []
B = []
r = 0
g = 0
b = 0
for i, s in enumerate(S):
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1
    R.append(r)
    G.append(g)
    B.append(b)

ans = 0
for i in range(N-1):
    if S[i] == "R":
        for j in range(i+1, N):
            #print("i", i, "j", j, ans)
            #print(S[i], S[j])
            if S[j] == "G":
                if 2*j - i < N and S[2*j - i] == "B":
                    ans += (B[N-1] - B[j] - 1)
                else:
                    ans += (B[N-1] - B[j])
            if S[j] == "B":
                if 2*j - i < N and S[2*j - i] == "G":
                    ans += (G[N-1] - G[j] - 1)
                else:
                    ans += (G[N-1] - G[j])
    if S[i] == "G":
        for j in range(i, N):
            if S[j] == "R":
                if 2*j - i < N and S[2*j - i] == "B":
                    ans += (B[N-1] - B[j] - 1)
                else:
                    ans += (B[N-1] - B[j])
            if S[j] == "B":
                if 2*j - i < N and S[2*j - i] == "R":
                    ans += (R[N-1] - R[j] - 1)
                else:
                    ans += (R[N-1] - R[j])
    if S[i] == "B":
        for j in range(i, N):
            if S[j] == "G":
                if 2*j - i < N and S[2*j - i] == "R":
                    ans += (R[N-1] - R[j] - 1)
                else:
                    ans += (R[N-1] - R[j])
            if S[j] == "R":
                if 2*j - i < N and S[2*j - i] == "G":
                    ans += (G[N-1] - G[j] - 1)
                else:
                    ans += (G[N-1] - G[j])
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for one in kv['R']:
    for t in kv['G']:
        tmp = len(kv['B'])
        if 2*t-one in kv['B']:
            tmp-=1
        if 2*one-t in kv['B']:
            tmp-=1
        if (t+one)/2 in kv['B']:
            tmp-=1
        ans+=tmp
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]:
            continue
        for k in range(j+1, N):
            if S[k] not in [S[i], S[j]] and j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(sys.stdin.readline())
s = sys.stdin.readline()
r = []
g = []
b = []
cnt = 0
i = 0
while i < n:
  if s[i] == "R":
    r.append(i)
  elif s[i] == "G":
    g.append(i)
  else:
    b.append(i)
for j in r:
  for k in g:
    for l in b:
      if l - k != k - j:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    res = 0
    r = []
    g = []
    b = []
    for i, c in enumerate(s):
        if c == 'R':
            r.append(i)
        elif c == 'G':
            g.append(i)
        else:
            b.append(i)
    
    cnt = 0
    for i in r:
        for j in g:
            diff = abs(j-i)
            for k in b:
                if abs(i-k) != diff and abs(j-k) != diff and abs(j-k) != abs(i-k):
                    cnt += 1
    print(cnt)
                    


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
S= [s for s in input()]
correct = ["RGB","RBG","GBR","GRB","BRG","BGR"]

cnt = 0
for i in range(N):
	for j in range(i+1,N):
		for k in range(j+1,N):
			if (j-i)!=(k-j):
				if "%s%s%s"%(S[i],S[j],S[k]) in correct:
					cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
R = []
G = []
B = []

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

res = 0
for r in R:
    for g in G:
        for b in B:
            lis = [r,g,b]
            lis.sort()
            if lis[0] + lis[2] != lis[1] * 2:
                res += 1
print(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)
    elif S[i] == 'B':
        B.append(i)
ans = 0
for i in R:
    for j in G:
        for k in B:
            if i < j < k and j-i != k-j:
                ans += 1
for i in R:
    for j in B:
        for k in G:
            if i < j < k and j-i != k-j:
                ans += 1
for i in G:
    for j in R:
        for k in B:
            if i < j < k and j-i != k-j:
                ans += 1
for i in G:
    for j in B:
        for k in R:
            if i < j < k and j-i != k-j:
                ans += 1
for i in B:
    for j in G:
        for k in R:
            if i < j < k and j-i != k-j:
                ans += 1
for i in B:
    for j in R:
        for k in G:
            if i < j < k and j-i != k-j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

print(z)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

    print(ans)


main()

Submission status: Time Limit Exceeded

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

R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = 0
for i in range(N):
    if S[i] == 'R':
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans += sum(x > j for x in B_index) -1
            else:
                ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans += sum(x > j for x in G_index) -1
            else:
                ans += sum(x > j for x in G_index)
    elif S[i] == 'G':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in B_index:
                ans += sum(x > j for x in B_index) -1
            else:
                ans += sum(x > j for x in B_index) 
        for j in B_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans += sum(x > j for x in R_index) -1
            else:
                ans += sum(x > j for x in R_index)
    elif S[i] == 'B':
        for j in R_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in G_index:
                ans += sum(x > j for x in G_index) -1
            else:
                ans += sum(x > j for x in G_index) 
        for j in G_index:
            if j < i:
                continue
            diff = j - i
            if j+diff in R_index:
                ans += sum(x > j for x in R_index) -1
            else:
                ans += sum(x > j for x in R_index)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
patterns = ["RGB","RBG","GBR","GRB","BRG","BGR"]

ans = 0
for pattern in patterns:
    c1 = pattern[0]
    c2 = pattern[1]
    c3 = pattern[2]
    cands = []
    #i c1の位置で
    for i1 in range(n):
        for i2 in range(i1 + 1,n):
            if S[i1] == c2 and S[i2] == c3:
                cands.append((i1,i2-i1))
    # anss[i1][i2-i1]→左をi1に固定したときの、距離i2-i1の組の個数

    for  i in range(0,n):
        if S[i] == c1:
            for cand in cands:
                if  cand[0] - i  != cand[1] and cand[0] > i:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
S=list(input())
Pattern=[x for x in combinations(range(N),3) if x[2]-x[1] != x[1]-x[0]]
res = sum([1 for x in Pattern if len(set(S[y] for y in x))==3])
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
b = []
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    else:
        b.append(i+1)
cnt = 0
for i in r:
    for j in g:
        if (i+j)%2==0:
            if (i + j) // 2 in b:
                cnt += 1
        if (2 * j) - i in b:
            cnt += 1
        if (2 * i) - j in b:
            cnt += 1
print((len(r) * len(g) * len(b))-cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j] or (2*j) - i < j:
                continue
            for k in range(j + 1, n):
                if s[i] == s[k] or s[j] == s[k] or (j - i) == (k - j):
                    continue
                else:
                    count += 1
    print(count)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R = [v for v,i in enumerate(S) if i == 'R']
G = [v for v,i in enumerate(S) if i == 'G']
B = [v for v,i in enumerate(S) if i == 'B']
t = len(B)
c = 0
for i in R:
    for j in G:
        a = max(i,j)
        b = min(i,j)
        d = [2* b -a,2 * a - b,(a+b)/2]
        for s in d:
            if s in B:
                c -= 1
        c += t

print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_list = list(s)#文字列をリストに
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

    su = 0
    for k in range(2, N):
        for j in range(1, k):
            for i in range(j):
                # print(i, j, k)
                if j-i == k-j:
                    pass
                elif S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    su += 1
                # su += 1
    print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
#h  = list(map(int, input().split()))
#h, w = list(map(int, input().split()))


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

print(count)

Submission status: Time Limit Exceeded

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

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

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = input()
ans = 0
for r in range(0,len(arr)):
    for g in range(r,len(arr)):
        if arr[g] != arr[r]:
            for b in range(g,len(arr)):
                if arr[b] != arr[g] and arr[b] != arr[r] and g-r != b-g:
                    ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
d = defaultdict(list)

for i,s in enumerate(S):
    d[s].append(i)

Bs = [0] * 8000
cnt_b = len(d["B"])

for i in  d["B"]:
    Bs[i] += 1

ans = 0
for i_r in d["R"]:
    for i_g in d["G"]:
        ls = []
        mx = max(i_r,i_g)
        mn = min(i_r,i_g)
        if (mx-mn) % 2 == 0:
            ls.append((mx+mn)//2)    
        ls.append(mx + (mx-mn))
        if  mn - (mx-mn) >= 0: 
            ls.append(mn-(mx-mn)) 
        cnt = 0
        for l in ls:
            cnt += Bs[l]
        # print(i_r,i_g,ls)
        ans += cnt_b - cnt
print(ans)

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)

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

Submission status: Time Limit Exceeded

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

answer = 0

for a in range(N):
  i = S[a]
  for b in range(N-a-1):
    j = S[b+a+1]
    if i == j:
      pass
    else:
      for c in range(N-a-b-2):
        k = S[c+a+b+2]
        if i == k or j == k:
          pass
        else:
          if (b+a+1) - a != (c+a+b+2) - (b+a+1):
            answer += 1
  	      
else:
  print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R = 0
G = 0
B = 0
RR = []
GG = []
BB = []
for i, s in enumerate(S):
  if s=='R':
    RR.append(i)
    R +=1
  elif s=='G':
    GG.append(i)
    G+=1
  else:
    BB.append(i*2)
    B+=1
ans_Full = R*G*B
for i in range(R):
  for j in range(G):
    if ((4*GG[j]-2*RR[i]) in BB):
      ans_Full -=1
    if ((4*RR[i]-2*GG[j]) in BB):
      ans_Full -=1
    if((RR[i]+GG[j]) in BB):
      ans_Full -=1

print(ans_Full)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
  return [i for i, _x in enumerate(l) if _x == x]
    
r_inds = my_index_multi(s, "R")
g_inds = my_index_multi(s, "G")
b_inds = my_index_multi(s, "B")

ans = 0
for r in r_inds:
  for g in g_inds:
    for b in b_inds:
      x = sorted([r, g, b])
      if x[1]-x[0] != x[2]-x[1]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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)
    elif S[i]=='B':
        B.append(i)
ans=0
for i in range(N):
    for j in range(i):
        if(S[i]==S[j]):
            continue
        else:
            s=set([S[i],S[j]])
        if('B' not in s):
            lon=len(B)-bisect.bisect_left(B,i)
            mi=bisect.bisect_left(B,i+i-j)
            if(mi<len(B) and B[mi]==i+i-j):
                lon-=1
        elif('R' not in s):
            lon=len(R)-bisect.bisect_left(R,i)
            mi=bisect.bisect_left(R,i+i-j)
            if(mi<len(R) and R[mi]==i+i-j):
                lon-=1
        elif('G' not in s):
            lon=len(G)-bisect.bisect_left(G,i)
            mi=bisect.bisect_left(G,i+i-j)
            if(mi<len(G) and G[mi]==i+i-j):
                lon-=1
        ans+=lon
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#4問目
count = 0
N = int(input())
S = list(input())
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(i <= j and j <= k):
                if(S[i] != S[j] and S[i] != S[k] and S[j] != S[k]):
                    if((j - i) != (k - j)):
                        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import bisect
n = ni()
s = ns()

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)

ans = 0


for i in range(n):
    if s[i] == 'R':
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(b,g[j])
            ans += (len(b)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'B':
                ans -= 1
        y = bisect.bisect_right(b,i)
        for j in range(y,len(b)):
            q = bisect.bisect_right(g,b[j])
            ans += (len(g)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'G':
                ans -= 1
    elif s[i] == 'G':
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(b,r[j])
            ans += (len(b)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'B':
                ans -= 1
        p = bisect.bisect_right(b,i)
        for j in range(p,len(b)):
            q = bisect.bisect_right(r,b[j])
            ans += (len(r)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'R':
                ans -= 1
    else:
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(r,g[j])
            ans += (len(r)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'R':
                ans -= 1
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(g,r[j])
            ans += (len(g)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'G':
                ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
c=0
for i in range(n-2):
  t1=s[i]
  for j in range(i+1,n-1):
    t2=s[j]
    if t1 not in t2:
      for k in range(j+1,n):
        t3=s[k]
        if t3 not in t1+t2 and j-i!=k-j:
          c+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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)

ans = 0
for i in range(n):
    for j in range(i+1, n):
        ci, cj = s[i], s[j]
        if ci==cj:
            continue
        ng = 2*j-i
        if (ci=='R' and cj=='B') or (ci=='B' and cj=='R'):
            ans += len(G)-bisect.bisect_left(G, j)
            if ng in G:
                ans -= 1
        elif (ci=='R' and cj=='G') or (ci=='G' and cj=='R'):
            ans += len(B)-bisect.bisect_left(B, j)
            if ng in B:
                ans -= 1
        else:
            ans += len(R)-bisect.bisect_left(R, j)
            if ng in R:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

def regular_intervals(r,g,b):
    i,j,k = sorted([r,b,g])
    if k-j == j-i:
        return True
    else:
        return False

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

Rids = []
Gids = []
Bids = []

for i,c in enumerate(S):
    if c == 'R':
        Rids.append(i)
    elif c == 'G':
        Gids.append(i)
    elif c == 'B':
        Bids.append(i)

print(sum(not regular_intervals(r,g,b) for r in Rids
              for g in Gids
              for b in Bids))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
words = [[0]*3for _ in range(n)] #i以降のR,G,Bの数(i含まず)
for i in range(n-2,-1,-1):
    if s[i+1] == 'R':
        words[i][0]+=1
    elif s[i+1] == 'G':
        words[i][1] +=1
    else:words[i][2] +=1
    words[i][0] +=words[i+1][0]
    words[i][1] +=words[i+1][1]
    words[i][2] +=words[i+1][2]
ans = 0
for i in range(n):
    for j in range(i+1,n):
        
        if s[i] == 'R':
            if s[j] == 'G':
                ans += words[j][2]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'B':ans-=1
            if s[j] == 'B':
                ans += words[j][1]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'G':ans-=1
        if s[i] == 'G':
            if s[j] == 'R':
                ans += words[j][2]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'B':ans-=1
            if s[j] == 'B':
                ans += words[j][0]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'R':ans-=1
        if s[i] == 'B':
            if s[j] == 'G':
                ans += words[j][0]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'R':ans-=1
            if s[j] == 'R':
                ans += words[j][1]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'G':ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import permutations as perm
from bisect import bisect_left as bisect
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


def gcd(a, b):
    # greatest common divisor
    while b > 0:
        a, b = b, a % b

    return a


def solve():
    def get_ans(c1, c2, c3):
        ret = 0
        lenc3 = len(RGB[c3])
        for c1i in range(len(RGB[c1])):
            tmp = bisect(RGB[c2], RGB[c1][c1i])
            for c2i in range(tmp, len(RGB[c2])):
                c3i = bisect(RGB[c3], RGB[c2][c2i])
                if lenc3 == c3i:
                    continue
                # print(c1i, c2i, c3i)
                c21d = RGB[c2][c2i] - RGB[c1][c1i]
                t = bisect(RGB[c3], RGB[c2][c2i] + c21d, c3i)

                if lenc3 != t and RGB[c3][t] - RGB[c2][c2i] == c21d:
                    ret += lenc3 - c3i - 1
                else:
                    ret += lenc3 - c3i

        return ret
    int(input())
    S = list(input().strip())
    RGB = {k: [] for k in "RGB"}
    for i, s in enumerate(S):
        RGB[s].append(i)

    ans = 0
    # print(RGB)
    for c1, c2, c3 in perm(list("RGB")):
        # print(c1, c2, c3)
        ans += get_ans(c1, c2, c3)
        # print(ans)
    print(ans)


solve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
from itertools import combinations as cmb
counter = 0
for i, j, k in cmb(list(range(n)), 3):
  #print(i, j, k, s[i], s[j], s[k])
  if j - i != k - j and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
    counter += 1
    #print("OK")
print(counter)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

s = s + "aa"*n

rl = [0]*(n+1)
gl = [0]*(n+1)
bl = [0]*(n+1)

for i in range(n-1,-1,-1):
  if s[i] == "R":
    rl[i] += 1
  if s[i] == "G":
    gl[i] += 1
  if s[i] == "B":
    bl[i] += 1
  rl[i]+=rl[i+1]
  gl[i]+=gl[i+1]
  bl[i]+=bl[i+1]
  
ans = 0
for i in range(n-3):
  for j in range(i+1,n-1):
    if s[i]==s[j]:
      continue
    if s[i]=="R":
      if s[j]=="G":
        ans += bl[j+1] - (s[2*j-i]=="B")
      elif s[j]=="B":
        ans += gl[j+1] - (s[2*j-i]=="G")
    if s[i]=="G":
      if s[j]=="R":
        ans += bl[j+1] - (s[2*j-i]=="B")
      elif s[j]=="B":
        ans += rl[j+1] - (s[2*j-i]=="R")
    if s[i]=="B":
      if s[j]=="G":
        ans += rl[j+1] - (s[2*j-i]=="R")
      elif s[j]=="R":
        ans += gl[j+1] - (s[2*j-i]=="G")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []

for i in range(0, len(s)):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    elif s[i] == "B":
        b.append(i+1)

cnt = 0
for i in r:
    for j in g:
        for k in b:
            if max(i, j, k) == k:
                if i < j:
                    if k-j != j-i:
                        cnt += 1
                else:
                    if k-i != i-j:
                        cnt += 1
            elif max(i, j, k) == j:
                if i < k:
                    if j-k != k-i:
                        cnt += 1
                else:
                    if j-i != i-k:
                        cnt += 1
            if max(i, j, k) == i:
                if k < j:
                    if i-j != j-k:
                        cnt += 1
                else:
                    if i-k != k-j:
                        cnt += 1


print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = [i+1 for i in range(N) if S[i] == 'R']
G = [i+1 for i in range(N) if S[i] == 'G']
B = [i+1 for i in range(N) if S[i] == 'B']

ans = 0

for r in R:
    for g in G:
        tmp = 0
        a,b = sorted([r,g])
        i = b-a
        if a-i in B:
            tmp+=1
        if b+i in B:
            tmp+=1
        if a+(i/2) == b -(i/2) and a+(i/2) in B:
            tmp += 1
        ans += len(B) - tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import Counter
from pprint import pprint

def myinput():
    return map(int,input().split())

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

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)
    elif s[i]=="B":
        b.append(i)
    else:
        pass
# print(r,g,b)

def mycount(r,g,b):
    c = 0
    for i in r:
        ng = [ x for x in g if x>i ]
        nb = [ x for x in b if x>i ]
        for j in ng:
            nb2 = [ y for y in b if y>j ]
            for k in nb2:
                if (j-i)!=(k-j):
                    c += 1
                else:
                    pass
        for j in nb:
            ng2 = [ y for y in g if y>j ]
            for k in ng2:
                if (j-i)!=(k-j):
                    c += 1
                else:
                    pass
    return c

ans = 0
ans += mycount(r,g,b)
ans += mycount(g,r,b)
ans += mycount(b,r,g)
print(ans)

Submission status: Time Limit Exceeded

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