Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c_r = 0
c_g = 0
c_b = 0
for i in range(n):
    if s[i] == "R":
        c_r += 1
    elif s[i] == "G":
        c_g += 1
    elif s[i] == "B":
        c_b += 1
c = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == "R" and s[j] == "G":
            if j < 2*j-i < n:
                if s[2*j-i] == "B":
                    c += 1
        if s[i] == "R" and s[j] == "B":
            if j < 2*j-i < n:
                if s[2*j-i] == "G":
                    c += 1
        if s[i] == "G" and s[j] == "R":
            if j < 2*j-i < n:
                if s[2*j-i] == "B":
                    c += 1
        if s[i] == "G" and s[j] == "B":
            if j < 2*j-i < n:
                if s[2*j-i] == "R":
                    c += 1
        if s[i] == "B" and s[j] == "R":
            if j < 2*j-i < n:
                if s[2*j-i] == "G":
                    c += 1
        if s[i] == "B" and s[j] == "G":
            if j < 2*j-i < n:
                if s[2*j-i] == "R":
                    c += 1
        else:
            pass
print(c_r*c_g*c_b-c)

Submission status: Time Limit Exceeded

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

total = s.count("R") * s.count("G") * s.count("B")

#l = ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]

lR = [0]*n
lG = [0]*n
lB = [0]*n

for i in range(len(s)):
    if s[i] == "R":
        lR[i] += 1
    elif s[i] == "G":
        lG[i] += 1
    else:
        lB[i] += 1

#print(lR)
#print(lG)
#print(lB)

for i in range(n):
    if lR[i]:
        for j in range(n):
            if lG[j]:
                for k in range(n):
                    if lB[k]:
                        if abs(j-i) == abs(k-j) or abs(j-k) == abs(k-i) or abs(j-i) == abs(k-i):
                            total -= 1
print(total)

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

            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(G)*len(B)
for i in range(n-2):
    for j in range(i+1,n-1):
        k  = 2*j-i
        if k<n:
            s1,s2,s3 = s[i],s[j],s[k]
            if (s1!=s2) and (s1!=s3) and (s2!=s3):
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [s for s in input()]
ans = S.count("R") * S.count("G") * S.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [s for s in input()]
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        A = S[i]
        B = S[j]
        C = S[k]
        if A == B or B == C or A == C:
            continue
        else:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if N - 1 < j + (j - i):
            continue
        elif S[i] == S[j] or S[j + (j - i)] == S[i] or S[j + (j - i)] == S[j]:
            continue
        else:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c = 0
for i in range(n):
    s1 = s[i]
    for j in range(i,n):
        s2 = s[j]
        for k in range(j,n):
            s3 = s[k]
            if (s1==s2) or (s1==s3) or (s2 == s3):
                pass
            elif (j-i)==((k-j)):
                pass
            else:
                c+=1

print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        elif S[i] == S[j] or S[k] == S[i] or S[k] == S[j]:
            continue
        else:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = S.count("R") * S.count("G") * S.count("B") - exc
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N - 2):
    for j in range(1, N - 1):
        k = j + (j - i)
        if i > j or k < 2 or N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            exc += 1

ans = S.count("R") * S.count("G") * S.count("B") - exc
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N - 2):
    for j in range(1, N - 1):
        d = j - i
        if d <= 0 or d + j > N - 1 or S[i] == S[j]:
            continue
        k = d + j
        if S[k] != S[i] and S[k] != S[j]:
            exc += 1

ans = S.count("R") * S.count("G") * S.count("B") - exc
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(N):
        d = j - i
        if d <= 0 or d + j > N - 1 or S[i] == S[j]:
            continue
        k = d + j
        if S[k] != S[i] and S[k] != S[j]:
            exc += 1

ans = S.count("R") * S.count("G") * S.count("B") - exc
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

def check(l1,l2,l3):
    count = 0
    for n2 in l2:
        for n1 in l1:
            if n1 > n2:
                break
            z = 2*n2 - n1
            for n3 in l3[::-1]:
                if n2 > n3:
                    break
                if n3 != z:
                    count += 1
    return count

N = int(input())
S = input()
Rlist, Glist, Blist = [],[],[]
for x in range(N):
    if S[x] == 'R':
        Rlist.append(x)
    elif S[x] == 'G':
        Glist.append(x)
    else:
        Blist.append(x)
    
