Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
from heapq import heappush

def main():
    N=int(input())
    S=input()

    C={"R":[],"G":[],"B":[]}
    for i in range(N):
        heappush(C[S[i]],i)

    res=0

    flag1=0
    flag2=0
    for i in C["R"]:
        #j=>G
        if flag1==0:
            start_j=bisect_left(C["G"],i)
            if start_j!=len(C["G"]):
                for g_index in range(start_j,len(C["G"])):
                    j=C["G"][g_index]
                    start_k=bisect_left(C["B"],j)
                    if start_k!=len(C["B"]):
                        for b_index in range(start_k,len(C["B"])):
                            k = C["B"][b_index]
                            if j-i!=k-j:
                                res+=1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break

        #j=>B
        if flag2==0:
            start_j = bisect_left(C["B"], i)
            if start_j != len(C["B"]):
                for b_index in range(start_j, len(C["B"])):
                    j = C["B"][b_index]
                    start_k = bisect_left(C["G"], j)
                    if start_k != len(C["G"]):
                        for g_index in range(start_k, len(C["G"])):
                            k = C["G"][g_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break

    flag1=0
    flag2=0

    for i in C["G"]:
        # j=>R
        if flag1==0:
            start_j = bisect_left(C["R"], i)
            if start_j != len(C["R"]):
                for r_index in range(start_j, len(C["R"])):
                    j = C["R"][r_index]
                    start_k = bisect_left(C["B"], j)
                    if start_k != len(C["B"]):
                        for b_index in range(start_k, len(C["B"])):
                            k = C["B"][b_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break
        # j=>B
        if flag2==0:
            start_j = bisect_left(C["B"], i)
            if start_j != len(C["B"]):
                for b_index in range(start_j, len(C["B"])):
                    j = C["B"][b_index]
                    start_k = bisect_left(C["R"], j)
                    if start_k != len(C["R"]):
                        for r_index in range(start_k, len(C["R"])):
                            k = C["R"][r_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break

    flag1=0
    flag2=0
    for i in C["B"]:
        # j=>G
        if flag1==0:
            start_j = bisect_left(C["G"], i)
            if start_j != len(C["G"]):
                for g_index in range(start_j, len(C["G"])):
                    j = C["G"][g_index]
                    start_k = bisect_left(C["R"], j)
                    if start_k != len(C["R"]):
                        for r_index in range(start_k, len(C["R"])):
                            k = C["R"][r_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break

        # j=>R
        if flag2==0:
            start_j = bisect_left(C["R"], i)
            if start_j != len(C["R"]):
                for r_index in range(start_j, len(C["R"])):
                    j = C["R"][r_index]
                    start_k = bisect_left(C["G"], j)
                    if start_k != len(C["G"]):
                        for g_index in range(start_k, len(C["G"])):
                            k = C["G"][g_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break


    print(res)

if __name__=="__main__":
    main()








Fixed submission:
def main():
    N=int(input())
    S=input()

    C={"R":0,"G":0,"B":0}

    for i in range(N):
        C[S[i]]+=1

    excepter=0

    for i in range(N-1):
        for j in range(i+1,N):
            if S[i]==S[j]:
                continue
            k=j+(j-i)
            if k>N-1:
                break
            if S[i]==S[k] or S[j]==S[k]:
                continue
            excepter+=1

    res=C["R"]*C["G"]*C["B"]-excepter
    print(res)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
from heapq import heappush

def main():
    N=int(input())
    S=input()

    C={"R":[],"G":[],"B":[]}
    for i in range(N):
        heappush(C[S[i]],i)

    res=0

    flag1=0
    flag2=0
    for i in C["R"]:
        #j=>G
        start_j=bisect_left(C["G"],i)
        if start_j!=len(C["G"]):
            for g_index in range(start_j,len(C["G"])):
                j=C["G"][g_index]
                start_k=bisect_left(C["B"],j)
                if start_k!=len(C["B"]):
                    for b_index in range(start_k,len(C["B"])):
                        k = C["B"][b_index]
                        if j-i!=k-j:
                            res+=1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break

        #j=>B
        start_j = bisect_left(C["B"], i)
        if start_j != len(C["B"]):
            for b_index in range(start_j, len(C["B"])):
                j = C["B"][b_index]
                start_k = bisect_left(C["G"], j)
                if start_k != len(C["G"]):
                    for g_index in range(start_k, len(C["G"])):
                        k = C["G"][g_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break

    flag1=0
    flag2=0

    for i in C["G"]:
        # j=>R
        start_j = bisect_left(C["R"], i)
        if start_j != len(C["R"]):
            for r_index in range(start_j, len(C["R"])):
                j = C["R"][r_index]
                start_k = bisect_left(C["B"], j)
                if start_k != len(C["B"]):
                    for b_index in range(start_k, len(C["B"])):
                        k = C["B"][b_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break
        # j=>B
        start_j = bisect_left(C["B"], i)
        if start_j != len(C["B"]):
            for b_index in range(start_j, len(C["B"])):
                j = C["B"][b_index]
                start_k = bisect_left(C["R"], j)
                if start_k != len(C["R"]):
                    for r_index in range(start_k, len(C["R"])):
                        k = C["R"][r_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break

    flag1=0
    flag2=1
    for i in C["B"]:
        # j=>G
        start_j = bisect_left(C["G"], i)
        if start_j != len(C["G"]):
            for g_index in range(start_j, len(C["G"])):
                j = C["G"][g_index]
                start_k = bisect_left(C["R"], j)
                if start_k != len(C["R"]):
                    for r_index in range(start_k, len(C["R"])):
                        k = C["R"][r_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break

        # j=>R
        start_j = bisect_left(C["R"], i)
        if start_j != len(C["R"]):
            for r_index in range(start_j, len(C["R"])):
                j = C["R"][r_index]
                start_k = bisect_left(C["G"], j)
                if start_k != len(C["G"]):
                    for g_index in range(start_k, len(C["G"])):
                        k = C["G"][g_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break


    print(res)

if __name__=="__main__":
    main()








Fixed submission:
def main():
    N=int(input())
    S=input()

    C={"R":0,"G":0,"B":0}

    for i in range(N):
        C[S[i]]+=1

    excepter=0

    for i in range(N-1):
        for j in range(i+1,N):
            if S[i]==S[j]:
                continue
            k=j+(j-i)
            if k>N-1:
                break
            if S[i]==S[k] or S[j]==S[k]:
                continue
            excepter+=1

    res=C["R"]*C["G"]*C["B"]-excepter
    print(res)
if __name__=="__main__":
    main()