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 → xa x bxa → xaxbxa x       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 → x oxxx → x xoxxx → x xxoxxx

Solution (Python):
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i+j < len(S)-1 and i <= len(S)-1 and j <= len(S)-1 :
    if S[i] == invS[j]:
        i += 1
        j += 1
    elif S[i] == "x":
        cnt += 1
        i += 1
    elif invS[j] == "x":
        cnt += 1
        j += 1
    else:
        cnt = -1
        break
print(cnt)

Solution (Python):
s=input()
l,r=0,len(s)-1
cnt=0
while l<r:
    if s[r]!=s[l]:
        cnt +=1
        if s[l]=="x":l +=1
        elif s[r]=="x":r -=1
        else:print(-1);exit()
    else:
        r -=1
        l +=1
print(cnt)

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

Solution (Python):
S=input()

A=list(S)

sets={}
for i in A:
    if i in sets:
        sets[i]+=1
    else:
        sets[i]=1

x_remove =S.replace("x","")

if len(x_remove)==1:
    central_index=S.index(x_remove)
    right_x=S[:central_index].count("x")
    left_x=S[central_index:].count("x")
    
    if right_x >= left_x:
        print(right_x-left_x)
    else:
        print(left_x - right_x)

elif len(x_remove)%2==1:
    central_index =len(x_remove)//2 

    if x_remove[:central_index] == x_remove[-central_index:][::-1]:
        string=x_remove[:central_index]
        
        left=S
        left_count=[]
        
        right=S[::-1]
        right_count=[]
        
        l_count=0
        r_count=0
            
        for i in right:
            if i=="x":
                r_count+=1
            else:
                right_count.append(r_count)
                r_count=0
                if len(right_count)==central_index+1:
                    break

        for i in left:
            if i=="x":
                l_count+=1
            else:
                left_count.append(l_count)
                l_count=0
                if len(left_count)==central_index+1:
                    break
        count=0
        for l,r in zip(left_count, right_count):
            if l+r>=1:
                if l>r:
                    count+=(l-r)
                else:
                    count+=(r-l)
        print(count)
            
    else:
        print(-1)
    
else:
    central_index =len(x_remove)//2 

    if x_remove[:central_index] == x_remove[-central_index:][::-1]:
        left=S
        left_count=[]
        
        right=S[::-1]
        right_count=[]
        
        l_count=0
        r_count=0
            
        for i in right:
            if i=="x":
                r_count+=1
            else:
                right_count.append(r_count)
                r_count=0
                if len(right_count)==central_index+1:
                    break

        for i in left:
            if i=="x":
                l_count+=1
            else:
                left_count.append(l_count)
                l_count=0
                if len(left_count)==central_index+1:
                    break
        count=0
        for l,r in zip(left_count, right_count):
            if l+r>=1:
                if l>r:
                    count+=(l-r)
                else:
                    count+=(r-l)
        print(count)
    else:
        print("-1")

Solution (Python):
s = list(input())

a = 0
b = len(s) - 1

ans = 0
flag = True

while True:
    if a >= b:
        break
    if s[a] == s[b]:
        a += 1
        b -= 1
    else:
        if s[a] == 'x' :
            a += 1
            ans += 1
        elif s[b] == 'x':
            b -= 1
            ans += 1
        else:
            flag = False
            break

if flag:
    print(ans)
else:
    print(-1)

Solution (Python):
S="Z"+input()+"Z";i=j=c=0
while i<len(S)or j<len(S):
    if S[i]==S[~j]:i+=1;j+=1
    elif S[i]=="x":c+=1;i+=1
    elif S[~j]=="x":j+=1
    else:print(-1);quit()
print(c)

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

Solution (Python):
import sys
s = input()

s_without_x = [si for si in s if si != 'x']
if len(s_without_x) % 2 == 0:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2-1:-1]:
        print(-1)
        sys.exit(0)
else:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2:-1]:
        print(-1)
        sys.exit(0)


l = 0
r = len(s) - 1
cnt = 0
while l < r:
    if s[l] != s[r]:
        cnt += 1
        if s[l] == 'x':
            l += 1
        else:
            r -= 1
    else:
        l += 1
        r -= 1