ans = 0
for p in permutations([Rlist,Glist,Blist]):
    ans += check(p[0],p[1],p[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(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)

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


for i in R:
    for j in G:
        k = j + (j - i)
        if k in B:
            ans -= 1
        k = i + (i - j)
        if k in B:
            ans -= 1
        k = (i+j)/2
        if k in B:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(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)

for i in R:
    for j in G:
        for k in B:
            a = [i,j,k]
            a.sort()
            if a[2] - a[1] == a[1] - a[0]:
                continue
            ans += 1

print(ans)

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

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)
    else:
        b.append(i)
p = len(r)*len(g)*len(b)
for j in r:
    for k in g:
        for l in b:
            ls = [j,k,l]
            ls.sort()
            (a,c,d) = (ls[0],ls[1],ls[2])
            if (c-a) == (d-c):
                p -= 1
print(p)

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)
    else:
        b.append(i)
p = 0
for j in r:
    for k in g:
        for l in b:
            ls = [j,k,l]
            ls.sort()
            (a,c,d) = (ls[0],ls[1],ls[2])
            if (c-a) != (d-c):
                p += 1
print(p)

Submission status: Time Limit Exceeded

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

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

def main():
    n = int(input())
    s = input()
    ans = 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:
                    if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                        ans += 1
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
ans = 0
for i in range(1,len(S)+1):
    for j in range(i+1,len(S)+1):
        for k in range(j+1,len(S)+1):
            if j-i==k-j:
                continue
            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:
def solve():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    def rap(num1):
        r.append(num1)
    def gap(num1):
        g.append(num1)
    def bap(num1):
        b.append(num1)
    for i in range(n):
        if s[i]=="R":
            rap(i)
        elif s[i]=="G":
            gap(i)
        else:
            bap(i)
    numr=len(r)
    numg=len(g)
    numb=len(b)
    nummax=max(numr,numg,numb)
    nummin=max(numr,numg,numb)
    def numabs(num1):
        return abs(num1)
    stra=[]
    def tc():
        return stra.count(True)
    if nummax==numr:
        if nummin==numg:
            ans=0
            def r1(num2,num3):
                num1=numabs(b[num2]-g[num3])
                return [min(b[num2],g[num3])-num1 in r,max(b[num2],g[num3])+num1 in r,(b[num2]+g[num3])/2 in r]
            for i in range(numb):
                for j in range(numg):
                    stra=r1(i,j)
                    ans+=numr-tc()
        else:
            ans=0
            def r2(num2,num3):
                num1=numabs(g[num2]-b[num3])
                return [min(g[num2],b[num3])-num1 in r,max(g[num2],b[num3])+num1 in r,(g[num2]+b[num3])/2 in r]
            for i in range(numg):
                for j in range(numb):
                    stra=r2(i,j)
                    ans+=numr-tc()
    elif nummax==numg:
        if nummin==numr:
            ans=0
            def g1(num2,num3):
                num1=numabs(b[num2]-r[num3])
                return [min(b[num2],r[num3])-num1 in g,max(b[num2],r[num3])+num1 in g,(b[num2]+r[num3])/2 in g]
            for i in range(numb):
                for j in range(numr):
                    stra=g1(i,j)
                    ans+=numg-tc()
        else:
            ans=0
            def g2(num2,num3):
                num1=numabs(r[num2]-b[num3])
                return [min(r[num2],b[num3])-num1 in g,max(r[num2],b[num3])+num1 in g,(r[num2]+b[num3])/2 in g]
            for i in range(numr):
                for j in range(numb):
                    stra=g2(i,j)
                    ans+=numg-tc()
    else:
        if nummin==numr:
            ans=0
            def r1(num2,num3):
                num1=numabs(g[num2]-r[num3])
                return [min(g[num2],r[num3])-num1 in b,max(g[num2],r[num3])+num1 in b,(g[num2]+r[num3])/2 in b]
            for i in range(numg):
                for j in range(numr):
                    stra=r1(i,j)
                    ans+=numb-tc()
        else:
            ans=0
            def r2(num2,num3):
                num1=numabs(r[num2]-g[num3])
                return [min(r[num2],g[num3])-num1 in b,max(r[num2],g[num3])+num1 in b,(r[num2]+g[num3])/2 in b]
            for i in range(numr):
                for j in range(numg):
                    stra=r2(i,j)
                    ans+=numb-tc()
    print(ans)
