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: Wrong Answer

Solution code:
S=input()
flag=0
ans=0
for n in range(len(S)-1):
    if S[n] != S[-n-1]:
        if S[n] == 'x':
            ans+=1
            S=S+'x'
        elif S[-n-1] == 'x':
            ans+=1
            S='x'+S
        else:
            flag=1
            break
if flag==1:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = deque(list(input()))

ans = 0
while len(s) > 1:
    l = s.popleft()
    r = s.pop()
    if l == r:
        continue
    elif l == 'x':
        ans += 1
        s.append(r)
    elif r == 'x':
        ans += 1
        s.append(l)
    else:
        print(-1)
        exit()

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
count = 0
for i in range(n):
    if s[i] != "x":
        count += 1
    if a%2 and count == a - a//2:
        l = i-1
        r = i+1
        break
    elif a%2 == 0:
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
ans = 0
c1 = a//2
while c1 > 0:
    lc = 0
    while l >= 0 and s[l] == "x":
        l -= 1
        lc += 1
    rc = 0
    while r < n and s[r] == "x":
        r += 1
        rc += 1
    if s[l] == s[r]:
        ans += abs(lc-rc)
        l -= 1
        r += 1
    else:
        print(-1)
        exit()
    c1 -= 1
ans += abs(n-r-(l+1))
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
count = 0
for i in range(n):
    if s[i] != "x":
        count += 1
    if a%2 and count == a - a//2:
        l = i-1
        r = i+1
        break
    elif a%2 == 0:
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
ans = 0
c1 = a//2
while c1 > 0:
    lc = 0
    while l >= 0 and s[l] == "x":
        l += 1
        lc += 1
    rc = 0
    while r < n and s[r] == "x":
        r += 1
        rc += 1
    if s[l] == s[r]:
        ans += abs(lc-rc)
        l -= 1
        r += 1
    else:
        print(-1)
        exit()
    c1 -= 1
ans += abs(n-r-(l+1))
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
count = 0
for i in range(n):
    if s[i] != "x":
        count += 1
    if a%2 and count == a - a//2:
        l = i-1
        r = i-1
        break
    elif a%2 == 0:
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
ans = 0
c1 = a//2
while c1 > 0:
    lc = 0
    while l >= 0 and s[l] == "x":
        l += 1
        lc += 1
    rc = 0
    while r < n and s[r] == "x":
        r += 1
        rc += 1
    if s[l] == s[r]:
        ans += abs(lc-rc)
        l -= 1
        r += 1
    else:
        print(-1)
        exit()
    c1 -= 1
ans += abs(n-r-(l+1))
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
if a%2:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a-a//2:
            ind = i
            break
    count = 0
    l = ind-1
    r = ind+1
    c1 = a//2
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    lc = 0
    while l >= 0:
        l -= 1
        lc += 1
    rc = 0
    while r < n:
        r += 1
        rc += 1
    count += abs(rc-lc)
    print(count)
else:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
    count = 0
    if s[l] != s[r]:
        print(-1)
        exit()
    c1 = a//2-1
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    lc = 0
    while l >= 0:
        l -= 1
        lc += 1
    rc = 0
    while r < n:
        r += 1
        rc += 1
    count + abs(rc-lc)
    print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
if a%2:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a-a//2:
            ind = i
            break
    count = 0
    l = ind-1
    r = ind+1
    c1 = a//2
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    lc = 0
    while l >= 0:
        l -= 1
        lc += 1
    rc = 0
    while r < n:
        r += 1
        rc += 1
    count + abs(rc-lc)
    print(count)
else:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
    count = 0
    if s[l] != s[r]:
        print(-1)
        exit()
    c1 = a//2-1
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    lc = 0
    while l >= 0:
        l -= 1
        lc += 1
    rc = 0
    while r < n:
        r += 1
        rc += 1
    count + abs(rc-lc)
    print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
x = s.count("x")
a = n-x
if a == 0:
    print(0)
    exit()
