Problem: 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.

Submission attempts (in order):

a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={len(r):r,len(g):g,len(b):b}
    tmp=sorted(min_len.keys())
    r=min_len[tmp[0]]
    g=min_len[tmp[1]]
    b=min_len[tmp[2]]
    for i in r:
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={r:0,g:1,b:2}
    if len(r)>len(g):
        tmp=min_len[r]
        min_len[r]=min_len[g]
        min_len[g]=tmp
    if len(g)>len(b):
        tmp=min_len[g]
        min_len[g]=min_len[b]
        min_len[b]=tmp
    if len(b)>len(r):
        tmp=min_len[b]
        min_len[b]=min_len[r]
        min_len[r]=tmp
    
    for key,value in min_len.items():
        if value==0:
            a1=key
        elif value==1:
            a2=key
        elif value==2:
            a3=key
    
    for i in a1:
        for j in a2:
            if -abs(i-j)+min(i,j) in a3:
                res-=1
            if abs(i-j)+max(i,j) in a3:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a3:
                res-=1
    print(res)

a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={len(r):r,len(g):g,len(b):b}
    tmp=list(min_len.keys()).sort()
    r=min_len[tmp[0]]
    g=min_len[tmp[1]]
    b=min_len[tmp[2]]
    for i in r:
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    A=set(a1[0])
    B=set(a1[1])
    C=set(a1[2])
    for i in A:
        for j in B:
            if -abs(i-j)+min(i,j) in C:
                res-=1
            if abs(i-j)+max(i,j) in C:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in C:
                res-=1
    print(res)


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted