Problem p03569 (python)

Fixed submission:
from collections import deque

s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    d = deque(s)
    a, b = [], []
    cnt = 0
    x = y = ''
    x_skip = y_skip = 0
    while d:
        if not x_skip:
            x = d.popleft()
        if 0 < len(d)and not y_skip:
            y = d.pop()

        if y == '':
            break

        if x == y != 'x':
            cnt += abs(len(a)-len(b))
            x = y = ''
            a, b = [], []
            x_skip = y_skip = 0
            continue

        if x == 'x':
            a.append(x)
        else:
            x_skip = 1

        if y == 'x':
            b.append('x')
        else:
            y_skip = 1
    else:
        cnt += abs(len(a)-len(b))

    print(cnt)

Buggy submission (verdict: Runtime Error):
s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    center = s2[len(s2)//2]
    cnt = s.count(center)//2 + 1
    for i, c in enumerate(s):
        if c == center:
            cnt -= 1
        if cnt == 0:
            center_idx = i
            break

    front = list(s)[:center_idx]
    rear = list(s)[center_idx+1:][::-1]
    if len(front) == 0 or len(rear) == 0:
        print(max(len(front), len(rear)))
    else:
        a, b = front.pop(), rear.pop()
        cnt = 0
        while True:
            if a == b == 'x':
                if 0 < len(front) <= len(rear):
                    a = front.pop()
                    b = rear.pop()
                else:
                    if 0 == len(front) == len(rear):
                        break
                    if 0 < len(front):
                        a = front.pop()
                        cnt += 1
                    if 0 < len(rear):
                        b = rear.pop()
                        cnt += 1
                    continue

            if a == b:
                if 0 < len(front):
                    a = front.pop()
                if 0 < len(rear):
                    b = rear.pop()

            if a == 'x' and b != 'x':
                cnt += 1
                if 0 < len(front):
                    a = front.pop()
            if a != 'x' and b == 'x':
                cnt += 1
                if 0 < len(rear):
                    b = rear.pop()
            if len(front) == 0 and len(rear) == 0:
                break
        print(cnt)

Buggy submission (verdict: Runtime Error):
s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    center = s2[len(s2)//2]
    cnt = s.count(center)//2 + 1
    for i, c in enumerate(s):
        if c == center:
            cnt -= 1
        if cnt == 0:
            center_idx = i
            break

    front = list(s)[:center_idx]
    rear = list(s)[center_idx+1:][::-1]
    if len(front) == 0 or len(rear) == 0:
        print(max(len(front), len(rear)))
    else:
        a, b = front.pop(), rear.pop()
        cnt = 0
        while True:
            if a == b:
                if 0 < len(front):
                    a = front.pop()
                if 0 < len(rear):
                    b = rear.pop()
                continue
            if a == 'x' and b != 'x':
                cnt += 1
                if 0 < len(front):
                    a = front.pop()
            if a != 'x' and b == 'x':
                cnt += 1
                if 0 < len(rear):
                    b = rear.pop()
            if len(front) == 0 and len(rear) == 0:
                break
        print(cnt)