if a%2:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a-a//2:
            ind = i
            break
    count = 0
    l = ind-1
    r = ind+1
    c1 = a//2
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    count += abs(n-r-(l+1))
    print(count)
else:
    count = 0
    for i in range(n):
        if s[i] != "x":
            count += 1
        if count == a//2:
            l = i
        elif count == a//2+1:
            r = i
            break
    count = 0
    if s[l] != s[r]:
        print(-1)
        exit()
    c1 = a//2-1
    while c1 > 0:
        lc = 0
        while l >= 0 and s[l] == "x":
            l += 1
            lc += 1
        rc = 0
        while r < n and s[r] == "x":
            r += 1
            rc += 1
        if s[l] == s[r]:
            count += abs(lc-rc)
            l -= 1
            r += 1
        else:
            print(-1)
            exit()
        c1 -= 1
    count += abs(n-r-(l+1))
    print(count)

Submission status: Wrong Answer

Solution code:
l=a=0;r=len(s:=input())-1;c='x'
while l<r:
  L,R=s[l],s[r]
  X=(L==R)
  L=(L==c)
  R=(R==c)
  l+=(L+X>0)
  r-=(R+X>0)
  a+=(~X and (L+R>0))
  if X+L+R<1:a=-1;break
print(a)

Submission status: Wrong Answer

Solution code:
A=list(input())
from collections import deque
A=deque(A)
cnt=0
while len(A)>1:
   if A[0]==A[-1]:
      A.pop()
      A.popleft()
   elif (A[0]!="x" and A[-1]=="x" ):
      cnt+=1
      A.pop()
   elif (A[0]=="x" and A[-1]!="x" ):
      cnt+=1
      A.popleft()
   else:
      print(-1,A)
      exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
import re
s = input()
s1 = re.sub('x', '', s)
num = len(s)
num1 = len(s1)