solve()

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    def rap(num1):
        r.append(num1)
    def gap(num1):
        g.append(num1)
    def bap(num1):
        b.append(num1)
    for i in range(n):
        if s[i]=="R":
            rap(i)
        elif s[i]=="G":
            gap(i)
        else:
            bap(i)
    ans=0
    numr=len(r)
    numg=len(g)
    numb=len(b)
    nummax=max(numr,numg,numb)
    nummin=max(numr,numg,numb)
    def numabs(num1):
        return abs(num1)
    stra=[]
    def tc():
        return stra.count(True)
    if nummax==numr:
        if nummin==numg:
            for i in range(numb):
                for j in range(numg):
                    num=numr
                    num1=numabs(b[i]-g[j])
                    stra=[min(b[i],g[j])-num1 in r,max(b[i],g[j])+num1 in r,(b[i]+g[j])/2 in r]
                    ans+=num-tc()
        else:
            for i in range(numg):
                for j in range(numb):
                    num=numr
                    num1=numabs(g[i]-b[j])
                    stra=[min(g[i],b[j])-num1 in r,max(g[i],b[j])+num1 in r,(g[i]+b[j])/2 in r]
                    ans+=num-tc()
    elif nummax==numg:
        if nummin==numr:
            for i in range(numb):
                for j in range(numr):
                    num=numg
                    num1=numabs(b[i]-r[j])
                    stra=[min(b[i],r[j])-num1 in g,max(b[i],r[j])+num1 in g,(b[i]+r[j])/2 in g]
                    ans+=num-tc()
        else:
            for i in range(numr):
                for j in range(numb):
                    num=numg
                    num1=numabs(r[i]-b[j])
                    stra=[min(r[i],b[j])-num1 in g,max(r[i],b[j])+num1 in g,(r[i]+b[j])/2 in g]
                    ans+=num-tc()
    else:
        if nummin==numr:
            for i in range(numg):
                for j in range(numr):
                    num=numb
                    num1=numabs(g[i]-r[j])
                    stra=[min(g[i],r[j])-num1 in b,max(g[i],r[j])+num1 in b,(g[i]+r[j])/2 in b]
                    ans+=num-tc()
        else:
            for i in range(numr):
                for j in range(numg):
                    num=numb
                    num1=numabs(r[i]-g[j])
                    stra=[min(r[i],g[j])-num1 in b,max(r[i],g[j])+num1 in b,(r[i]+g[j])/2 in b]
                    ans+=num-tc()
    print(ans)
solve()

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    def rap(num1):
        r.append(num1)
    def gap(num1):
        g.append(num1)
    def bap(num1):
        b.append(num1)
    for i in range(n):
        if s[i]=="R":
            rap(i)
        elif s[i]=="G":
            gap(i)
        else:
            bap(i)
    ans=0
    def numabs(num1):
        return abs(num1)
    for i in range(len(r)):
        for j in range(len(g)):
            for k in range(len(b)):
                if numabs(r[i]-g[j])!=numabs(g[j]-b[k]) and numabs(g[j]-b[k])!=numabs(b[k]-r[i]) and numabs(b[k]-r[i])!=numabs(r[i]-g[j]):
                    ans+=1
    print(ans)
solve()

