Problem:


Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and  abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx



Sample input:
xabxa


Sample output:
2

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    def check_palindrome(s):
        n = len(s)
        for i in range(n//2):
            if s[i] != s[n-1-i]:
                return False
        return True
    
    def find_center(s, cnt):
        num = 0
        n = len(s)
        for i in range(n):
            if s[i] != 'x':
                num += 1
            if num == cnt:
                return i

    def find_center_even(s, cnt):
        num = 0
        n = len(s)
        left, right = -1, -1
        for i in range(n):
            if s[i] != 'x':
                num += 1     
            if num == cnt:
                if left == -1:
                    left = i
                # print(f"left {left}")
            if num == cnt + 1:
                right = i
                # print(f"right {right}")
                return (left + right) / 2
    
    def not_x_split(s):
        buf = []
        s += 'E'
        n = len(s)
        x_cnt = 0
        for i in range(n):
            if s[i] != 'x':
                buf.append(x_cnt)
                x_cnt = 0
            else:
                x_cnt += 1
        return buf



    def solve(s, buf):
        n = len(s)
        bufsize = len(buf)
        if bufsize % 2 == 0:
            cnt = bufsize // 2
            ind = find_center_even(s, cnt)
        else:
            cnt = (bufsize + 1) // 2
            ind = find_center(s, cnt)
        # print(f"ind {ind}")
        if ind % 1 == 0:
            left_s = s[0:ind][::-1]
            right_s = s[ind+1:]
        else:
            left_s = s[0:ind//1+1][::-1]
            right_s = s[ind//1+1:]
        # print(left_s)
        # print(right_s)
        left = not_x_split(left_s)
        right = not_x_split(right_s)
        # print(left)
        # print(right)
        assert len(left) == len(right)
        ans = 0
        for i in range(len(left)):
            ans += max(left[i], right[i]) - min(left[i], right[i])
        return ans

    
    s = input()
    if check_palindrome(s):
        print(0)
    else:
        buf = [char for char in s if char != 'x']
        if not check_palindrome(buf):
            print(-1)
        else:
            # print('solve')
            print(solve(s, buf))



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def is_valid():
    x_removed_S = S.replace("x", "")
    M = len(x_removed_S)
    for i in range(M // 2):
        if x_removed_S[i] != x_removed_S[M - i - 1]:
            return False
    return True


S = input()
N = len(S)
if is_valid():
    ans = 0
    left, right = 0, N - 1
    while left != right:
        if S[left] == S[right]:
            left += 1
            right -= 1
            continue
        elif S[left] == "x":
            ans += 1
            left += 1
        else:
            ans += 1
            right -= 1
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
cou = 0
while len(s)!=1:
  a = s[0]
  b = s[-1]
  if a=='x' and b!='x':
    cou += 1
    s.popleft()
  elif a!="x" and b=='x':
    cou += 1
    s.pop()
  elif a=="x" and b=='x':
    s.popleft()
    s.pop()
  elif a==b:
    s.popleft()
    s.pop()
  else:
    print(-1)
    exit()
print(cou)

Submission status: Runtime Error

Solution code:
import sys

S = sys.stdin.readline().rstrip()
N = len(S)
not_x = []
x_cnt_idx = [0] * N
not_x_cnt_idx = [0] * N

for i, s in enumerate(S):
    if s != "x":
        not_x.append(s)
        not_x_cnt_idx[i] = not_x_cnt_idx[i - 1] + 1 if i > 0 else 1
    else:
        x_cnt_idx[i] = x_cnt_idx[i - 1] + 1 if i > 0 else 1

not_x_cnt = len(not_x)
x_cnt = N - not_x_cnt

if not_x_cnt == 0:
    ans = 0
    print(ans)
    exit()
elif not_x_cnt == 1:
    if N == 1:
        ans = 0
    else:
        x_left_cnt = x_cnt_idx[not_x_cnt_idx.index(1)]
        ans = abs(x_left_cnt - (x_cnt - x_left_cnt))
    print(ans)
    exit()

if not_x_cnt % 2 == 1:
    left = not_x[:not_x_cnt // 2]
    right = not_x[not_x_cnt // 2 + 1:][::-1]
else:
    left = not_x[:not_x_cnt // 2]
    right = not_x[not_x_cnt // 2:][::-1]

if left == right:
    if x_cnt == 0:
        ans = 0
    else:
        x_left_cnt = x_cnt_idx[not_x_cnt_idx.index(not_x_cnt // 2)]
        ans = abs(x_left_cnt - (x_cnt - x_left_cnt))
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(map(str, input().rstrip()))

cnt = 0
while s:
    if s[0] != s[-1]:
        if s[0] != "x" and s[-1] != "x":
            print(-1)
            sys.exit()
        elif s[0] == "x":
            tmp = s.popleft()
        else:
            tmp = s.pop()
        cnt += 1
    elif len(s) <= 1:
        break
    else:
        s.popleft()
        s.pop()

print(cnt)

Submission status: Runtime Error

Solution code:
s = input()

ans = 0

l, r = 0, len(s)-1
if len(s) != 1:
    while l != r:
        # print("l", l, "r", r)
        if s[l] == s[r]:
            l += 1
            r -= 1
        else:
            if s[l] == "x":
                l += 1
                ans += 1
            elif s[r] == "x":
                r -= 1
                ans += 1
            else:
                ans = -1
                break

print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
N = len(S)

cnt = 0
l = 0
r = N-1
while l<=r:
    if S[l]=="x" and S[r]!="x":
        l += 1
        cnt += 1
    if S[l]!="x" and S[r]=="x":
        r += -1
        cnt += 1
    if S[l]=="x" and S[r]=="x":
        l += 1
        r += -1
    if S[l]!="x" and S[r]!="x":
        if S[l]==S[r]:
            l += 1
            r += -1
        else:
            cnt = -1
            break
print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    S = deque(input())
    N = len(S)
    ans = 0
    if len(S) == 1:
        print(0)
        return
    left = S.popleft()
    right = S.pop()
    while True:
        if left == right:
            if len(S) >= 2:
                left = S.popleft()
                right = S.pop()
                continue
            else:
                break
            
        if left != 'x' and right!='x':
            print(-1)
            return

        ans += 1
        if left == 'x':
            if len(S)>0:
                left = S.popleft()
            else:
                break
        elif right == 'x':
            if len(S)>0:
                right = S.pop()
            else:
                break

    print(ans)



            

        

main()

Submission status: Runtime Error

Solution code:
S = input()
N = len(S)
ans = []
check = []
for i in range(N):
    if S[i] != 'x':
        ans.append(S[i])
        check.append(i)
if ans != ans[::-1]:
    print(-1)
    exit()
if len(ans)%2:
    cl,cr = check[len(ans)//2],check[len(ans)//2]
else:
    cl,cr = check[len(ans)//2-1],check[len(ans)//2]
cnt = 0
while 0 <= cl and cr <= N-1:
    if S[cl] == S[cr]:
        cl -= 1
        cr += 1
    elif S[cl] == 'x':
        cnt += 1
        cl -= 1
    else:
        cnt += 1
        cr += 1
cnt += max(cl+1,N-cr)
print(cnt)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

S = input()
N = len(S)
count = 0
is_ok = True

right_index = 0
left_index = N - 1

while right_index != left_index:

    if right_index == left_index:
        break

    if S[right_index] == S[left_index]:
        right_index += 1
        left_index -= 1
        continue
    if S[right_index] == "x":
        count += 1
        right_index += 1
        continue

    if S[left_index] == "x":
        count += 1
        left_index -= 1
        continue

    count = -1
    break

ans = count

print(ans)

Submission status: Runtime Error

Solution code:
s=list(input())

ch=[]
for i in range(len(s)):
    if s[i]!='x':
        ch.append(s[i])
        
error=0
for i in range(len(ch)//2):
    if ch[i]!=ch[len(ch)-i-1]:
        error=1
        break

if error==1:
    print('-1')
else:
    ans=0
    li=0
    ri=0
    for i in range(len(ch)//2+1):
        x=0
        while s[li]!=ch[i]:
            x += 1
            li += 1
        li += 1
        y=0
        while s[len(s)-1-ri]!=ch[len(ch)-1-i]:
            y += 1
            ri += 1
        ri += 1
        ans += abs(x-y)
    print(ans)

Submission status: Runtime Error

Solution code:
s=list(input())

ch=[]
for i in range(len(s)):
    if s[i]!='x':
        ch.append(s[i])
for i in range(len(ch)//2):
    if ch[i]!=ch[len(ch)-i-1]:
        print('-1')
        exit()

ans=0
li=0
ri=0
for i in range(len(ch)//2+1):
    x=0
    while s[li]!=ch[i]:
        x += 1
        li += 1
    li += 1
    y=0
    while s[len(s)-1-ri]!=ch[len(ch)-1-i]:
        y += 1
        ri += 1
    ri += 1
    ans += abs(x-y)
print(ans)

Submission status: Runtime Error

Solution code:
def solve(s):
    l, r = 0, len(s) - 1

    ans = 0
    while l != r:
        if s[l] != s[r] and s[l] != 'x' and s[r] != 'x':
            return -1
        if s[l] == s[r]:
            l += 1
            r -= 1
        elif s[l] == 'x':
            ans += 1
            l += 1
        elif s[r] == 'x':
            ans += 1
            r -= 1
    return ans


if __name__ == '__main__':
    print(solve(input()))

Submission status: Runtime Error

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n=len(s)
ch = 0
ans = 0
left=0
right=n-1
while left!=right:
    if s[left] == s[right]:
        left+=1
        right-=1
    else:
        if s[left] == 'x':
            ans+=1
            left+=1
        elif s[right] =='x':
            ans+=1
            right-=1
        else:
            print(-1)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = input().strip()
    N = len(s)
    l = -1
    r = N
    while l < r:
        l += 1
        r -= 1
        while s[l] == 'x':
            l += 1
        while s[r] == 'x':
            r -= 1
        if s[l] != s[r]:
            print(-1)
            exit()
    
    ans = 0
    while l != -1 or r != N:
        if l == -1:
            ans += N - r
            break
        if r == N:
            ans += l + 1
            break
        if s[l] == s[r]:
            l -= 1
            r += 1
            continue
        elif s[l] == 'x':
            ans += 1
            l -= 1
        elif s[r] == 'x':
            ans += 1
            r += 1
        
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = input().strip()
    N = len(s)
    l = -1
    r = N
    while l < r:
        l += 1
        r -= 1
        while s[l] == 'x':
            l += 1
        while s[r] == 'x':
            r -= 1
        if s[l] != s[r]:
            print(-1)
            exit()
    
    ans = 0
    if l > r:
        l, r = r, l
        if r - l % 2 != 0:
            ans += 1
    
    while l != -1 or r != N:
        if l == -1:
            ans += N - r
            break
        if r == N:
            ans += l + 1
            break
        if s[l] == s[r]:
            l -= 1
            r += 1
            continue
        elif s[l] == 'x':
            ans += 1
            l -= 1
        elif s[r] == 'x':
            ans += 1
            r += 1
        
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from math import ceil
s=list(input())
n=len(s)
l=[]
m=[]
buf=[]
for i in range(n):
    if s[i]!="x":
        l.append(s[i])
m=l[::-1]
#print(l,m)
left=[]
right=[]
ans=0
if m==l:
    flag=1
    for i in range(ceil(len(l)/2)):
        buf.append(s[i])
    piv=0
    cnt=0
    for i in range(n):
        if piv==len(buf):
            break
        if s[i]==buf[piv]:
            left.append(cnt)
            cnt=0
            piv=piv+1
        else:
            cnt=cnt+1
    piv=0
    cnt=0
    for i in range(n-1,-1,-1):
        if piv==len(buf):
            break
        if s[i]==buf[piv]:
            right.append(cnt)
            cnt=0
            piv=piv+1
        else:
            cnt=cnt+1
    #print(left,right)
    for i in range(len(left)):
        ans=ans+abs(left[i]-right[i])
    print(ans)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
s = input()

s_no_x = ""
for i in range(len(s)):
    if s[i] != "x":
        s_no_x += s[i]

if s_no_x != s_no_x[::-1]:
    print(-1)
    exit()

if len(s_no_x) % 2 == 0:
    non_x_count = 0
    for i in range(len(s)):
        if s[i] != "x":
            non_x_count += 1
        if non_x_count == len(s_no_x)//2:
            s1 = s[:i]
        if non_x_count == len(s_no_x)//2 + 1:
            s2 = s[i+1:]
            break
else:
    non_x_count = 0
    for i in range(len(s)):
        if s[i] != "x":
            non_x_count += 1
        if non_x_count == len(s_no_x)//2 + 1:
            s1 = s[:i]
            s2 = s[i+1:]
            break

ans = abs(len(s1) - len(s2))
for i in range(min(len(s1), len(s2))):
    if s[i] != s[-i-1]:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
#WA
s = input()
ns = ''
cnt = 0
cnt1 = 0
for i in range(len(s)):
    if s[i] != 'x':
        ns += s[i]
    else:cnt += 1

if ns != ns[::-1]:
    print(-1)
    exit()

mid = len(ns) // 2
mid_str = ns[mid]
mid_ind = s.index(mid_str)
#print(mid_ind)

count = 0
for i in range(len(s)):
    if s[i] != 'x':continue
    temp_ind = 2 * mid_ind - i
    if temp_ind >= len(s) or temp_ind < 0:continue
    if s[temp_ind] == 'x':
        count += 1

#print(cnt, count)
print(cnt - count * 2)

Submission status: Runtime Error

Solution code:
s=input()
d=s.replace("x","")
rd=d[::-1]
if d!=rd:print(-1)
else:
    ans=0
    x,y=0,len(s)-1
    while abs(x-y)>0:
        if s[x]==s[y]:x+=1;y-=1
        elif s[x]=="x":ans+=1;x+=1
        else:ans+=1;y-=1
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
ans = 0
r = 0
l = len(s)-1
while r < l:
  if s[r] == s[l]:
    r += 1
    l -= 1
  elif s[r] == "x":
    ans += 1
    r += 1
  elif s[l] == "x":
    rns += 1
    l -= 1
  else:
    print(-1)
    exit()

print(r)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    s = input()
    t = []
    for i,si in enumerate(s):
        if si != "x":
            t.append((i,si))
    q = [i[1] for i in t]
    if q != q[::-1]:
        print(-1)
        return
    n = len(t)
    m = n >> 1
    l = t[m-(1-(n&1))][0]
    r = t[m][0]
    ans = 0
    while l >= 0 or r < len(s):
        if l < 0:
            r += 1
            ans += 1
        elif r >= len(s):
            l -= 1
            ans += 1
        elif s[l] == s[r]:
            l -= 1
            r += 1
        elif s[l] == "x":
            l -= 1
            ans += 1
        else:
            r += 1
            ans += 1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)

def solve2():
    l = 0
    r = N - 1
    cost = 0
    while l < r:
        if s[l] == s[r]:
            l += 1
            r += 1
        elif s[l] == 'x':
            cost += 1
            l += 1
        elif s[r] == 'x':
            cost += 1
            r -= 1
        else:
            return -1
    return cost

print(solve2())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            # assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        # assert s[xl] == s_trim[pl]
        # assert s[xr] == s_trim[pr]
        # assert s[xl] == s[xr]
        # assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            # assert s[xl] == s_trim[tl]
            if s[xl] != s_trim[tl]:
                while True: pass
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        assert s[xl] == s_trim[pl]
        assert s[xr] == s_trim[pr]
        assert s[xl] == s[xr]
        assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        assert s[xl] == s_trim[pl]
        assert s[xr] == s_trim[pr]
        assert s[xl] == s[xr]
        # assert s_trim[pl] == s_trim[pr]
        if s_trim[pl] != s_trim[pr]:
            while True: pass
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        assert s[xl] == s_trim[pl]
        assert s[xr] == s_trim[pr]
        # assert s[xl] == s[xr]
        if s[xl] != s[xr]:
            while True: pass
        assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        assert s[xl] == s_trim[pl]
        #assert s[xr] == s_trim[pr]
        if s[xr] != s_trim[pr]:
            while True: pass
        assert s[xl] == s[xr]
        assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        # assert s[xl] == s_trim[pl]
        if s[xl] != s_trim[pl]:
            while True: pass
        assert s[xr] == s_trim[pr]
        assert s[xl] == s[xr]
        assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
s_trim = ''.join([c for c in s if c != 'x'])
N_trim = len(s_trim)

def check_anagram():
    for i in range(N_trim):
        j = N_trim - 1 - i
        if s_trim[i] != s_trim[j]:
            return False
    return True

def solve():
    if not check_anagram():
        return -1

    # Center of anagram
    pr = N_trim // 2
    pl = pr
    if N_trim % 2 == 0:
        pl -= 1

    xl = 0
    tl = 0
    while xl < N:
        if s[xl] != 'x':
            assert s[xl] == s_trim[tl]
            if tl == pl:
                break
            tl += 1
        xl += 1

    if N_trim % 2 == 1:
        xr = xl
    else:
        xr = xl + 1
        while s[xr] == 'x':
            xr += 1

    cost = 0
    while True:
        assert s[xl] == s_trim[pl]
        assert s[xr] == s_trim[pr]
        assert s[xl] == s[xr]
        assert s_trim[pl] == s_trim[pr]
        pl -= 1; pr += 1
        while True:
            xl -= 1; xr += 1
            if not (xl >= 0 and xr < N and s[xl] == 'x' and s[xr] == 'x'):
                break
        while xl >= 0 and s[xl] == 'x':
            cost += 1
            xl -= 1
        while xr < N and s[xr] == 'x':
            cost += 1
            xr += 1
        if pl < 0 and pr >= N_trim and xl < 0 and xr >= N:
            return cost

print(solve())