Problem p02714 (python)

Fixed submission:
n = int(input())
S = input()

R = {}
G = {}
B = {}

for i in range(n):
    if S[i] == 'R':
        R[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None

#print(R, G, B)

cnt = 0

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

for b in B:
    for g in G:
        if (b + g) / 2 in R:
            cnt -= 1

for r in R:
    for b in B:
        if (r + b) / 2 in G:
            cnt -= 1

for r in R:
    for g in G:
        if (r + g) / 2 in B:
            cnt -= 1
            
print(cnt)

Buggy submission (verdict: Runtime Error):
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)

#print(R, G, B)

cnt = 0
r = R[0]
g = G[0]
b = B[0]

iR = 0
iG = 0
iB = 0

def ccc(ia, ib, ic, BB, CC):
    cnt = 0
    #for aa in BB[:ib:-1]:
    for aa in BB[ib:]:
        if aa > ia:
            for aaa in CC[ic:]:
                if aaa > aa:
                    if (aa - ia) != (aaa - aa):
                        #print(ia, aa, aaa)
                        cnt += 1


    return cnt

for i in range(n):
    if S[i] == 'R':
        if iR < len(R) - 1:
            iR += 1

        cnt += ccc(i, iG, iB, G, B)
        cnt += ccc(i, iB, iG, B, G)

    elif S[i] == 'G':
        if iG < len(G) - 1:
            iG += 1

        cnt += ccc(i, iR, iB, R, B)
        cnt += ccc(i, iB, iR, B, R)

    else:
        if iB < len(B) - 1:
            iB += 1

        cnt += ccc(i, iG, iR, G, R)
        cnt += ccc(i, iR, iG, R, G)

    
print(cnt)

Buggy submission (verdict: Runtime Error):
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)

#print(R, G, B)

cnt = 0
r = R[0]
g = G[0]
b = B[0]

iR = 0
iG = 0
iB = 0

def ccc(ia, ib, ic, BB, CC):
    cnt = 0
    for aa in BB[ib:]:
        if aa > ia:
            for aaa in CC[ic:]:
                if aaa > aa and (aa - ia) != (aaa - aa):
                        #print(ia, aa, aaa)
                        cnt += 1
    return cnt

for i in range(n):
    if S[i] == 'R':
        if iR < len(R):
            iR += 1
            cnt += ccc(i, iG, iB, G, B)
            cnt += ccc(i, iB, iG, B, G)
        # else:
        #     print(iR)
    elif S[i] == 'G':
        if iG < len(G):
            iG += 1
            cnt += ccc(i, iR, iB, R, B)
            cnt += ccc(i, iB, iR, B, R)
        # else:
        #     print(iG)
    else:
        if iB < len(B):
            iB += 1
            cnt += ccc(i, iG, iR, G, R)
            cnt += ccc(i, iR, iG, R, G)
        # else:
        #     print(iB)
    
print(cnt)

Buggy submission (verdict: Runtime Error):
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)

#print(R, G, B)

cnt = 0
r = R[0]
g = G[0]
b = B[0]

iR = 0
iG = 0
iB = 0

def ccc(ia, ib, ic, BB, CC):
    cnt = 0
    for aa in BB[ib:]:
        if aa > ia:
            for aaa in CC[ic:]:
                if aaa > aa:
                    if (aa - ia) != (aaa - aa):
                        #print(ia, aa, aaa)
                        cnt += 1
    return cnt

for i in range(n):
    if S[i] == 'R':
        iR += 1
        cnt += ccc(i, iG, iB, G, B)
        cnt += ccc(i, iB, iG, B, G)
    elif S[i] == 'G':
        iG += 1
        cnt += ccc(i, iR, iB, R, B)
        cnt += ccc(i, iB, iR, B, R)      
    else:
        iB += 1
        cnt += ccc(i, iG, iR, G, R)
        cnt += ccc(i, iR, iG, R, G)
    
print(cnt)
# c = 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:
#                     continue
#                 if s[i] != s[k] and s[j] != s[k]:
#                     c += 1
# print(c)