Problem p03569 (python)

Fixed submission:
from collections import deque

S = input()

d = deque(S)


left = 0
right = len(d) - 1

count = 0
while left < right:
    #print(f'{left=}, {right=}')
    if d[right] == d[left]:
        left += 1
        right -= 1
    else:
        if d[right] == 'x':
            count += 1
            right -= 1
        elif d[left] == 'x':
            count += 1
            left += 1
        else:
            count = -1
            print(-1)
            break

if count != -1:
    print(count)

Buggy submission (verdict: Runtime Error):
S = input()
remove_x = S.replace('x','')

s_len = len(S)
remove_x_len = len(remove_x)

if remove_x != remove_x[::-1]:
    print(-1)
else:
    if remove_x_len % 2 == 0:
        left = remove_x_len // 2
        center = -1
        right = left + 1
    else:
        left = remove_x_len // 2 
        center = left + 1
        right = left + 2
    
    left_index = 0
    right_index = 0
    center_index = 0

    left_x = []
    left_y = []

    count = 0
    for i, s in enumerate(S):
        if s != 'x':
            count += 1
        if count == left:
            left_index = i
            break
    count = 0
    for i, s in enumerate(S):
        if s != 'x':
            count += 1
        if count == right:
            right_index = i
            break
    count = 0
    for i, s in enumerate(S):
        if s != 'x':
            count += 1
        if count == center:
            center_index = i
            break


    #print(f'{left=}, {right=}')
    #print(f'{left_index=}, {right_index=}')
    tmp = 0
    for s in reversed(S[:left_index]):
        if s == 'x':
            tmp += 1
        else:
            left_x.append(tmp)
            tmp = 0
    left_x.append(tmp)

    tmp = 0
    for  s in S[right_index+1:]:
        if s == 'x':
            tmp += 1
        else:
            left_y.append(tmp)
            tmp = 0
    left_y.append(tmp)

    ans = 0
    for i in range(len(left_y)):
        ans += abs(left_x[i]-left_y[i])

    if remove_x_len % 2 != 0:
        ans += abs((right_index-center_index) - (center_index-left_index))
    
    print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
remove_x = S.replace('x','')

count = 0
index = 0
left = 0
right = 0
ans = 0
s_len = len(S)

if remove_x != remove_x[::-1]:
    print(-1)
else:
    if len(remove_x) % 2 == 0:
        center = len(remove_x) // 2
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index
        x_count = 0
        j = left+1
        while S[j] == 'x' and j < s_len:
            x_count += 1
            j += 1
        if x_count % 2 != 0:
            ans += 1
        right = j
        #print(f'{right=}')
    else:
        center = len(remove_x) // 2 + 1
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index - 1
        right = index + 1 


    #print(f'{left}, {right}')
    while left >= 0 and right < s_len:
        if S[left] != S[right]:
            if S[left] == 'x':
                ans += 1
                left -= 1
            elif S[right] == 'x':
                ans += 1
                right += 1
        else:
            left -= 1
            right += 1

    if right != s_len:
        ans += s_len - right
    else:
        ans += left + 1

    print(ans)