Submission status: Time Limit Exceeded

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

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:
                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:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    s=STR()
    r=s.count('R')
    g=s.count('G')
    b=s.count('B')
    ans=r*g*b
    #print(ans)
    for i in range(n+1):
        for j in range(1,n//2+2):
            k=i+2*j
            if k<=n-1:
                ch=set()
                ch.add(s[i])
                ch.add(s[i+j])
                ch.add(s[k])
                #print(ch)
                if len(ch)==3:
                    ans-=1
                    
    print(ans)

                
if __name__ == '__main__':
    do()

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
N = int(input())
S = input()
res = N**3

for i in range(N):
  for j in range(N):
    for k in range(N):
      if not i<j<k: 
        res -= 1
        continue
      
      if j-i == k-j:
        res -= 1
        continue

      if S[i]==S[j] or S[i]==S[k] or S[j]==S[k]:
        res -= 1
        continue

print(res)

Submission status: Time Limit Exceeded

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

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        elif c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        else:
            ans += (ruisekiB[n] - ruisekiB[j])

        if j+(j-i)<n and s[j+(j-i)] == c:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        if c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        if c == "B":
            ans += (ruisekiB[n] - ruisekiB[j])

        if j+(j-i)<n and s[j+(j-i)] == c:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

ruisekiR = [0]*(n+1)
ruisekiG = [0]*(n+1)
ruisekiB = [0]*(n+1)

for i, c in enumerate(s):
  ruisekiR[i+1] = ruisekiR[i] + (1 if c == 'R' else 0)
  ruisekiG[i+1] = ruisekiG[i] + (1 if c == 'G' else 0)
  ruisekiB[i+1] = ruisekiB[i] + (1 if c == 'B' else 0)
  
for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i] == s[j]:
      continue
    c = 'R'
    if 'R' in (s[i],s[j]):
      c = 'G'
      if 'G' in (s[i],s[j]):
        c = 'B'
    
    if c == 'R':
      ans += ruisekiR[n]-ruisekiR[j]
    if c == 'G':
      ans += ruisekiG[n]-ruisekiG[j]
    if c == 'B':
      ans += ruisekiB[n]-ruisekiB[j]
    
    if j+(j-i) < n and s[j+(j-i)] == c:
      ans -= 1
      
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n-2):
    for j in range(i,n-1):
        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 = input()
s = list(input())
n = int(n)
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()

count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
a,b = '',''
r = S.count('R')
g = S.count('G')
b = len(S)-r-g
rgb = r*g*b
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,N):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if 0<=k<=N-1:
        if S[k]!=b and S[k]!=a:
          expt += 1
print(rgb-expt)

Submission status: Time Limit Exceeded

Solution code:
import collections as cl
N = int(input())
S = input()
a,b = '',''
d = cl.Counter(S)
rgb = d['R']*d['B']*d['G']
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,N):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if 0<=k<=N-1:
        if S[k]!=b and S[k]!=a:
          expt += 1
