Problem p02714 (python)

Fixed submission:
#input
N = int(input())
S = str(input())

#output
from collections import Counter
answer = 0
if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = Counter(S[:i])["G"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["G"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["G"]
            c = Counter(S[:i])["G"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1

print(answer)

Buggy submission (verdict: Runtime Error):
#input
N = int(input())
S = str(input())

#output
from collections import Counter
answer = 0
if N <= 3:
    continue
else:
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if S[i] == "R":
                if S[j] == "G":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
            elif S[i] == "G":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
            elif S[i] == "B":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
                elif S[j] == "G":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
                
print(answer)

Buggy submission (verdict: Runtime Error):
#input
N = int(input())
S = str(input())

#output
from collections import Counter
answer = 0
if N <= 3:
    continue
else:
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if S[i] == "R":
                if S[j] == "G":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
            elif S[i] == "G":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
            elif S[i] == "B":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
                elif S[j] == "G":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
                
print(answer)