Problem p03569 (python)

Fixed submission:
s = list(input())
count = 0
l = 0
r = len(s)-1

#リストsの先頭と末尾を比較
#先頭と末尾がx以外で異なるなら,-1を返す。
#先頭と末尾が同じなら先頭と末尾を移動させる。
#どちらか片方がxなら片方にxを追加し先頭と末尾を移動させたとするので最初にxだったほうだけ移動させる

#xabxa
while l != r:
    if s[l] != s[r]:
        if s[l] != "x" and s[r] != "x":
            count = -1
            break
        elif s[l] == "x" and s[r] != "x":
            l += 1
            count += 1
        elif s[r] == "x" and s[l] != "x":
            r -= 1
            count += 1
    elif s[l] == s[r]:
        if abs(l-r) == 1:
            break
        else:
            l += 1
            r -= 1

print(count)

Buggy submission (verdict: Runtime Error):
s = list(input())
count = 0
l = 0
r = len(s)-1

#リストsの先頭と末尾を比較
#先頭と末尾がx以外で異なるなら,-1を返す。
#先頭と末尾が同じなら先頭と末尾を移動させる。
#どちらか片方がxなら片方にxを追加し先頭と末尾を移動させたとするので最初にxだったほうだけ移動させる

#xabxa
while l != r:
    if s[l] != s[r]:
        if s[l] != "x" and s[r] != "x":
            count = -1
            break
        elif s[l] == "x" and s[r] != "x":
            l += 1
            count += 1
        elif s[r] == "x" and s[l] != "x":
            r -= 1
            count += 1
    elif s[l] == s[r]:
        l += 1
        r -= 1

print(count)

Buggy submission (verdict: Runtime Error):
from collections import Counter

S = input()
s = list(S)

counter = Counter(s)
odd = 0
flag = True

for k,v in counter.items():
    #偶奇性の判定
    if k != 'x':
        if v%2 == 1:
            odd += 1
            mid = k
    if odd >= 2:#奇数個ある小文字が複数ある場合できない
        flag = False

#"x"を除いた時の回文判定
if flag:
    s_sub = S.replace("x","")

    indexes = [i for i,x in enumerate(s_sub)if x == mid]
    center = indexes[len(indexes)//2 ]#奇数個ある数の中央のindexをとる
    if s_sub[center-1::-1] != s_sub[center+1:]:
        flag == False

#各アルファベットの距離判定
    if flag:
        indexes = [i for i,x in enumerate(s) if x == mid]
        center = indexes[len(indexes)//2 ]
        count = 0
        hidari = []
        migi = []

        for i in range(0,center)[::-1]:
            if s[i] == 'x':
                count += 1
            else:
                hidari.append(count)
                count = 0
    
        hidari.append(count)
        count = 0

        for i in range(center+1,len(s)):
            if s[i] == 'x':
                count += 1
            
            else:
                migi.append(count)
                count = 0

        migi.append(count)
        count = 0

        ans = 0
        for i in range(len(hidari)):
            ans += abs(hidari[i]-migi[i])
            
        print(ans)
 

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
from collections import Counter

S = input()
s = list(S)

counter = Counter(s)
odd = 0
flag = True

for k,v in counter.items():
    #偶奇性の判定
    if k != 'x':
        if v%2 == 1:
            odd += 1
            mid = k
    if odd >= 2:#奇数個ある小文字が複数ある場合できない
        flag = False

#"x"を除いた時の回文判定
if flag:
    s_sub = S.replace("x","")

    indexes = [i for i,x in enumerate(s_sub)if x == mid]
    center = indexes[len(indexes)//2 ]#奇数個ある数の中央のindexをとる
    if s_sub[center-1::-1] != s_sub[center+1:]:
        flag == False

#各アルファベットの距離判定
if flag:
    indexes = [i for i,x in enumerate(s) if x == mid]
    center = indexes[len(indexes)//2 ]
    count = 0
    hidari = []
    migi = []

    for i in range(0,center)[::-1]:
        if s[i] == 'x':
            count += 1
        else:
            hidari.append(count)
            count = 0
    
    hidari.append(count)
    count = 0

    for i in range(center+1,len(s)):
        if s[i] == 'x':
            count += 1
            
        else:
            migi.append(count)
            count = 0

    migi.append(count)
    count = 0

    ans = 0
    for i in range(len(hidari)):
        ans += abs(hidari[i]-migi[i])
            
    print(ans)
 

else:
    print(-1)