print(rgb-expt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    if c == "G":
        g += 1
    if c == "B":
        b += 1
ans = r*g*b

for i in range(n):
    for j in range(i+1,n):
        k = 2*j - i
        if k > n-1:
            continue
        x = s[i]
        y = s[j]
        z = s[k]

        if x!= y and y != z and z != x:
            ans -= 1

print(ans)

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")
ans = r*g*b

for i in range(n):
    for j in range(i+1,n):
        k = 2*j - i
        if k > n-1:
            continue
        x = s[i]
        y = s[j]
        z = s[k]

        if x!= y and y != z and z != x:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n=int(input())
dic={ 'R':[], 'G':[], 'B':[] }

stri=list(input())
count=0
for i,v in enumerate(stri):
  #print(i,v)
  dic[v].append(i)
  
for name in dic:
  dic[name]=np.asarray(dic[name])
#print(dic)
for i in range(n):
  lib=['R','G','B']
  lib.remove(stri[i])
  #print(lib)
  for col in lib:
    for j in dic[col][dic[col]>i]:
      t=[i for i in lib if i != col].pop()
      #print(i,j)
      for k in dic[t][dic[t]>j]:
        if 2*j-i-k!=0:
          #print(i,j,k)
          count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dic={}
cntr=s.count("R")
cntg=s.count("G")
cntb=s.count("B")
for i in range(n):
    if s[i]=="R":
        dic[i]="R"
    if s[i]=="G":
        dic[i]="G"
    if s[i]=="B":
        dic[i]="B"

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1 and dic[i]!= dic[j] and dic[i]!=dic[2*j-i] and dic[j]!=dic[2*j-i]:
                cnt+=1
print(cntr*cntg*cntb-cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dic={}
cntr=0
cntg=0
cntb=0
for i in range(n):
    if s[i]=="R":
        dic[i]="R"
        cntr+=1
    if s[i]=="G":
        dic[i]="G"
        cntg+=1
    if s[i]=="B":
        dic[i]="B"
        cntb+=1

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1 and dic[i]!= dic[j]:
            if dic[i]!=dic[2*j-i] and dic[j]!=dic[2*j-i]:
                cnt+=1
print(cntr*cntg*cntb-cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = '0'
S += input()
R_count = 0
G_count = 0
B_count = 0
for i in range(1, N + 1):
  if S[i] == 'R':
    R_count += 1
  if S[i] == 'G':
    G_count += 1
  if S[i] == 'B':
    B_count += 1
res = R_count * G_count * B_count
for i in range(1, N + 1):
  for j in range(i + 1, N + 1):
    if 2 * j - i > N:
      break
    temp = []
    temp += S[i] + S[j] + S[2 * j - i]
    temp.sort()
    if temp[0] == 'B' and temp[1] == 'G' and temp[2] == 'R':
      res -= 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = input()

t = []

ans = 0
MAX = 4002
for i in range(1, min(len(s) + 1, MAX)):
    for j in range(i + 1, min(len(s) + 1, MAX)):
        if s[i - 1] == s[j - 1]:
            continue
        for k in range(j + 1, min(len(s) + 1, MAX)):
            if j - i == k - j:
                continue
            if s[k - 1] == s[i - 1] or s[k - 1] == s[j - 1]:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = input()

t = []

ans = 0
def dfs(a, n):
    global ans, s
    if len(t) == 3:
        i, j, k = t[0], t[1], t[2]
        if j - i == k - j:
            return
        i -= 1
        j -= 1
        k -= 1
        if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
            return
        ans += 1
        return
    for i in range(a, n + 1):
        t.append(i)
        dfs(i + 1, n)
        t.pop()

dfs(1, n)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets
N = int(input())
S = input()
R = []
G = []
B = []
RG = []
GB = []
BR = []
ans = 0
for i in range(N):
    if S[i]=='R':
        R.append(i)
        for j in G:
            RG.append([j,i])
        for j in B:
            BR.append([j,i])
        for j,k in GB:
            if k-j!=i-k:
                ans += 1
    elif S[i]=='G':
        G.append(i)
        for j in B:
            GB.append([j,i])
        for j in R:
            RG.append([j,i])
        for j,k in BR:
            if k-j!=i-k:
                ans += 1
    else:
        B.append(i)
        for j in R:
            BR.append([j,i])
        for j in G:
            GB.append([j,i])
        for j,k in RG:
            if k-j!=i-k:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
r=b=g=0
for s in S:
    if s=='R':
        r+=1
    elif s=='B':
        b+=1
    else:
        g+=1
out=0
for i in range(N):
    for j in range(N):
        if 0<=i+j<N and 0<=i-j<N and S[i]!=S[i+j] and S[i]!=S[i-j] and S[i+j]!=S[i-j]:
            out+=1
print(r*b*g-out)

Submission status: Time Limit Exceeded

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

p = 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
            if {s[i], s[j], s[k]} == {'R', 'G', 'B'}:
                p += 1
print(p)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
r=S.count('R')
g=S.count('G')
b=S.count('B')
c=0
for i in range(N):
  for j in range(i+1,N):
    if 2*j-i<=N-1:
      if S[i]!=S[j] and S[i]!=S[2*j-i] and S[j]!=S[2*j-i]:
        c+=1
ans=r*g*b-c
print(ans)

Submission status: Time Limit Exceeded

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

RGB = set(['R', 'G', 'B'])
list_R = []
list_G = []
list_B = []
for k in range(N):
    s_sep = S[k]
    if s_sep == 'R':
        list_R.append(k)
    elif s_sep == 'G':
        list_G.append(k)
    else:
        list_B.append(k)


def count_num(list_RGB, i, j):
    counter = 0
    for k in list_RGB[::-1]:
        if k < j:
            break
        if k - j != j - i:
            counter += 1
    return counter


count = 0
for i in range(0, N):
    for j in range(i+1, N):
        one = RGB - set([S[i], S[j]])
        if len(one) == 1:
            if list(one)[0] == 'R':
                count += count_num(list_R, i, j)
            elif list(one)[0] == 'G':
                count += count_num(list_G, i, j)
            else:
                count += count_num(list_B, i, j)

print(count)