Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n= int(input())
    slis = tuple(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]
 
    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB
 
    if lenB <= lenG <= lenR:
        for i in lis_R:
            for j in lis_G:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
 
    elif lenB <= lenR <= lenG:
        for i in lis_G:
            for j in lis_R:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
 
    elif lenG <= lenB <= lenR:
        for i in lis_R:
            for j in lis_B:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenG <= lenR <= lenB:
        for i in lis_B:
            for j in lis_R:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenB <= lenG:
        for i in lis_G:
            for j in lis_B:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenG <= lenB:
        for i in lis_B:
            for j in lis_G:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    print(total)
 
 
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n= int(input())
    slis = tuple(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]

    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB

    for i in lis_R:
        for j in lis_G:
            for k in lis_B:
                if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                    total -= 1
    print(total)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n= int(input())
    slis = list(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]

    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB

    if lenB <= lenG <= lenR:
        for i in lis_R:
            for j in lis_G:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1

    elif lenB <= lenR <= lenG:
        for i in lis_G:
            for j in lis_R:
                for k in lis_B:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1

    elif lenG <= lenB <= lenR:
        for i in lis_R:
            for j in lis_B:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenG <= lenR <= lenB:
        for i in lis_B:
            for j in lis_R:
                for k in lis_G:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenB <= lenG:
        for i in lis_G:
            for j in lis_B:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    elif lenR <= lenG <= lenB:
        for i in lis_B:
            for j in lis_G:
                for k in lis_R:
                    if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                        total -= 1
    print(total)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n= int(input())
    slis = list(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]

    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB

    for i in lis_R:
        for j in lis_G:
            for k in lis_B:
                if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                    total -= 1
    print(total)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()