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: Accepted

Solution code:
s=list(input())

l=0
r=len(s)-1

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
l = 0
r = n-1
res = 0
while l < r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        res += 1
        l += 1
    elif s[r] == 'x':
        res += 1
        r -= 1
    else:
        print(-1)
        break
else:
    print(res)

Submission status: Accepted

Solution code:
s=input()
t=""
a=[0]
for i in s:
  if i!="x":
    t+=i
    a.append(0)
  else:a[-1]+=1
if t!=t[::-1]:print(-1);exit()
x=0
for i in range(len(a)//2):x+=abs(a[i]-a[-i-1])
print(x)

Submission status: Accepted

Solution code:
#inserting X
s=input()
length=len(s)
string=""
for i in range(length):
    if s[i]!="x":
        string=string+s[i]
    else:
        continue


flag=True
length2=len(string)
if length2>=2:
    for i in range(length2//2):
        if string[i]!=string[-(i+1)]:
            flag=False
            break
if flag:
    s="x"+s+"X"
    length=len(s)
    checklist=[]
    count=0
    for i in range(length):
        if s[i]=="x":
            count+=1
        else:
            checklist.append(count)
            count=0
    checklist[0]-=1
    ans=0
    for i in range(len(checklist)//2):
        ans+=abs(checklist[i]-checklist[-(i+1)])
    print(ans)
    
else:
    print("-1")

Submission status: Accepted

Solution code:
s = input()
l, r = 0, len(s)-1
ans = 0
while(True):
    if l >= r: break
    sl, sr = s[l], s[r]
    if sl == sr: l += 1; r -= 1
    elif sl == "x":
        ans += 1
        l += 1
    elif sr == "x":
        ans += 1
        r -= 1
    elif sl != sr:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
s = input()
cnt = 0
while len(s) > 0:
#  print(s)
  if s[0] == s[-1]:
    s = s[1:-1]
  elif s[0] == "x":
    cnt += 1
    s = s[1:]
  elif s[-1] == "x":
    cnt += 1
    s = s[:-1]
  else:
    print(-1)
    exit()
print(cnt)

Submission status: Accepted

Solution code:
S = list(input())
i = 0
r = len(S)-1
ans = 0
while i < len(S) or r >= 0:
  if i >= len(S):
    if r < 0:
      break
    else:
      if S[r] == "x":
        ans += 1
        r -= 1
      else:
        ans = -1
        break
  if r < 0:
    if i >= len(S):
      break
    else:
      if S[i] == "x":
        ans += 1
        i += 1
      else:
        ans = -1
        break
  if i < len(S) and r >= 0:
    if S[i] != S[r]:
      if S[i] == "x":
        ans += 1
        i += 1
      elif S[r] == "x":
        ans += 1
        r -= 1
      else:
        ans = -1
        break
    else:
      i += 1
      r -= 1
print(ans//2)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

def main():
    s = input().strip()
    n = len(s)
    ans = 0
    i = 0
    j = n-1
    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:
                print(-1)
                return
        else:
            i += 1
            j -= 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
l = 0
r = 1
ans = 0

for i in range(len(s)):

    if l+r >= len(s):
        break

    if s[l] == s[-r]:
        l += 1
        r += 1

    elif s[l] == "x" and s[-r] != "x":
        ans += 1
        l += 1

    elif s[l] != "x" and s[-r] == "x":
        ans += 1
        r += 1

    else:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
l = 0
r = 1
ans = 0

for i in range(len(s)):

    if l+r > len(s):
        break

    if s[l] == s[-r]:
        l += 1
        r += 1

    elif s[l] == "x" and s[-r] != "x":
        ans += 1
        l += 1

    elif s[l] != "x" and s[-r] == "x":
        ans += 1
        r += 1

    else:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
s = input()
l, r = 0, len(s) - 1
ans = 0
while l < r:
    if s[l] != s[r]:
        if s[l] == 'x':
            ans += 1
            r += 1
        elif s[r] == 'x':
            ans += 1
            l -= 1
        else:
            print(-1)
            exit(0)
    l += 1
    r -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
i=0
j=len(s)-1
c=0
while i<j:
  l=s[i]
  r=s[j]
  if l==r:
    i+=1
    j-=1
  elif l=="x"!=r:
    i+=1
    c+=1
  elif l!="x"==r:
    j-=1
    c+=1
  else:
    print(-1)
    exit()
print(c)

Submission status: Accepted

Solution code:
s=input()

r=-1
idx=0
LS=len(s)
st=(LS+1)//2

while True:
    if idx>=len(s)+r:
        break
    if s[idx]==s[r]:
        idx+=1
        r-=1
    elif s[idx]=="x" and s[r]!="x":
        LS+=1
        idx+=1
    elif s[idx]!="x" and s[r]=="x":
        LS+=1
        r-=1
    else:
        print(-1)
        exit()


print(LS-len(s))

Submission status: Accepted

Solution code:
s=input()
l=[["x",-1]]
for i in range(len(s)):
    if s[i]!="x":
        l.append([s[i],i])
a=0
l.append(["x",len(s)])
for i in range(1,-(-len(l)//2)):
    if l[i][0]==l[-i-1][0]:
        a+=abs(l[i][1]-l[i-1][1]+l[-i-1][1]-l[-i][1])
    else:
        a=-1
        break
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = list(input())
N = len(s)
s_ = s[:]
#x_index = []
#(方針)x抜いて回文なら回文にできる ←結構複雑になるのでボツ
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)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)

Submission status: Accepted

Solution code:
"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:  # leftが抜かしたら
            break
            # if S[left_pointa] == S[right_pointa]:
            #     flag = True
            #     break
            # else:
            #     flag = False
            #     break

    if S[left_pointa] != S[right_pointa]:  # 違う文字のときに分岐を考える
        if S[left_pointa] == 'x':
            cnt += 1  # 今right_pointaが見ている文字の右にxを挿入する
            left_pointa += 1  # 右側を停止させておいて左側を進めて行く
        elif S[right_pointa] == 'x':
            cnt += 1  # 今left_pointaが見ている文字の左にxを挿入する
            right_pointa -= 1  # 左側を停止させておいて右側を進めて行く
        else:  # どちらも違う文字を見ていたら回文にはならない
            flag = False
            break

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import deque

    s = deque(input())
    ans = 0

    # KeyInsight
    # 回文の判定：最初と最後の文字が一致しているかどうか
    # データ構造：文字の取り出しと削除を高速に行う
    while s:
        # 先頭の文字を取り出す
        first = s.popleft()

        if len(s) >= 1:
            # 残りが1文字以上なら，末尾の文字を取り出す
            last = s.pop()
        else:
            # 残り0文字なら終了
            print(ans)
            exit()

        # 先頭と末尾が一致なら，そのまま
        if first == last:
            pass
        # 先頭x，末尾がx以外→末尾にxを追加
        # 操作が1回増える
        elif first == 'x' and last != 'x':
            s.appendleft(first)
            s.append(last)
            s.append('x')
            ans += 1
        # 先頭x以外，末尾がx→先頭にxを追加
        # 操作が1回増える
        elif first != 'x' and last == 'x':
            s.appendleft(first)
            s.appendleft('x')
            s.append(last)
            ans += 1
        # それ以外 -1, 終了
        else:
            print(-1)
            exit()

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)
#print(xc)
ans = 0
for i in range(len(xc)//2):
  ans += abs(xc[i] - xc[len(xc)-1-i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/09/20

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
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()
    # test()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

# x を除いたときに回文になっていなければ達成不可能
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(lx)
        # print(rx)
        print(np.sum(np.abs(lx-rx)))
    else:
        print(-1)

Submission status: Accepted

Solution code:
# code-festival-2017-qualcC - Inserting 'x'
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()

Submission status: Accepted

Solution code:
# code-festival-2017-qualcC - Inserting 'x'
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
n = len(s)
i = 0
j = n-1
ans = 0
while j - i > 0:
    #print(s[i],s[j])
    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)

Submission status: Accepted

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

Submission status: Accepted

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

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)

Submission status: Accepted

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

Submission status: Accepted

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