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 = []
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)

score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)

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)
    elif(s[i]=="G"):
        g.append(i)
    elif(s[i]=="B"):
        b.append(i)
score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)

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)
    elif(s[i]=="G"):
        g.append(i)
    elif(s[i]=="B"):
        b.append(i)
score = len(r)*len(g)*len(b)

for i in r:
    for j in g:
        if (i>j)and(2*i-j in b):
            score-=1
        if (i>j)and(2*j-i in b):
            score-=1
        if(j>i)and(2*j-i in b):
            score-=1
        if(j>i)and(2*i-j in b):
            score-=1
        if((i+j)/2 in b):
            score-=1
# print(count_)
print(score)

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)
    elif(s[i]=="G"):
        g.append(i)
    elif(s[i]=="B"):
        b.append(i)
# print(r)
# print(b)
score = len(r)*len(g)*len(b)
count_ = 0
for i in r:
    for j in g:
        if (i>j)and(2*i-j in b):
            count_ +=1
        if (i>j)and(2*j-i in b):
            count_+=1
        if(j>i)and(2*j-i in b):
            count_ +=1
        if(j>i)and(2*i-j in b):
            count_+=1
        if((i+j)/2 in b):
            count_ +=1
# print(count_)
print(score-count_)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = str(input())
count = 0
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)
# print(r)
# print(b)
count=0
for i in r:
    for j in g:
        for k in b:
            if abs(j-i)!=abs(k-j) and abs(j-i)!=abs(k-i) and(abs(k-i)!=abs(k-j)):
                count +=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin,stdout


def convert(x):
    if x == 'R': return 0
    if x == 'G': return 1
    if x == 'B': return 2
    

def solve(S,N):
    presum = [[0 for _ in range(3)] for _ in range(N + 1)]
    
    for i in range(N):
        for j in range(3): 
            presum[i + 1][j] += presum[i][j]
        presum[i + 1][S[i]] += 1

    #Exclusion 

    
    sumall = 0
    for i in range(N - 2):
        for k in range(i + 2,N):
            if S[i] != S[k]:
                c = 3 - S[i] - S[k] 
                sumall += presum[k][c] - presum[i + 1][c]
                
                if (i + k) % 2 == 0:
                    j = (i + k)//2
                    if S[j] == c: sumall -= 1
    return str(sumall)


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    S = [convert(x) for x in S]
    
    stdout.write(solve(S,N))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin,stdout


def convert(x):
    if x == 'R': return 0
    if x == 'G': return 1
    if x == 'B': return 2
    

def solve(S,N):
    presum = [[0 for _ in range(3)] for _ in range(N + 1)]
    
    for i in range(N):
        for j in range(3): 
            presum[i + 1][j] += presum[i][j]
        presum[i + 1][S[i]] += 1

    #Exclusion 

    
    sumall = 0
    for i in range(N - 2):
        for k in range(i + 2,N):
            if S[i] != S[k]:
                c = 3 - S[i] - S[k] 
                sumall += presum[k][c] - presum[i + 1][c]
                
                if (i + k) % 2 == 0:
                    j = (i + k)//2
                    if S[j] == c: sumall -= 1
    return sumall


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    S = [convert(x) for x in S]
    
    print(solve(S,N))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
ans=0
for x in range(n):
    a=s[x]
    for y in range(x+1,n):
        if s[y]!=a:
            b=s[y]
            for z in range (y+1,n):
                if (z-y!=y-x) and s[z]!=a and s[z]!=b:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
    c = [[] for _ in range(3)] #c = [[]] * 3 とすると全ての要素にappendされてしまうのでNG
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : # 'B'のとき
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])
    
    reigai = 0
    for i in c[0]:
        for j in c[1]:
            s = j - i # i < j と仮定して差を求める。逆の場合は負の値になるのでOK
            if j + s in c[2]:
                reigai += 1
            if i - s in c[2]:
                reigai += 1
            if (i + j) / 2 in c[2]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

r = s.count('R')
g = s.count('G')
b = s.count('B')

cnt = 0

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

Submission status: Time Limit Exceeded

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

def main():

    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(n):
        for j in range(n):
            if i + j*2 < n:
                if s[i] != s[i + j]:
                    if s[i] != s[i + j*2]:
                        if s[i + j] != s[i + j*2]:
                            ans -= 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

