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:
from collections import Counter
s = input()
N = len(s)
sc = Counter(s)
del sc['x']

def trim_x():
    s_trim = ''.join([c for c in s if c != 'x'])
    for i in range(len(s_trim)):
        j = len(s_trim) - 1 - i
        if i >= j:
            break
        if s_trim[i] != s_trim[j]:
            return None
    return s_trim

def solve():
    s_trim = trim_x()  # anagram
    if s_trim is None:
        return -1
    pl = len(s_trim) // 2
    pr = pl
    if len(s_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 len(s_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 >= len(s_trim) and xl < 0 and xr >= N:
            return cost

print(solve())

Submission status: Runtime Error

Solution code:
from collections import Counter
s = input()
N = len(s)
sc = Counter(s)
del sc['x']

def trim_x():
    s_trim = [c for c in s if c != 'x']
    for i in range(len(s_trim)):
        j = len(s_trim) - 1 - i
        if i >= j:
            break
        if s_trim[i] != s_trim[j]:
            return None
    return s_trim

def solve():
    s_trim = trim_x()  # anagram
    if s_trim is None:
        return -1
    pl = len(s_trim) // 2
    pr = pl
    if len(s_trim) % 2 == 0:
        pr += 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
    xr = xl + (0 if len(s_trim) % 2 == 1 else 1)
    while s[xr] != s_trim[pr]:
        assert 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 >= len(s_trim) and xl < 0 and xr >= N:
            return cost
    raise RuntimeError('never reach here')

print(solve())

Submission status: Runtime Error

Solution code:
def solve():
    S = input()
    N = len(S)

    left = 0
    right = N-1

    ans = 0
    while left != right:
        if S[left] == S[right]:
            left += 1
            right -= 1
        else: 
            if S[left] == 'x':
                left += 1
                ans += 1
            elif S[right] == 'x':
                right -= 1
                ans += 1
            else:
                print('-1')
                return
    
    print(ans)
                
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
from collections import defaultdict #d = defaultdict(int) d[key] += value

def main():
    S = input()
    l = len(S)
    T = S.replace('x', '')
    if T != T[::-1]:
        print(-1)
        exit()
    else:
        al = T[len(T)//2]
    pos_list = []
    for i in range(l):
        if S[i] == al:
            pos_list.append(i)
    cnt = 0
    if len(pos_list)%2 == 1:
        mid = pos_list[len(pos_list)//2]
        if mid>0:
            st = mid-1
        else:
            st=0
        if mid<l-1:
            end = mid+1
        else:
            end = l-1
    else:
        mids = pos_list[len(pos_list)//2-1 : len(pos_list)//2+1]
        if mids[0]>0:
            st = mids[0]-1
        else:
            st = 0
        if mids[1]<l-1:
            end = mids[1]+1
        else:
            end = l-1
    flag = 0
    while True:
        if st==0 and end==l-1:
            flag = 1
        if S[st] == S[end]:
            if st > 0:
                st -= 1
            if end < l-1:
                end += 1
        elif S[st] == 'x':
            if st > 0:
                st -= 1
            cnt += 1
        elif S[end] == 'x':
            if end < l-1:
                end += 1
            cnt += 1
        if flag:
            break
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
def main():

    s = deque(input())

    

    ans = 0
    while s!=[]:
        if s[0]==s[-1]:
            s.popleft()
            if len(s)>0:
                s.pop()
            else:
                break
            continue
        if s[0]!='x' and s[-1]!='x':
            ans = -1
            break
        if s[0]=='x':
            ans+=1
            s.popleft()
        if s[-1]=='x':
            ans+=1
            s.pop()
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
s=input()
S=""
for i in range(len(s)):
    if s[i]=="x":
        continue
    S=S+s[i]
rev=""
n=len(S)//2
for i in range(n):
    rev=rev+S[len(S)-1-i]
#print(S)
#print(rev)
flag=True
for i in range(n):
    if S[i]==rev[i]:
        pass
    else:
        flag=False
if flag:
    pass
else:
    print(-1)
    exit()

ky=S[n]
count=0
for i in range(n):
    if S[i]==ky:
        count+=1
count+=1
xcount=0
tmp=0
num=-1
for i in range(len(s)):
    if s[i]==ky:
        tmp+=1
        if tmp==count:
            num=i
            break
for i in range(len(s)):
    if s[i]=="x":
        xcount+=1

eat=0
noteat=0
for i in range(num):
    if (num-1-i)<0 or (num+1+i)>=(len(s)):
        break

    if s[num-1-i]=="x" or s[num+1+i]=="x":
        if s[num-1-i]==s[num+1+i]:
            eat+=1
        else:
            noteat+=1
ans=(xcount-eat)
ans=max(0,ans)
print(ans)

Submission status: Runtime Error

Solution code:
s=list(input())
s1=[]
for i in range(len(s)):
  if s[i]!="x":
    s1.append(s[i])
s2=s1[::-1]
if s1!=s2:
  print(-1)
  exit()
A1=[0]*10**5
A2=[0]*10**5
i=0
j=0
while i<=len(s1)//2:
  if s[j]=="x":
    A1[i]+=1
    j=j+1
  else:
    i=i+1
    j=j+1
i=0
j=0
while i<=len(s1)//2:
  if s[-1-j]=="x":
    A2[i]+=1
    j=j+1
  else:
    i=i+1
    j=j+1
ans=0
for i in range(10**5):
  ans=ans+abs(A1[i]-A2[i])
print(ans)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
cntf = 0
cnte = 0
med = t[len(t) // 2]
medidx = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        i += 1
        j -= 1
    elif s[i] == 'x':
        i += 1
        ans += 1
    elif s[j] == 'x':
        j -= 1
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def base_10_to_n(X, n):
    if int(X / n):
        return base_10_to_n(int(X/n), n) + str(X % n)
    return str(X % n)


def main():
    A = I()

    for i in range(10, 10000+1):
        print(i, base_10_to_n(A, i))
        if str(i) == base_10_to_n(A, i):
            print(i)
            return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# C


S = input()
N = len(S)
# 回分判定
ss = ""
for s in S:
    if s == 'x':
        pass
    else:
        ss += s
if ss != ss[::-1]:
    print(-1)
    exit()

# 真ん中を決める
# x の位置と個数を求める
n = len(ss)
if n % 2 == 0:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:
                cnt = j+1
                break
        if i == n//2-1:
            left_center = cnt-1
    right_center = cnt-1
    # 真ん中の分を先に計算する
    ans = (abs(right_center - left_center) % 2)
    # centerから両側に展開していく
    count = 0
    x_left = []
    x_right = []
    for i in range(left_center):
        if S[left_center-1 - i] == 'x':
            count += 1
        else:
            x_left.append(count)
            count = 0
    x_left.append(count)
    count = 0
    for i in range(right_center+1, N, 1):
        if S[i] == 'x':
            count += 1
        else:
            x_right.append(count)
            count = 0
    x_right.append(count)
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i] - x_right[i])
    print(ans)
else:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:       # S[j] != "x" なら ss[i] と S[j] は一致する
                cnt = j + 1
                break
    center = cnt-1
    # print(center)
    # center から両側に展開していき，center からの位置と x の個数を求める
    distans = 0     # x 以外の文字が出るたびに更新する
    count = 0
    x_left = []
    x_right = []
    for i in range(center):
        if S[center-1 - i] == "x":
            count += 1
        else:
            x_left.append([distans, count])
            distans += 1
            count = 0
    x_left.append([distans, count])
    distans = 0
    count = 0
    for i in range(center+1, N):
        if S[i] == "x":
            count += 1
        else:
            x_right.append([distans, count])
            distans += 1
            count = 0
    x_right.append([distans, count])
    # print(x_left)
    # print(x_right)
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i][1] - x_right[i][1])
    print(ans)

Submission status: Runtime Error

Solution code:
s=input()
li=list(s)

i=0
j=len(s)-1
answer=0

while i!=j:
    if li[i]==li[j]:
        i+=1
        j-=1
    else:
        if li[i]!="x" and li[j]!="x":
            answer=-1
            break
        elif li[i]=="x" and li[j]!="x":
            answer+=1
            i+=1
        elif li[i]!="x" and li[j]=="x":
            answer+=1
            j-=1

print(answer)

Submission status: Runtime Error

Solution code:
s = input()
S = []
xid = [0] * (10 ** 5)
xcnt = 0
for c in s:
    if c != "x":
        S.append(c)
        xcnt += 1
    else:
        xid[xcnt] += 1
xid = xid[:xcnt + 1]
ans = 0
if S[0:xcnt // 2] == S[-1:-(xcnt // 2) - 1:-1]:
    for i in range(-(-xcnt // 2)):
        ad = xid[i] - xid[xcnt - i]
        if ad < 0:
            ad = -ad
        ans += ad
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
s = input()
sr = s[-1::-1]
left = 0
right = 0
ans = 0
while (1):
    if left + right == len(s) - 1:
        break
    if s[left] != sr[right]:
        if s[left] == 'x':
            left += 1
        elif sr[right] == 'x':
            right += 1
        else:
            print(-1)
            exit()
        ans += 1
    elif s[left] == sr[right]:
        left += 1
        right += 1

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
midlle=le[len(le)//2]
ans =[]
for i in lex:
  ans.append(abs(i-midlle))
ans = list(set(ans))
print(len(ans))

Submission status: Runtime Error

Solution code:
s = input()
s1 = []
le = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
print(le[len(le)//2])

Submission status: Runtime Error

Solution code:
S = input()
S = list(S)
S_no_x = []
def is_kaibun(kaibun):
    for i in range(len(kaibun)//2):
        if kaibun[i] != kaibun[-i-1]:
            return False
    return True
inds = []
for i,c in enumerate(S):
    if c != "x":
        S_no_x.append(c)
        inds.append(i)
if not is_kaibun(S_no_x):
    print(-1)
else:
    l = len(S_no_x)



    mae = ""
    usiro = ""
    if l % 2 == 0:
        mae_ind = inds[l // 2 - 1]
        usiro_ind = inds[l // 2]
        mae = S[:mae_ind +1]
        usiro = S[usiro_ind :]
    else:
        center = inds[l // 2]
        mae = S[:center]
        usiro = S[center +1:]

    usiro.reverse()
    usiro_x = []
    mae_x = []
    cot = 0
    usiro.append("@")
    for c in usiro:
        if c == "x":
            cot += 1
        else:
            usiro_x.append(cot)
            cot = 0
    cot = 0
    mae.append("@")
    for c in mae:
        if c == "x":
            cot += 1
        else:
            mae_x.append(cot)
            cot = 0
    ans = 0
    for a,b in zip(mae_x,usiro_x):
        ans += abs(a-b)
    print(ans)

Submission status: Runtime Error

Solution code:
S=input()
T=S.replace("x","")
if all(s==t for s,t in zip(T,T[::-1])):
    ans=0
    i=0
    j=len(S)-1
    while i<j:
        pi=i
        pj=j
        while S[i]=="x":
            i+=1
        while S[j]=="x":
            j-=1
        ans+=abs(i-pi-j+pj)
        i+=1
        j-=1
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
s = input()
count = 0
if len(set(s)) == 1:
    print(0)
    exit()
left = 0
right = len(s) - 1

ans = 0
while True:
    if (s[left] == 'x' and s[right] == 'x') or s[left] == s[right]:
        left += 1
        right -= 1
    elif s[left] == 'x' and s[right] != 'x':
        ans += 1
        left += 1
    elif s[left] != 'x' and s[right] == 'x':
        ans += 1
        right -= 1
    elif s[left] != 'x' and s[right] != 'x' and s[left] != s[right]:
        print(-1)
        exit()
    if left == right:
        break
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
rev_s = s[::-1]
count = 0
if len(set(s)) == 1:
    print(0)
    exit()
left = 0
right = len(s) - 1

ans = 0
while True:
    if (s[left] == 'x' and s[right] == 'x') or s[left] == s[right]:
        left += 1
        right -= 1
    if s[left] == 'x' and s[right] != 'x':
        ans += 1
        left += 1
    elif s[left] != 'x' and s[right] == 'x':
        ans += 1
        right -= 1
    elif s[left] != 'x' and s[right] != 'x' and s[left] != s[right]:
        print(-1)
        exit()
    if left == right:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  i_f=0
  i_b=len(s)-1

  ans=0
  while True:
    if i_f==i_b:
      return ans

    if s[i_f]==s[i_b]:
      i_f+=1
      i_b-=1
    else:
      if s[i_f]=='x':
        i_f+=1
        ans+=1
      elif s[i_b]=='x':
        i_b-=1
        ans+=1
      else:
        return -1

# main()
print(main())

Submission status: Runtime Error

Solution code:
s = input()
n = len(s)
t = s.replace("x","")
if not t:
  print(0)
  exit()
l = len(t)
if t != t[::-1]:
  print(-1)
  exit()
plc = [-1]
for i in range(n):
  if s[i] != x:
    plc.append(i)
plc.append(n)
l = len(plc)
if l%2 == 0:
  fm = plc[:l//2]
  lt = plc[l//2:]
else:
  fm = plc[l//2+1]
  lt = plc[l//2:]
lt = lt[::-1]
ans = 0
for i in range(1,len(fm)):
  ans += abs((fm[i]-fm[i-1])-(lt[i]-lt[i-1]))
print(ans)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

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

# 回文の判定
def judge(s):
    n = len(s)
    half = n//2
    for i in range(half):
        if s[i] != s[n-1-i]:
            return False
    return True

removeX = S.replace("x", "")
n_removex = len(removeX)
if N % 2 == 0:
    center = removeX[n_removex//2-1:n_removex//2+1]
else:
    center = removeX[n_removex//2:n_removex//2+1]
left, right = S.split(center)
left_idx = 0
right_idx = len(right) - 1
ret = 0
while left_idx < len(left) and right_idx > -1:
    if left[left_idx] != right[right_idx]:
        if left[left_idx] == "x":
            right_idx -= 1
            ret += 1
        elif right[right_idx] == "x":
            left_idx += 1
            ret += 1
    else:
        left_idx += 1
        right_idx -= 1

ret += len(left) - left_idx
ret += right_idx + 1
if judge(removeX):
    print(ret)
else:
    print(-1)

Submission status: Runtime Error

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

# 回文の判定
def judge(s):
    n = len(s)
    half = n//2
    for i in range(half):
        if s[i] != s[n-1-i]:
            return False
    return True

removeX = S.replace("x", "")
n_removex = len(removeX)
if N % 2 == 0:
    center = removeX[n_removex//2-1:n_removex//2+1]
else:
    center = removeX[n_removex//2:n_removex//2+1]
left, right = S.split(center)
left_idx = 0
right_idx = len(right) - 1
ret = 0
while left_idx < len(left) and right_idx > -1:
    if left[left_idx] != right[right_idx]:
        if left[left_idx] == "x":
            right_idx -= 1
            ret += 1
        if right[right_idx] == "x":
            left_idx += 1
            ret += 1
    else:
        left_idx += 1
        right_idx -= 1

ret += len(left) - left_idx
ret += right_idx + 1
if judge(removeX):
    print(ret)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
s = readline().rstrip().decode('utf-8')

t = ""
for i in s:
    if not i == "x":
        t += i

if t[::] != t[::-1]:
    print(-1)

else:
    ans = 0
    left,right = 0,len(s)-1
    while True:
        if left == right:
            break
        if s[left] == "x" and s[right] != "x":
            ans += 1
            left += 1
        elif s[left] != "x" and s[right] == "x":
            ans += 1
            right -= 1
        else:
            left += 1
            right -= 1
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(i<len(s) and s[i]=='x'):
        i+=1
        xi+=1
#        print(i,xi)

    while(j>=0 and s[j]=='x'):
        j-=1
        yj+=1
#        print(j,yj)

#    print(i,xi,j,yj)
    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(s[i]=='x'):
        i+=1
        xi+=1
#        print(i,xi)

    while(s[j]=='x'):
        j-=1
        yj+=1
#        print(j,yj)

#    print(i,xi,j,yj)
    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(s[i]=='x'):
        i+=1
        xi+=1
#        print(i,xi)

    while(s[j]=='x'):
        j-=1
        yj+=1
#        print(j,yj)

    print(i,xi,j,yj)
    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(s[i]=='x'):
        i+=1
        xi+=1
        print(i,xi)

    while(s[j]=='x'):
        j-=1
        yj+=1
        print(j,yj)

    print(i,xi,j,yj)
    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
l = 0
r = len(s) - 1

cnt = 0
while l != r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        l += 1
        cnt += 1
    elif s[r] == 'x':
        r -= 1
        cnt += 1
    else:
        print(-1)
        sys.exit()

print(cnt)

Submission status: Runtime Error

Solution code:
import sys
s=input()

s_nox=[]
x=[]
xcount=0
for si in list(s):
    if(si!='x'):
        x.append(xcount)
        xcount=0
        s_nox.append(si)
    else:
        xcount+=1
else:
    x.append(xcount)


for i in range((len(s_nox)//2)+1):
    j=-1*i -1
    if(s_nox[i] != s_nox[j]):
        print(-1)
        sys.exit()


ans=0
for i in range(len(x)//2):
    j=-1*i -1
    ans+=abs(x[i]-x[j])

if(len(x)%2==1 and x[len(x)//2]%2==1):
    ans+=1

print(ans)