print(cnt)

Solution (Python):
s = list(input())

i = 0
j = len(s) - 1
ans = 0
while(i <= j):
    if s[i] == s[j]:
        i += 1
        j -= 1
        continue
    else:
        if s[i] == "x":
            i += 1
            ans += 1
            continue
        elif s[j] == "x":
            j -= 1
            ans += 1
            continue
        else:
            ans = -1
            break

print(ans)

Solution (Python):
s = list(input())
N = len(s)
s_ = s[:]


l,r = 0,N-1
ans = 0

while(1):
  if(l >= r): 
    break
    
  if(s[l] == s[r]):
    l,r = l+1,r-1
  else:
    if(s[l] == 'x'): 
      l = l+1
      ans += 1
    elif(s[r] == 'x'): 
      r = r-1
      ans += 1
    else: 
      ans = -1
      break
      
print(ans)

Solution (Python):


s=list(input())

a=0
z=len(s)-1
count=0

while a<z:
    if s[a]==s[z]:
        a+=1
        z-=1
    else:
        if s[a]=="x":
            a+=1
            count+=1
        elif s[z]=="x":
            z-=1
            count+=1
        else:
            print(-1)
            exit()
    
print(count)

Solution (Python):
"C - Inserting'x' "

S = str(input())

left_pointa = 0
right_pointa = len(S)-1

flag = True
cnt = 0
while flag:
    if S[left_pointa] == S[right_pointa]:  
        left_pointa += 1
        right_pointa -= 1
        if left_pointa > right_pointa:  
            break
            
            
            
            
            
            

    if S[left_pointa] != S[right_pointa]:  
        if S[left_pointa] == 'x':
            cnt += 1  
            left_pointa += 1  
        elif S[right_pointa] == 'x':
            cnt += 1  
            right_pointa -= 1  
        else:  
            flag = False
            break

if flag:
    print(cnt)
else:
    print(-1)

Solution (Python):



def main():
    from collections import deque

    s = deque(input())
    ans = 0

    
    
    
    while s:
        
        first = s.popleft()

        if len(s) >= 1:
            
            last = s.pop()
        else:
            
            print(ans)
            exit()

        
        if first == last:
            pass
        
        
        elif first == 'x' and last != 'x':
            s.appendleft(first)
            s.append(last)
            s.append('x')
            ans += 1
        
        
        elif first != 'x' and last == 'x':
            s.appendleft(first)
            s.appendleft('x')
            s.append(last)
            ans += 1
        
        else:
            print(-1)
            exit()

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()

ss = s.replace('x','')
if ss != ss[::-1]:
  print(-1)
  exit()

xc = []
c = 0
for i in range(len(s)):
  if s[i] == 'x':
    c += 1
  else:
    xc.append(c)
    c = 0
xc.append(c)