def main():

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=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 j-i!=k-j:
                if S[i-1]!=S[j-1] and S[i-1]!=S[k-1]\
                    and S[j-1]!=S[k-1]:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=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[i-1]!=S[k-1]\
                and S[j-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()
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[j]!=S[k]) and (S[k]!=S[i]) and ((k-j)!=(j-i)):
                cnt+=1
            else:
                continue
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
for i in range(N-2):
    for j in range(i+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:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
for i in range(N-2):
    for j in range(i+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:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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)

samu = 0

for i in R:
    for j in G:
        for k in B:
            X = sorted([i,j,k])
            if(X[2] - X[1] != X[1] - X[0]):
                samu += 1

print(samu)

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    _rs = []
    _gs = []
    _bs = []
    for i in range(n):
        if s[i] == "R":
            _rs.append(i)
        elif s[i] == "G":
            _gs.append(i)
        else:
            _bs.append(i)

    cnt = 0
    lrs = len(_rs)
    lgs = len(_gs)
    lbs = len(_bs)
    rs = _rs
    gs = _gs
    bs = _bs

    if lrs < lgs < lbs:
        rs = _rs
        gs = _gs
        bs = _bs
    elif lrs < lbs < lgs:
        rs = _rs
        gs = _bs
        bs = _gs
    elif lgs < lrs < lbs:
        rs = _gs
        gs = _rs
        bs = _bs
    elif lgs < lbs < lrs:
        rs = _gs
        gs = _bs
        bs = _rs
    elif lbs < lrs < lgs:
        rs = _bs
        gs = _rs
        bs = _gs
    elif lbs < lgs < lrs:
        rs = _bs
        gs = _gs
        bs = _rs

    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df % 2 == 0:
                    mm = ri + int(df / 2)

                lim = rr if rr <= (n - 1) else max(mm, ll)

                if lim < 0:
                    continue

                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt += 1
                    elif bi == mm:
                        cnt += 1
                    elif bi == rr:
                        cnt += 1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df % 2 == 0:
                    mm = gi + int(df / 2)

                lim = rr if rr <= (n - 1) else max(mm, ll)

                if lim < 0:
                    continue
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt += 1
                    elif bi == mm:
                        cnt += 1
                    elif bi == rr:
                        cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    _rs = []
    _gs = []
    _bs = []
    for i in range(n):
        if s[i] == "R":
            _rs.append(i)
        elif s[i] == "G":
            _gs.append(i)
        else:
            _bs.append(i)

    cnt = 0
    lrs = len(_rs)
    lgs = len(_gs)
    lbs = len(_bs)
    rs = _rs
    gs = _gs
    bs = _bs

    if lrs < lgs < lbs:
        rs = _rs
        gs = _gs
        bs = _bs
    elif lrs < lbs < lgs:
        rs = _rs
        gs = _bs
        bs = _gs
    elif lgs < lrs < lbs:
        rs = _gs
        gs = _rs
        bs = _bs
    elif lgs < lbs < lrs:
        rs = _gs
        gs = _bs
        bs = _rs
    elif lbs < lrs < lgs:
        rs = _bs
        gs = _rs
        bs = _gs
    elif lbs < lgs < lrs:
        rs = _bs
        gs = _gs
        bs = _rs

    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df %2 ==0:
                    mm = ri + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df %2 ==0:
                    mm = gi + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df %2 ==0:
                    mm = ri + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df %2 ==0:
                    mm = gi + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                if rr in bs:
                    cnt+=1
                if ll in bs:
                    cnt+=1
                if df %2 ==0:
                    mm = ri + int(df/2)
                    if mm in bs:
                        cnt +=1
            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                if rr in bs:
                    cnt+=1
                if ll in bs:
                    cnt+=1
                if df %2 ==0:
                    mm = gi + int(df/2)
                    if mm in bs:
                        cnt +=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            rg = abs(ri - gi)
            for bi in bs:
                rb = abs(ri - bi)
                if rg == rb:
                    cnt += 1
                elif (gi < ri and bi < ri) or (ri < gi and ri < bi):
                    if rg == 2 * rb or 2 * rg == rb:
                        cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()

Submission status: Time Limit Exceeded

Solution code:
import math


def main():
    n = int(input())
    s = input()

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            for bi in bs:
                if ri < gi < bi:
                    if bi - gi != gi - ri:
                        cnt += 1
                elif ri < bi < gi:
                    if gi - bi != bi - ri:
                        cnt += 1
                elif bi < gi < ri:
                    if ri - gi != gi - bi:
                        cnt += 1
                elif bi < ri < gi:
                    if gi - ri != ri - bi:
                        cnt += 1
                elif gi < ri < bi:
                    if bi - ri != ri - gi:
                        cnt += 1
                else:
                    if ri - bi != bi - gi:
                        cnt += 1

    print(cnt)


main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
t=len(s)
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
for ri in r:
    for gi in g:
        for bi in b:
            if 2*ri==gi+bi or 2*gi==ri+bi or 2*bi==gi+ri:
                a+=1
d=len(r)*len(g)*len(b)
d=d-a
print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
t=len(s)
r=[]
g=[]
b=[]
a=0
for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='G':
        g.append(i)
    if s[i]=='B':
        b.append(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        rg=gi-ri
        if rg+gi in b:
            d-=1
        if  ri-rg in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
t=len(s)
r=[]
g=[]
b=[]
a=0
for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='G':
        g.append(i)
    if s[i]=='B':
        b.append(i)
for ri in r:
    for gi in g:
        for bi in b:
            if 2*ri==gi+bi or 2*gi==ri+bi or 2*bi==gi+ri:
                a+=1
d=len(r)*len(g)*len(b)
d=d-a
print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
t=len(s)
r=[]
g=[]
b=[]
a=0
for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='G':
        g.append(i)
    if s[i]=='B':
        b.append(i)
for ri in r:
    for gi in g:
        for bi in b:
            if 2*ri==gi+bi or 2*gi==ri+bi or 2*bi==gi+ri:
                continue
            else:
                a+=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
#abc162-d
n=int(input())
s=str(input())
t=len(s)
a=0
for i in range(t):
    for j in range(i+1,t):
        for k in range(j+1,t):
            if k-j==j-i:
                continue
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                a+=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
from collections import deque
s=deque(input())
R=[]
G=[]
B=[]
i=0
while s:
    x=s.popleft()
    if x=='R':
        R.append(i)
    elif x=='G':
        G.append(i)
    else:
        B.append(i)
    i+=1
#print(R)    
ans=0
from itertools import product
S=product(R,G,B)
for i,j,k in S:
    if i+j-2*k!=0 and j+k-2*i!=0 and k+i-2*j!=0:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
from collections import deque
s=deque(input())
R=[]
G=[]
B=[]
i=0
while s:
    x=s.popleft()
    if x=='R':
        R.append(i)
    elif x=='G':
        G.append(i)
    else:
        B.append(i)
    i+=1
#print(R)    
ans=0
for i in R:
    for j in G:
        for k in B:
            if not(i+j==2*k or j+k==2*i or k+i==2*j):
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
rgb=input()
r=[]
g=[]
b=[]
s=0
for i in range(n):
  if rgb[i]=="R":
    r.append(i)
  elif rgb[i]=="G":
    g.append(i)
  elif rgb[i]=="B":
    b.append(i)
for x in range(len(r)):
  for y in range(len(g)):
    for z in range(len(b)):
      if (r[x]<g[y] and g[y]<b[z]) or (r[x]>g[y] and g[y]>b[z]):
        if abs(r[x]-g[y])!=abs(g[y]-b[z]):
          s+=1
      elif (r[x]<b[z] and b[z]<g[y]) or (r[x]>b[z] and b[z]>g[y]):
        if abs(r[x]-b[z])!=abs(b[z]-g[y]):
          s+=1
      elif (g[y]<r[x] and r[x]<b[z]) or (g[y]>r[x] and r[x]>b[z]):
        if abs(g[y]-r[x])!=abs(r[x]-b[z]):
          s+=1
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = 0
r = s.count('R')
g = s.count('G')
b = s.count('B')

for i in range(n):
    for j in range(i,n):
        if i == j:
            continue
        k = 2 * j - i
        if k > len(s) - 1:
            continue
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            cnt +=1

all = r * g * b
print(all - cnt)

Submission status: Time Limit Exceeded

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

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

R_num = [0]*(N+1)
G_num = [0]*(N+1)
B_num = [0]*(N+1)

for i in range(N):
    if S[i]=="R":
        R[i] = 1
        continue
    if S[i]=="G":
        G[i] = 1
        continue
    if S[i]=="B":
        B[i] = 1
        continue


for i in range(N):
    R_num[i+1] = R[i] + R_num[i]
    G_num[i+1] = G[i] + G_num[i]
    B_num[i+1] = B[i] + B_num[i]
    
ans = 0
#愚直に実装すると
for i in range(N-2):
    for j in range(i+1, N-1):
        #累積和で求める
        if (S[i]==S[j]):
            continue
        if (S[i]=="R" and S[j]=="G") or (S[i]=="G" and S[j]=="R"):
            ans = ans + B_num[N]-B_num[j+1]
            if( (j+j-i)<N and B[j+j-i]==1):
                ans = ans - 1
        if (S[i]=="R" and S[j]=="B") or (S[i]=="B" and S[j]=="R"):
            ans = ans + G_num[N]-G_num[j+1]
            if (j+j-i)<N and (G[j+j-i]==1):
                ans = ans - 1
        if (S[i]=="B" and S[j]=="G") or (S[i]=="G" and S[j]=="B"):
            ans = ans + R_num[N]-R_num[j+1]
            if (j+j-i)<N and (R[j+j-i]==1):
                ans = ans - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 = len(r)*len(b)*len(g)
r, g, b = np.array(r), np.array(g), np.array(b)
for i in r:
    for j in g:
        for k in b:
            a = sorted([i,j,k])
            if a[1] - a[0] == a[2] - a[1]:
                ans -= 1

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 = len(r)*len(b)*len(g)
for i in r:
    for j in g:
        for k in b:
            a = sorted([i,j,k])
            if a[1] - a[0] == a[2] - a[1]:
                ans -= 1

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 = len(r)*len(b)*len(g)
tmp = []
for i in r:
    tmp.append(i)
    for j in g:
        tmp.append(j)
        for k in b:
            tmp.append(k)
            a = sorted(tmp)
            if a[1] - a[0] == a[2] - a[1]:
                ans -= 1
            tmp.pop()
        tmp.pop()
    tmp.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
import numpy as np
s = input()
ans = 0
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)
r, g, b = np.array(r), np.array(g), np.array(b)
tmp = []
for i in r:
    tmp.append(i)
    for j in g:
        tmp.append(j)
        for k in b:
            tmp.append(k)
            tmp.sort()
            if tmp[1] - tmp[0] != tmp[2] - tmp[1]:
                ans += 1
            tmp.remove(k)
        tmp.remove(j)
    tmp.remove(i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
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)
tmp = []
for i in r:
    tmp.append(i)
    for j in g:
        tmp.append(j)
        for k in b:
            tmp.append(k)
            tmp.sort()
            if tmp[1] - tmp[0] != tmp[2] - tmp[1]:
                ans += 1
            tmp.remove(k)
        tmp.remove(j)
    tmp.remove(i)

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

Submission status: Time Limit Exceeded

Solution code:
import bisect


def count(text, s, rgb_bag):
    ans = 0
    for i in rgb_bag[text[0]]:
        for j in rgb_bag[text[1]]:
            if i > j:
                continue
            k = bisect.bisect_left(rgb_bag[text[2]], j)
            ans += len(rgb_bag[text[2]]) - k
            ng_idx = j + (j - i)
            if ng_idx >= len(s):
                continue
            if s[ng_idx] == text[2]:
                ans -= 1
    return ans


def main():
    n = int(input())
    s = input()
    rgb_bag = {'R': [], 'G': [], 'B': []}
    for i, v in enumerate(s):
        rgb_bag[v].append(i)
    ans = 0

    for r in 'RGB':
        for g in 'RGB':
            for b in 'RGB':
                if r == g or g == b or b == r:
                    continue
                ans += count(r+g+b, s, rgb_bag)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

print(count)

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([s[i], s[i+k], s[i+2*k]])
    if len(se) == 3:
        return True
    return False


for k in range(1, n//2+1):
    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())
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//2+1):
    for i in range(n):
        if i+2*k >= n:
            break
        if rgb(k, i):
            ans -= 1
print(ans)