if s1 == s1[::-1]:
    print(num-num1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import math

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

 #ランレングス圧縮
def rle_comp(S):
     rle = []
     pre = '@'
     chain = 1
     for c in S:
         if c == pre:
             chain += 1
         else:
             if pre != '@':
                 rle.append([pre,chain])
             
             pre = c
             chain = 1
     rle.append([pre,chain])
 
     #print(rle)
     return rle

s = input()
t = [i for i in s if i != 'x']
#print(t)
h = len(t)//2

if len(s) == 1:
    print(0)
else:
    r_t = t[::-1]
    #print(t[:h])
    #print(r_t[:h])
    if t[:h] == r_t[:h]:
        #rleが必要。そうしないと半分で折れない
        rle_s = rle_comp(s)
        pre = list(rle_s[:len(rle_s)//2])
        post = list(rle_s[::-1])
        post = post[:len(rle_s)//2]
        i = 0
        j = 0
        ans = 0
        #print(pre)
        #print(post)
        while i != len(rle_s)//2 and j != len(rle_s)//2:
            #print(i,j)
            if pre[i][0] != post[j][0]:
                if pre[i][0] == 'x':
                    ans += pre[i][1]
                    i += 1
                elif post[j][0] == 'x':
                    ans += post[j][1]
                    j += 1
            else:
                if pre[i][0] == 'x':
                    ans += abs(pre[i][1] - post[j][1])
                i += 1
                j += 1
        if len(pre) != i and h != 0:
            ans += pre[i][1]
        if len(post) != j and h != 0:
            ans += post[j][1]

        #print(i,j)
        print(ans)

    else:
        print("-1")

Submission status: Wrong Answer

Solution code:
s=[ord(c) for c in input()]
x=ord('x')
n=len(s)

wox=[c for c in s if c!=x]

left=0
right=len(wox)-1

while left<right:
    if wox[left]!=wox[right]:
        print(-1)
        exit(0)

    left+=1
    right-=1
i=0
cntx=[]

for notx in wox+[0]:
    cnt=0
    while i<n and s[i]!=notx:
        cnt+=1 if s[i]==x else 0
        i+=1
    
    cntx.append(cnt)
left=0
right=len(cntx)-1
ans=0
while left<right:
    ans+=abs(cntx[left]-cntx[right])
    left+=1
    right-=1
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

S = input()
C = Counter(S)

Keys = tuple(C.keys())

cnt = 0
for k in Keys:
    if k != "x" and C[k]%2:
        cnt += 1
        
if not cnt % 2:
    print(-1)
else:
  	print(C["x"])

Submission status: Wrong Answer

Solution code:
s=list(input())
s2=[]
num=0
num2=0
for i in range(len(s)):
    if s[-i-1]!="x":
        num=i
        break
for i in range(len(s)):
    if s[i]!="x":
        num2=i
        break
if num>num2:
    num-=num2
    num2=0
elif num<num2:
    num2-=num
    num=0
s3=["O"]*num
for i in range(len(s)):
    if s[i]!="x":
        s2.append(s[i])
        if i!=len(s)-1:
            if s[i+1]!="x":
                s3.append("O")
    else:
        s3.append("x")
for i in range(num2):
    s3.append(["O"])
num=0
for i in range(len(s2)//2):
    if s2[i]!=s2[-1-i]:
        num=1
        break
if num==1:
    print(-1)
else:
    ans=0
    for i in range(len(s3)//2):
        if s3[i]!=s3[-i-1]:
            ans+=1
    print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
s2=[]
num=0
for i in range(len(s)):
    if s[-i-1]!="x":
        num=i+1
        break
s3=["O"]*num
for i in range(len(s)):
    if s[i]!="x":
        s2.append(s[i])
        if i!=len(s)-1:
            if s[i+1]!="x":
                s3.append("O")
    else:
        s3.append("x")
num=0
for i in range(len(s2)//2):
    if s2[i]!=s2[-1-i]:
        num=1
        break
if num==1:
    print(-1)
else:
    ans=0
    for i in range(len(s3)//2):
        if s3[i]!=s3[-i-1]:
            ans+=1
    print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
temp=""
for c in s:
     if c!="x": temp+=c
if temp!=temp[::-1]: print(-1)
elif len(temp)==0:  print(0)
else:
    pass

Submission status: Wrong Answer

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

    nox = []
    idxs = []
    ans = 0
    for i, c in enumerate(s):
        if c != "x":
            nox.append(c)
            idxs.append(i)
    
    if nox != nox[::-1]:
        print(-1)
        exit()
    
    if len(nox) == 0:
        print(0)
        exit()

    if len(nox)%2 == 0:
        left = len(nox)//2 - 1
        right = len(nox)//2
        
        left_idx = idxs[left]
        right_idx = idxs[right]
        print(left_idx, right_idx, ans)

    else:
        center = len(nox)//2
        left_idx = idxs[center]
        right_idx = idxs[center]

    while not(left_idx == 0 and right_idx == N-1):
        if s[left_idx] == s[right_idx]:
            if left_idx > 0 and right_idx < N - 1:
                left_idx -= 1
                right_idx += 1
            else:
                if left_idx <= idxs[0] and right_idx >= idxs[-1] and (left_idx > 0 or right_idx < N - 1):
                    ans += 1
                if left_idx > 0:
                    left_idx -= 1
                if right_idx < N-1:
                    right_idx += 1

        elif s[left_idx] == "x":
            ans += 1
            if left_idx > 0:
                left_idx -= 1
        elif s[right_idx] == "x":
            ans += 1
            if right_idx < N-1:
                right_idx += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    nox = []
    idxs = []
    ans = 0
    for i, c in enumerate(s):
        if c != "x":
            nox.append(c)
            idxs.append(i)
    
    if nox != nox[::-1]:
        print(-1)
        exit()
    
    if len(nox) == 0:
        print(0)
        exit()

    if len(nox)%2 == 0:
        left = len(nox)//2 - 1
        right = len(nox)//2
        
        left_idx = idxs[left]
        right_idx = idxs[right]
        print(left_idx, right_idx, ans)

    else:
        center = len(nox)//2
        left_idx = idxs[center]
        right_idx = idxs[center]
        
    while not(left_idx == 0 and right_idx == N-1):
        if s[left_idx] == s[right_idx]:
            if left_idx > 0 and right_idx < N - 1:
                left_idx -= 1
                right_idx += 1
            else:
                if left_idx <= idxs[0] and right_idx >= idxs[-1] and (left_idx > 0 or right_idx < N - 1):
                    ans += 1
                if left_idx > 0:
                    left_idx -= 1
                if right_idx < N-1:
                    right_idx += 1

        elif s[left_idx] == "x":
            ans += 1
            if left_idx > 0:
                left_idx -= 1
        elif s[right_idx] == "x":
            ans += 1
            if right_idx < N-1:
                right_idx += 1

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    nox = []
    idxs = []
    ans = 0
    for i, c in enumerate(s):
        if c != "x":
            nox.append(c)
            idxs.append(i)
    
    if nox != nox[::-1]:
        print(-1)
        exit()
    
    if len(nox) == 0:
        print(0)
        exit()

    if len(nox)%2 == 0:
        left = len(nox)//2 - 1
        right = len(nox)//2
        
        left_idx = idxs[left]
        right_idx = idxs[right]
        
    else:
        center = len(nox)//2
        left_idx = idxs[center]
        right_idx = idxs[center]

    old_left = -1
    old_right = -1
    while not (old_left == left_idx and old_right == right_idx):
        old_left = left_idx
        old_right = right_idx

        if s[left_idx] == s[right_idx]:
            if left_idx > 0:
                left_idx -= 1
            if right_idx < N-1:
                right_idx += 1
        elif s[left_idx] == "x":
            ans += 1
            if left_idx > 0:
                left_idx -= 1
        elif s[right_idx] == "x":
            ans += 1
            if right_idx < N-1:
                right_idx += 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s=input()
i=0
j=len(s)-1
ans=0
while i<j:
    while s[i]==s[j]=="x":
        i+=1
        j-=1
        if i>j:break    
    while s[i]=="x":
        i+=1
        ans+=1
        if i>=len(s):break
    if i>j:break
    while s[j]=="x":
        j-=1
        ans+=1
        if j<=-1:break
    if s[i]!=s[j]:
        ans=-1
        break
    if i>j:break
    i+=1
    j-=1
    if i>j:break
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
i=0
j=len(s)-1
ans=0
while i<j:
    while s[i]==s[j]=="x":
        i+=1
        j-=1
        if i>=len(s) or j<0 or i>j:break    
    while s[i]=="x":
        i+=1
        ans+=1
        if i>=len(s):break
    if i>j:break
    while s[j]=="x":
        j-=1
        ans+=1
        if j<=-1:break
    if s[i]!=s[j]:
        ans=-1
        break
    if i>j:break
    i+=1
    j-=1
    if i>j:break
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
i=0
j=len(s)-1
ans=0
while i<j:
    while s[i]=="x":
        i+=1
        ans+=1
        if i==len(s):break
    if i>j:break
    while s[j]=="x":
        j-=1
        ans+=1
        if j==-1:break
    if s[i]!=s[j]:
        ans=-1
        break
    if i>j:break
    i+=1
    j-=1
    if i>j:break
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()

    l = 0
    r = len(s) - 1

    ans = 0
    while l < r:
        xl = 0
        while (l < r) and (s[l] == 'x'):
            l += 1
            xl += 1

        xr = 0
        while (l < r) and (s[r] == 'x'):
            r -= 1
            xr += 1

        if s[l] != s[r]:
            print(-1)
            return

        ans += abs(xl - xr)
        l += 1
        r -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()

    l = 0
    r = len(s) - 1

    ans = 0
    while l < r:
        xl = 0
        while (l < r) and (s[l] == 'x'):
            l += 1
            xl += 1

        xr = 0
        while (l < r) and (s[r] == 'x'):
            r -= 1
            xr += 1

        if s[l] != s[r]:
            print(-1)
            return

        if s[l] != 'x':
            ans += xl + xr
        l += 1
        r -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()

    l = 0
    r = len(s) - 1

    ans = 0
    while l < r:
        xl = 0
        while (l < r) and (s[l] == 'x'):
            l += 1
            xl += 1

        xr = 0
        while (l < r) and (s[r] == 'x'):
            r -= 1
            xr += 1

        if s[l] != s[r]:
            print(-1)
            return

        ans += xl + xr
        l += 1
        r -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())
new = []
for s in S:
    if s=='x':
        continue
    new.append(s)

leng = len(new)
for n in range(leng//2):
    if new[n]!=new[-n-1]:
        print(-1)
        break
else:
    lis = []
    cnt = 0
    for s in S:
        if s!='x':
            lis.append(cnt)
        else:
            cnt += 1
    lis.append(cnt)
    leng = len(lis)
    ans = 0
    for i in range(leng//2):
        ans += abs(lis[i]-lis[-i-1])
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())
new = []
for s in S:
    if s=='x':
        continue
    new.append(s)

leng = len(new)
for n in range(leng//2):
    if new[n]!=new[-n-1]:
        print(-1)
        break
else:
    print(len(S)-leng)

Submission status: Wrong Answer

Solution code:
s = input()

c = s.count("x")
t =  s.replace('x',"")

if t == t[::-1]:
    print(c)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
ss = s.replace("x","")

l = len(s)//2
ll = len(ss)//2

if len(s)%2 ==1:
    s1 = s[:l]
    s2 = s[l+1:][::-1]
else:
    s1 = s[:l]
    s2 = s[l:][::-1]


for i in range(ll):
    if ss[i] != ss[-(i+1)]:
        print(-1)
        exit()
cnt = 0
for i in range(l):
    if s1[i] != s2[i]:
        cnt +=1
print(cnt)

Submission status: Wrong Answer

Solution code:
s=input()
s1=''
for c in s:
    if c!='x':
        s1+=c
if s1!=s1[::-1]:
    print('-1')
else:
    print(s.count('x'))

Submission status: Wrong Answer

Solution code:
import re
string = input()
reverse = string[::-1]
no_x = re.sub('x', '', string)
if no_x != no_x[::-1]:
    print(-1)
    exit()
ans = 0
for idx in range(len(string)-1, -1, -1):
    if string[idx] == 'x':
        if reverse[idx] != 'x':
            ans += 1
print(ans)

Submission status: Wrong Answer

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

if n==1:
    print(0)
    exit(0)


i=0
j=1
cnt=0
while True:
    f=s[i]
    l=s[-j]
    if f==l:
        i+=1
        j+=1
    else:
        if f=='x' and l!='x':
            cnt+=1
            i+=1
        elif f!='x' and l=='x':
            cnt+=1
            j+=1
        else:
            print(-1)
            exit(0)

    if i+j==cnt+n or (i>=n-1 or j>=n):
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()

core = []

for c in s:
    if c != 'x':
        core.append(c)
if core == []:
    print(0)
    exit()
l = 0
r = len(core) - 1

while l <= r:
    if core[l] != core[r]:
        print(-1)
        exit()
    l += 1
    r -= 1

ans = 0
l = 0
r = len(s) - 1

while l <= r:
    print(l, r)
    lx = 0
    rx = 0
    while s[l] == 'x':
        l += 1
        lx += 1
    while s[r] == 'x':
        r -= 1
        rx += 1
    ans += abs(lx - rx)
    l += 1
    r -= 1
print(ans)

Submission status: Wrong Answer

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):
        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)
        
        if ind % 1 == 0:
            left_s = s[:int(ind)][::-1]
            right_s = s[int(ind)+1:]
        else:
            left_s = s[:int(ind)+1][::-1]
            right_s = s[int(ind)+1:]

        # print(f"ind {ind}")
        # 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()