ans = 0
for i in range(len(xc)//2):
  ans += abs(xc[i] - xc[len(xc)-1-i])
print(ans)

Solution (Python):
def main():
    s = list(input())
    n = len(s)

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = list(input())
    n = len(s)

    ans = 0
    l, r = 0, n-1
    while l < r:
        if s[l] == "x" and s[r] == "x":
            l += 1
            r -= 1
        elif s[l] == "x":
            l += 1
            ans += 1
        elif s[r] == "x":
            r -= 1
            ans += 1
        elif s[l] == s[r]:
            l += 1
            r -= 1
        else:
            print(-1)
            exit()
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = list(input())
    n = len(s)

    ans = 0
    l, r = 0, n-1
    while l < r:
        if s[l] == "x" or s[r] == "x":
            if s[l] == "x" and s[r] == "x":
                l += 1
                r -= 1
            elif s[l] == "x":
                l += 1
                ans += 1
            elif s[r] == "x":
                r -= 1
                ans += 1
        elif s[l] == s[r]:
            l += 1
            r -= 1
        else:
            print(-1)
            exit()
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd
from datetime import date
from math import factorial
import functools
from heapq import*
from collections import deque
import collections
import math
from collections import defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
inf = float("inf")


def main():
    s = list(input())
    n = len(s)
    l, r = 0, n-1

    ans = 0
    while l < n and r >= 0 and l < r:
        if s[l] == "x" or s[r] == "x":
            if s[l] == "x" and s[r] == "x":
                l += 1
                r -= 1
            elif s[l] == "x":
                l += 1
                ans += 1
            elif s[r] == "x":
                r -= 1
                ans += 1
        elif s[l] == s[r]:
            l += 1
            r -= 1
        else:
            print(-1)
            exit()
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


from collections import deque

s=input()
n=len(s)
def chk(s):
    s=''.join(s).replace('x','')
    if s!=s[::-1]:
        print(-1)
        exit()
chk(s)

s=deque(s)

res=[]
cnt=0
while len(s)>1:
    if s[0]==s[-1]:
        s.pop()
        res.append(s.popleft())
    else:
        if s[0]=='x':
            s.append('x')
        else:
            s.appendleft('x')
        cnt+=1
print(cnt)

Solution (Python):
s = input()
left = 0
right = len(s)-1
ans = 0
while left < right:
    if 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
    else:
        print(-1)
        exit()
print(ans)

Solution (Python):
import math
def solve():
  s = input()
  n = len(s)
  ct = 0
  mae = -1
  usiro = 0
  times = (n+1)//2
  tim = 0
  while tim < times:
    tim += 1
    mae += 1
    usiro -= 1
    if s[mae] == s[usiro]:
      continue
    else:
      if s[mae] == "x":
        ct += 1
        usiro += 1
        tim -= 0.5
      elif s[usiro] =="x":
        ct += 1
        mae -= 1
        tim -= 0.5
      else:
        print("-1")
        exit()
  print(ct)

solve()

Solution (Python):
def main():

    s = input()
    xcount = 0
    i, j = 0, len(s)-1
    ans = 0
    while i < j:
        if s[i] != s[j]:
            if s[i] == "x":
                i += 1
                ans += 1
            elif s[j] == "x":
                j -= 1
                ans += 1
            else:
                return -1
        else:
            i += 1
            j -= 1
    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def main():
    s = list(input())

    left, right = 0, len(s) - 1
    ans = 0
    while 1:
        if left >= right:
            break
        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)
                sys.exit()
    print(ans)





if __name__ == '__main__':
    main()

Solution (Python):
s = input()
r = 0
a = 0
b = len(s)-1
while a < b:
  if s[a] == s[b]:
    a += 1
    b -= 1
  elif s[a] == "x":
    r += 1
    a += 1
  elif s[b] == "x":
    r += 1
    b -= 1
  else:
    print(-1)
    exit()
print(r)

Solution (Python):
s=input()

sAray=[]

x=0
for i in s:
  if i=="x":
    x+=1
  else:
    sAray.append(x)
    sAray.append(i)
    x=0
sAray.append(x)

