Problem p02714 (python)

Fixed submission:
def binary_search(list,target):

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = int((left + right)/2)
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return result
	else:
		return result


if __name__=="__main__":
    n=int(input())
    s=list(input())
    rlist=[]
    glist=[]
    blist=[]
    ans=0
    if len(s)<=3:
        print(0)
        exit()
    for i in range(n):
        if s[i]=="R":
            rlist.append(i)
        elif s[i]=="G":
            glist.append(i)
        else:
            blist.append(i)
    if len(rlist)==0 or len(glist)==0 or len(blist)==0:
        print(0)
        exit()
    ans+=len(rlist)*len(glist)*len(blist)
    for i in range(n-2):
        for j in range(i+1,n-1):
            k=2*j-i
            if s[i]==s[j]:
                continue
            elif k>len(s)-1:
                continue
            else:
                if s[k]!=s[i] and s[k]!=s[j]:
                    ans-=1

    print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())
rlist=[]
glist=[]
blist=[]
ans=0
for i in range(n):
    if s[i]=="R":
        rlist.append(i)
    elif s[i]=="G":
        glist.append(i)
    else:
        blist.append(i)
if rlist[0] < glist[0] and glist[0]< blist[0]:
    for i in rlist:
        for j in glist:
            for k in blist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif glist[0] < rlist[0] and rlist[0]< blist[0]:
    for i in glist:
        for j in rlist:
            for k in blist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif glist[0] < blist[0] and blist[0]< rlist[0]:
    for i in glist:
        for j in blist:
            for k in rlist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif blist[0] < glist[0] and glist[0]< rlist[0]:
    for i in blist:
        for j in glist:
            for k in rlist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif blist[0] < rlist[0] and rlist[0]< glist[0]:
    for i in blist:
        for j in rlist:
            for k in glist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
else:
    for i in rlist:
        for j in blist:
            for k in glist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
print(ans)