cnt=0
half=(-(-len(sAray)//2))

for i in range(half):
  if sAray[i]!=sAray[-i-1]:
    if isinstance(sAray[i], int) and isinstance(sAray[-1-i], int) :
      cnt+=abs(sAray[i]-sAray[-i-1])
    else:
      print(-1)
      exit()

print(cnt)

Solution (Python):
import sys,bisect,heapq
def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(input())

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a*b*GCD(a,b)

def main():
    s = S()
    l = 0
    r = len(s)-1
    ans = 0

    while l < r:
        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
        else:
            ans = -1
            break
    return(ans)


if __name__ == "__main__":
    print(main())

Solution (Python):
s = input()


if s.replace('x', '') != s.replace('x', '')[::-1]:
    print(-1)
    exit()

ans = 0

l = 0
r = len(s)-1

while l < r:
    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
    else:
        print('error')
        exit()
print(ans)

Solution (Python):
ans = 0
s = input()

s_without_x = [ i for i in s if i != "x" ]
if s_without_x != s_without_x[::-1]:
    ans = -1

else:
    x_list = []
    x = 0
    for i, v in enumerate(s):
        if v != "x":
            x_list.append(x)
            x = 0
        else:
            x += 1
        if i == len(s) - 1:
            x_list.append(x)

    for i in range(len(x_list)//2):
        a, b = max(x_list[i], x_list[-1-i]), min(x_list[i], x_list[-1-i])
        ans += a-b

print(ans)

Solution (Python):
s=input()

if s==s[::-1]:
  print(0)
else:
  ss=s.replace("x","",10**5)
  if ss!=ss[::-1]:
    print(-1)
  else:
    x=[0]*(len(ss)+1)
    j=0
    for i in range(len(s)):
      if s[i]=="x":
        x[j]+=1
      else:
        j+=1
    for i in range(len(x)):
      x[i],x[-1-i]=x[i]-min(x[i],x[-1-i]),x[-i-1]-min(x[i],x[-1-i])
    print(sum(x))

Solution (Python):
import collections

s = input()
v = collections.deque(s)
c = 0
while len(v) > 1:
    x = v[0]
    y = v[-1]
    if x == y:
        v.popleft()
        v.pop()
    else:
        if x == 'x':
            c += 1
            v.popleft()
        elif y == 'x':
            c += 1
            v.pop()
        else:
            print(-1)
            exit()

print(c)

Solution (Python):
from collections import deque

S = input()

ans = 0
Q = deque(S)
while len(Q) >= 2:
    left, right = Q[0], Q[-1]

    if left == right:
        Q.popleft()
        Q.pop()
    elif left == 'x':
        Q.popleft()
        ans += 1
    elif right == 'x':
        Q.pop()
        ans += 1
    else:
        print(-1)
        quit()

print(ans)

Solution (Python):
from collections import deque

S = input()

ans = 0
Q = deque(S)
while len(Q) >= 2:
    left = Q.popleft()
    right = Q.pop()
    if left != right:
        if left == 'x':
            Q.append(right)
            ans += 1
        elif right == 'x':
            Q.appendleft(left)
            ans += 1
        else:
            print(-1)
            quit()
print(ans)

Solution (Python):
s = input()
left = 0
right = len(s) - 1
x_left = 0
x_right = 0
if len(s) >= 2:
    while (left <= right):
        if s[left] == s[right]:
            left += 1
            right -= 1
        elif s[left] == "x":
            x_left += 1
            left += 1
        elif s[right] == "x":
            x_right += 1
            right -= 1
        else:
            print(-1)
            exit()
    print(x_left + x_right)
else:
    print(0)

Solution (Python):
import sys
s=list(input())
n=len(s)
l=0;r=n-1
ans=0
while l<r:
  if s[l]==s[r]:
    l+=1;r-=1
  elif s[l]=='x':
    l+=1
    ans+=1
  elif s[r]=='x':
    r-=1
    ans+=1
  else:
    print(-1)
    sys.exit()
print(ans)

Solution (Python):
S = input()
cnt = 0
l = 0
r = len(S)-1
while(l<r):
    if(S[l]==S[r]):
        l+=1
        r-=1
    elif(S[l]=='x'):
        cnt += 1
        l+=1
    elif(S[r]=='x'):
        cnt += 1
        r-=1
    else:
        print(-1)
        break
else:
    print(cnt)

Solution (Python):
S = input()
cnt = 0
l = 0
r = len(S)-1
while(l<r):
    if(S[l]==S[r]):
        l+=1
        r-=1
    elif(S[l]=='x'):
        cnt += 1
        l+=1
    elif(S[r]=='x'):
        cnt += 1
        r-=1
    else:
        print(-1)
        break
else:
    print(cnt)

Solution (Python):
import numpy as np
s = input()
temp = s
temp = temp.replace("x","")
is_rev = True

mirro = []

if len(temp) == 0:
    print(0)
else:
    for i in range(len(temp)//2):
        if temp[i] == temp[-i-1]:
            mirro.append(temp[i])
            continue
        else:
            is_rev = False
    if len(temp) %2 == 1:
        mirro.append(temp[len(temp)//2])

    if is_rev:
        ind = 0
        i = 0
        lx = np.array([0 for i in range(len(temp)//2+len(temp)%2)])
        rx = np.array([0 for i in range(len(temp)//2+len(temp)%2)])
        temp = 0
        while (True):
            if s[i] == mirro[ind]:
                lx[ind] = temp
                temp = 0
                ind += 1
                i += 1
            else:
                temp += 1
                i += 1

            if ind > len(mirro)-1:
                break
        ind = 0
        i = -1
        temp = 0
        while (True):
            if s[i] == mirro[ind]:
                rx[ind] = temp
                temp = 0
                ind += 1
                i -= 1
            else:
                temp += 1
                i -= 1

            if ind > len(mirro)-1:
                break
        
        
        print(np.sum(np.abs(lx-rx)))
    else:
        print(-1)

Solution (Python):

def main():
    S = input().rstrip()
    ans, l, r = 0, 0, len(S) - 1
    while l < r:
        if S[l] == S[r]:
            l += 1
            r -= 1
        else:
            ans += 1
            if S[l] == "x":
                l += 1
            elif S[r] == "x":
                r -= 1
            else:
                print(-1)
                return
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque


def main():
    S = deque(list(input().rstrip()))
    ans = 0
    while len(S) >= 2:
        l, r = S[0], S[-1]
        if l == r:
            S.popleft(), S.pop()
        else:
            ans += 1
            if l == "x":
                S.append("x")
            elif r == "x":
                S.appendleft("x")
            else:
                print(-1)
                return
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
new_s = []
for i in range(len(s)):
    if s[i] != "x":
        new_s.append(s[i])

for i in range(len(new_s)):
    if new_s[i] != new_s[-1-i]:
        print(-1)
        exit()

i = 0
j = len(s) - 1
cnt = 0
while True:
    if  i > j:
        break
    if s[i] == s[j]:
        i += 1
        j -= 1
    else:
        if s[i] == "x":
            cnt += 1
            i += 1
        else:
            cnt += 1
            j -= 1
print(cnt)

Solution (Python):
S = input()
N = len(S)
l, r = 0, N - 1
num = 0
while l <= r:
    if S[l] == S[r]:
        l += 1
        r -= 1
    else:
        if S[l] == 'x':
            l += 1
            num += 1
        elif S[r] == 'x':
            r -= 1
            num += 1
        else:
            print(-1)
            exit()

print(num)

Solution (Python):
s = input()
_s = s.replace("x", "")
if _s == _s[::-1]:
	cnt = 0
	lngt = len(s)
	i, j = 0, lngt - 1
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -=1
		elif s[i] == "x":
			i += 1
			cnt += 1
		else:
			j -= 1
			cnt += 1

	print(cnt)
		
else:
	print(-1)

Solution (Python):
s = input()
n = len(s)
i = 0
j = n-1
ans = 0
while j - i > 0:
    
    if s[i] == s[j]:
        i += 1
        j -= 1
        continue
    if s[i] == 'x':
        ans += 1
        i += 1
        continue
    if s[j] == 'x':
        ans += 1
        j -= 1
        continue

    print(-1)
    exit()

print(ans)

Solution (Python):
s=input()
n=[0]
ss=[]
for c in s:
    if c=='x':
        n[-1]+=1
    else:
        ss.append(c)
        n.append(0)
if all([ss[i]==ss[-1-i] for i in range(len(ss)//2)]):
    print(sum([abs(n[i]-n[-1-i]) for i in range(len(n)//2)]))
else:
    print(-1)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
que = deque(s)
ans = 0
while que:
    l,r = que[0],que[-1]
    if l == r:
        if len(que) == 1:
            break
        que.popleft()
        que.pop()
        continue
    if l == 'x':
        que.append('x')
        ans += 1
        continue
    if r == 'x':
        que.appendleft('x')
        ans += 1
        continue
    print(-1)
    quit()
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input().rstrip()

answer = 0
while S:
    if len(S) < 2:
        break
    L,R = S[0],S[-1]
    if L == R:
        S = S[1:-1]
        continue
    if L == 'x':
        answer += 1
        S = S[1:]
        continue
    if R == 'x':
        answer += 1
        S = S[:-1]
        continue
    answer = -1
    break

print(answer)

Solution (Python):
s=input()

res=0
while len(s)>1:
  if s[0]==s[-1]:
    s=s[1:-1]
  elif s[0]=="x":
    s=s+"x"
    res+=1
  elif s[-1]=="x": 
    s="x"+s
    res+=1
  else:
    print(-1)
    exit()
    
print(res)

Solution (Python):
s = list(input())
X = []
hoge = 0
for i in range(len(s)):
    if s[i] == 'x':
        hoge += 1
    else:
        X.append(hoge)
        X.append(s[i])
        hoge = 0
X.append(hoge)
ans = 0

for i in range(len(X)//2):
    if i % 2 == 0:
        ans += abs(X[i]-X[-i-1])
    elif X[i] != X[-i-1]:
        print(-1)
        exit()
print(ans)

Solution (Python):
s=input()
r=s.replace("x","")
if r!=r[::-1]:
    print(-1)
else:
    ans=[]
    cnt=0
    for i in s:
        if i=="x":
            cnt+=1
        else:
            ans.append(cnt)
            cnt=0
    ans.append(cnt)
    ans2=0
    for i in range(len(ans)//2):
        ans2+=abs(ans[i]-ans[len(ans)-1-i])
    print(ans2)

Solution (Python):
S = input()
N = len(S)

left = 0
right = N - 1

ans = 0
while left < right :
    if S[left] == S[right] :
        left += 1
        right -= 1
        continue
    if S[left] == 'x' :
        ans += 1
        left += 1
        continue
    if S[right] == 'x' :
        ans += 1
        right -= 1
        continue
    print('-1')
    break
else :
    print(ans)

Solution (Python):
def solve(s):
    n = len(s)
    t = "".join(filter(lambda _: _!= "x", s))
    if t != t[::-1]:
        return -1
    l = 0
    r = n-1
    res = 0
    while l < r:
        nl = 0
        while (l < r) and (s[l] == "x"):
            nl += 1
            l += 1
        nr = 0
        while (l < r) and (s[r] == "x"):
            nr += 1
            r -= 1
        if (s[l] == s[r]) and (s[l] != "x"):
            res += abs(nl - nr)
        l += 1
        r -= 1
    return res

assert solve("xabxa") == 2
assert solve("ab") == -1
assert solve("a") == 0
assert solve("oxxx") == 3
assert solve("axxxa") == 0

s = input()
print(solve(s))

Solution (Python):
s = input()
cnt = 0
l = 0
r = len(s) - 1
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)
        exit()
print(cnt)

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())



def judge(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    if news == news[::-1]:
        return True
    else:
        return False


def min_insertion(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    n = len(news)
    fir = [0] * ((n + 1)//2)
    lat = [0] * ((n + 1) // 2)

    
    cursor = 0
    for si in s:
        if si == 'x':
            fir[cursor] += 1
        else:
            cursor += 1

        if cursor == (n+1)//2:
            break

    
    cursor = 0
    for si in s[::-1]:
        if si == 'x':
            lat[cursor] += 1
        else:
            cursor += 1

        if cursor == (n + 1) // 2:
            break

    ans = 0
    for firi, lati in zip(fir, lat):
        ans += abs(firi - lati)

    return ans



s = ns()


is_kaibun = judge(s)



if is_kaibun:
    if len(set(list(s))) == 1:
        print(0)
    else:
        print(min_insertion(s))
else:
    print(-1)

Solution (Python):
s = input()
t = "".join(s.split("x"))
if t!=t[::-1]:print(-1);exit()
ans = 0
i,j=0,len(s)-1
while i<j:
    if s[i]==s[j]:i+=1;j-=1
    elif s[i]=="x":ans+=1;i+=1
    elif s[j]=="x":ans+=1;j-=1
print(ans)

Solution (Python):
def main():
    s = input()
    t = s.replace("x", "")
    M = len(t)
    for i in range(M//2):
        if t[i] != t[M-i-1]:
            print(-1)
            return
    xcnt = [0] * (M+1)
    i = 0
    for c in s:
        if c == "x":
            xcnt[i] += 1
        else:
            i += 1
    ans = 0
    for i in range((M+1)//2):
        ans += abs(xcnt[i] - xcnt[M-i])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s=input()
S="a"+s[::]+"a"
s=s.replace("x","")
if s[:len(s)//2]!=s[:-(len(s)//2)-1:-1]:
    print(-1)
    quit()
r=0
l=len(S)-1
ans=0
while r<l:
    R,L=0,0
    while S[r+R]=="x":
        R+=1
    while S[l-L]=="x":
        L+=1
    ans+=abs(R-L)
    r+=R+1
    l-=L+1
print(ans)

Solution (Python):
import re

S = input()
without_x = ''.join([s for s in S if s != 'x'])
if without_x != without_x[::-1]: print(-1)
elif S == S[::-1]: print(0)
else:
    ans = 0
    x_lists = re.split('[{}]'.format(''.join(list(set(without_x)))), S)
    n = len(x_lists); i = 0
    while i < n-1-i:
        ans += abs(len(x_lists[i]) - len(x_lists[n-1-i]))
        i += 1
    print(ans)

Solution (Python):
s = input().rstrip();
n = len(s);
i = 0;
j = n-1;
ans = 0;
while i < j :
	if s[i] == s[j] :
		i += 1;
		j -= 1; 
	elif s[i] == 'x' :
		ans += 1;
		i += 1;
	elif s[j] == 'x' :
		ans += 1;
		j -= 1;
	else :
		ans = -1;
		break;
print(ans);

Solution (Python):
S = input()
if S==S[::-1]:
    print(0)
else:
    S1 = ""
    x = []
    a = 0
    for i in range(len(S)):
        if S[i]=="x":
            a += 1
            if i==len(S)-1:
                x.append(a)
        else:
            S1+=S[i]
            x.append(a)
            a = 0
            if i==len(S)-1:
                x.append(a)
    if S1!=S1[::-1]:
        print(-1)
    else:
        ans = 0
        y = x[::-1]
        for i in range(len(x)//2):
            ans += abs(x[i]-y[i])
        print(ans)

Solution (Python):
S=input()
X=[]
SS=""
cnt=0
for s in S:
    if s=="x":
        cnt+=1
    else:
        SS+=s
        X.append(cnt)
        cnt=0
X.append(cnt)
flag=True
for i in range(len(SS)//2):
    if SS[i]!=SS[-i-1]:
        flag=False
        break
ans=0
for i in range(len(X)//2):
    ans+=abs(X[i]-X[-i-1])
if flag:
    print(ans)
else:
    print(-1)

Solution (Python):
import sys
import numpy as np


def main():
    s = input()
    snotx = s.replace('x', '')
    if snotx != snotx[::-1]:
        print(-1)
        sys.exit()
    ans = 0
    ileft = 0
    iright = len(s) - 1
    while ileft < iright:
        if s[ileft] != s[iright]:
            if s[ileft] == 'x':
                ans += 1
                ileft += 1
            elif s[iright] == 'x':
                ans += 1
                iright -= 1
            else:
                print("Error!")
                sys.exit()
        else:
            ileft += 1
            iright -= 1
    print(ans)



main()

Solution (Python):
S = input()

T = []

x_list = []

temp = 0
for i in range(len(S)):
    if S[i]!="x":
        T.append(S[i])
        x_list.append(temp)
        temp=0
    else:
        temp+=1
x_list.append(temp)

U = T[::-1]
flag=False
for i in range(len(T)):
    if T[i]!=U[i]:
        flag=True
        break

if flag:
    print(-1)
else:
    count=0
    for i in range(len(x_list)//2):
        count+=abs(x_list[i]-x_list[len(x_list)-1-i])

    print(count)

Solution (Python):
s=input()
l=len(s)
s2=''
for i in range(l):
  if s[i]!='x':
    s2+=s[i]
l2=len(s2)
for i in range(l2):
  if s2[i]!=s2[-(i+1)]:
    print(-1)
    break
else:
  lpos=0
  rpos=l-1
  cnt=0
  while rpos-lpos>=1:
    if s[lpos]==s[rpos]:
      lpos+=1
      rpos-=1
      continue
    else:
      if s[lpos]=='x':
        lpos+=1
        cnt+=1
      else:
        rpos-=1
        cnt+=1
  print(cnt)