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:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
ss=s.replace("x","")
if ss!=ss[::-1]:
 print(-1)
else:
 e=[]
 for i in range(len(ss)):
  e.append(ss[i])
 j=0
 k=0
 d1=[]
 jj=0
 kk=0
 d2=[]
 sr=s[::-1]
 for i in range(len(s)):
  if k<len(e) and s[i]==e[k] :
    d1.append(s[j:i])
    j=i+1
    k+=1
  if kk<len(e) and sr[i]==e[kk] :
    d2.append(sr[jj:i])
    jj=i+1
    kk+=1
 d1.append(s[j:])
 d2.append(sr[jj:]) 
 dd=0  
 for i in range(len(d1)//2):
   dd+=abs(len(d1[i])-len(d2[i]))
 print(dd)

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  9 22:12:40 2017

@author: kitagenbakurou
"""
import numpy as np
import copy

def insert(pos, s, x):
  return x.join([s[:pos], s[pos:] ])

s = input()
N = len(s)

i = 0
j = N - 1
cnt = 0
flag = True
while i < j : 
    if s[i] != s[j] : 
        if s[i] == "x" : 
            cnt += 1
            #s = insert(N - i  , s , "x")
            i += 1
            j += 0
        elif s[j] == "x" : 
            cnt += 1
            #s = insert(i , s , "x")
            i +=0
            j -= 1
        else : 
            flag = False
            break
    else : 
        i += 1
        j -= 1

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

Submission status: Accepted

Solution code:
S = input()
s = []
roop = True
index = [0]
cnt = 0
for i in S:
    if not(i == "x"):
        s.append(i)
        index.append(0)
    else:
        index[-1] += 1
for i in range(len(s) // 2):
    if s[i] != s[- i - 1]:
        roop = False
if roop:
    for i in range(len(index) // 2):
        if index[i] - index[- i - 1] >= 0:
            cnt += index[i] - index[- i - 1]
        else:
            cnt += index[- i - 1] - index[i]
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()
a = [0]*26
flag = 0
for i in s:
	a[ord(i) - ord('a')]+=1;
for i in range(len(a)):
	if i == ord('x') - ord('a'):
		continue;
	flag += (a[i]%2)
if flag > 1:
	print(-1)
else:
	left = 0;
	right = len(s) - 1;
	ans = 0
	while(left < right):
		if s[left] == s[right]:
			left+=1;
			right-=1;
		else:
			if s[left] == 'x':
				ans+=1;
				left += 1;
			elif s[right] == 'x':
				ans+= 1;
				right -= 1;
			else:
				flag = -1;
				break;
	if flag == -1:
		print(-1)
	else:
		print(ans);

Submission status: Accepted

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

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

    i1 = 0
    i2 = len(S) - 1
    cnt = 0
    while True:
        if S[i1] == 'x' and S[i2] != 'x':
            cnt += 1
            i1 += 1
        elif S[i1] != 'x' and S[i2] == 'x':
            cnt += 1
            i2 -= 1
        else:
            i1 += 1
            i2 -= 1
        if i2 - i1 < 1:
            break

    print(cnt)

main()

Submission status: Accepted

Solution code:
S = input()
n, m = 0, 1
s = []
patern = 0
for i in range(len(S)):
    s.append(S[i])
while n + m < len(s):
    if s[n] is 'x':
        if s[-m] is not 'x':
            m -= 1
            patern += 1
    else:
        if s[-m] is 'x':
            n -= 1
            patern += 1
        elif s[-m] is not s[n]:
            patern = -1
            break
    n += 1
    m += 1
if patern != -1:
    print(patern)
else:
    print(patern)

Submission status: Accepted

Solution code:
S = input()
n, m = 0, 1
s = []
patern = 0
for i in range(len(S)):
    s.append(S[i])
while n + m < len(s):
    if s[n] is 'x':
        if s[-m] is not 'x':
            s.append('x')
            patern += 1
    else:
        if s[-m] is 'x':
            n -= 1
            patern += 1
        elif s[-m] is not s[n]:
            patern = -1
            break
    n += 1
    m += 1
if patern != -1:
    print(patern)
else:
    print(patern)

Submission status: Accepted

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

s = input()
sx = s.replace('x','')
IsPossible = True
for i in range(int(len(sx)/2)):
    if(sx[i] != sx[-(i+1)]) : IsPossible = False

ans = 0
if IsPossible:
    l, r = 0, 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
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
def palindrome(s):
  for i in range(len(s) // 2):
    if s[i] != s[-i -1]:
      return False
  return True

def main():
  s = input()
  t = ''
  for ch in s:
    if ch != 'x':
      t += ch

  if len(t) == 0:
    print(0)
    return

  if (palindrome(t) == False):
    print(-1)
    return

  cnt = 0
  odd = len(t) & 1
  mid = len(t) // 2

  if odd == 1:  # ax[b]a

    for i in range(len(s)):
      if s[i] != 'x':
        cnt += 1
        if cnt == mid + 1:
          s1 = s[      : i]
          s2 = s[i + 1 :  ]
          break    

  else: # a[x]a

    for i in range(len(s)):
      if s[i] != 'x':
        cnt += 1
        if cnt == mid:
          s1 = s[  : i + 1]
        if cnt == mid + 1:
          s2 = s[i :  ]
          break

  # print(s1, s2)
  ans = 0
  i = 0
  j = 0
  while (i < len(s1) and j < len(s2)):
    c1 = s1[i]
    c2 = s2[-j-1]
    if c1 == c2:
      i += 1
      j += 1
      continue

    ans += 1
    if c1 == 'x':
      i += 1
    else:
      j += 1

  ans += (len(s1) - i) + (len(s2) - j)
  print(ans)






if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
s = input()
i = 0
j = len(s)-1
ist = 0

while i < j:
    if s[i] == s[j]:
        i += 1
        j -= 1
    elif s[i] == 'x':
        ist += 1
        i += 1
    elif s[j] == 'x':
        ist += 1
        j -= 1
    else:
        print(-1)
        exit()

print(ist)

Submission status: Accepted

Solution code:
s = input()

def kaibun(string):
    if string==string[::-1]:
        return True
    else:
        return False

a = s.replace('x', '')

if kaibun(a) == True:
    cnt = 0
    i = 0
    j = len(s) - 1
    while i < j:
        if s[i] != s[j]:
            if s[i] == 'x':
                cnt += 1
                i += 1
            elif s[j] == 'x':
                cnt += 1
                j -= 1
        else:
            i += 1
            j -= 1

    print(cnt)
else:
    print('-1')

Submission status: Accepted

Solution code:
S = input()
N = len(S)
st = 0
la = N - 1
result = 0

while(st < la):
  if S[st] == 'x' and S[la] == 'x':
    st += 1
    la -= 1
    continue
  if S[st] != 'x' and S[la] == 'x':
    la -= 1
    result += 1
  elif S[st] == 'x' and S[la] != 'x':
    st += 1
    result += 1
  elif S[st] == S[la]:
    st += 1
    la -= 1
  else:
    result = -1
    break

print(result)

Submission status: Accepted

Solution code:
def kaibun(s):
	for i in range( len(s) //2 ):
		#print(s[i], s[(-1*i) -1])
		if (s[i] != s[(-1*i) -1]):
			return False
	return True

s = input()		# 文字列の入力  abcdefg...


if kaibun(s):
	print(0)
	exit(0)

t = s.replace('x', '')

if not kaibun(t):
	print(-1)
	exit(0)


a=0
b=len(s)-1
ans=0

while a < b:
	A=s[a]
	B=s[b]
	#print(a,A, b,B)
	
	if A==B:
		a+=1
		b-=1
		continue
	
	if B=='x':
		b-=1
		ans+=1
		continue
	
	a+=1
	ans+=1


print(ans)

Submission status: Accepted

Solution code:
import re
s = input()
s_without_x = s.replace("x", "")
if s_without_x[::-1] != s_without_x:
    print(-1)
else:
    length = len(s_without_x)
    idx = 0
    where = [0 for _ in range(length + 1)]
    for i in range(len(s)):
        now = s[i]
        if now == "x":
            where[idx] += 1
        else:
            idx += 1
    res = 0
    rev = where[::-1]
    for i in range(len(rev)):
        res += abs(where[i] - rev[i])
    print(int(res / 2))

Submission status: Accepted

Solution code:
inputs = input()

a = inputs

ans = 0
while len(a) > 1:
    if a[0] == a[-1]:
        a = a[1:-1]
    elif a[0] == 'x' and a[-1] != 'x':
        ans += 1
        a += 'x'
    elif a[0] != 'x' and a[-1] == 'x':
        ans += 1
        a = 'x' + a
    elif a[0] != a[-1]:
        ans = -1
        break
        
print(ans)

Submission status: Accepted

Solution code:
s = raw_input()

replaced = s.replace('x', '')
l = len(replaced)
halfl = int(l / 2)

if l > 1 and replaced[:halfl] != replaced[-halfl:][::-1]:
  print "-1"
  exit()

xset = {}
i = 0
for c in s:
  if c == 'x':
    d = min(i, l-i)
    if i != l-i:
      xset[d] = xset.get(d, 0) + [-1, 1][i < l-i]
  else:
    i += 1

ans = 0
for v in xset.itervalues():
  ans += abs(v)
print ans

Submission status: Accepted

Solution code:
s = raw_input()

def precheck():
    res = ""
    for c in s:
        if c != "x":
            res += c
    if not res:
        return 1
    if res != "".join(reversed(res)):
        return 0
    return res



def calc(ns):
    lf = ""
    rf = ""
    
    vtype = len(ns) % 2

    pos = 0

    cnt = 0
    while cnt < len(ns)/2:
        lf += s[pos]
        if s[pos] == "x":
            pass
        else:
            cnt+=1
        pos+=1
    
    if vtype:
        while s[pos] == "x":
            lf += s[pos]
            pos+=1
        pos+=1
    else:
         while s[pos] == "x":
            pos+=1

   

    rf = "".join(reversed(s[pos:]))


    lx = 0
    rx = 0
    res = 0
    while 1:

        if lx >= len(lf):
            res += len(rf)-rx
            break
        if rx >= len(rf):
            res += len(lf)-lx
            break

        rrr = rf[rx] == "x"
        lll = lf[lx] == "x"

        if rrr and lll:
            rx+=1
            lx+=1
        elif rrr and not lll:
            rx += 1
            res += 1
        elif not rrr and lll:
            lx += 1
            res += 1
        else:
            rx += 1
            lx += 1
    return res

pc = precheck()
if pc == 1:
    print 0
elif not pc:
    print -1
else:
    print calc(pc)

Submission status: Accepted

Solution code:
import sys
import re
import math

S = input()
Sd = re.sub('x', '', S)
Sd_rev = Sd[::-1]

if Sd != Sd_rev:
    print(-1)
    sys.exit()

x_n = re.split('[abcdefghijklmnopqrstuvwyz]', S)
res = 0
for i in range(0, len(x_n)):
    i_rev = len(x_n) - i - 1
    res += int(math.fabs(len(x_n[i]) - len(x_n[i_rev])))
print(res // 2)

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
s=input()
p=''.join(s.split('x'));
if p==p[::-1]:
    a=[0]
    for i in range(len(s)):
        if s[i]=='x':
            a[-1]+=1
        else:
            a.append(0)

    for i in range(len(a)//2):
        a[i]=abs(a[i]-a[-i-1])
    print(sum(a[:len(a)//2]))
else:
    print(-1)

Submission status: Accepted

Solution code:
import functools


def check(s):
    result = 0
    sss = s

    while True:
        if len(sss) <= 1:
            break
        head = sss[0]
        tail = sss[-1]
        if head == tail:
            sss = sss[1:-1]
            continue
        if head == "x":
            result += 1
            sss = sss[1:]
            continue
        if tail == "x":
            result += 1
            sss = sss[:-1]
            continue
        return -1
    return result

def main():
    n = check(input())
    print(n)

    # total = functools.reduce(lambda a, b: a * (2 if b % 2 == 1 else 1),A)
    # print(pow(3 ,len(A)-1)*total)
    # # print(123)
    # b = [int(a) for a in input().split(" ")]
    # print(b)
    # [input() for a in range(3)]


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

s=list(input())


l=0
r=len(s)-1

count=0
while True:
    #print(s,l,r)
    if s[l]!=s[r]:
        if s[l]=="x":
            #s.insert(r+1,"x")
            l+=1
        elif s[r]=="x":
            #s.insert(l,"x")
            r-=1
            #l+=1
        else:
            res=-1
            break

        #print(s,l,r)


        count+=1
    else:
        r-=1
        l+=1

    if l>=r:
        res=count
        break


print(res)

Submission status: Accepted

Solution code:
# C - Inserting 'x'
s = input()

cnt = 0
l = 0
r = len(s)-1
while r>l:
    if ( s[l]==s[r] ):
        l+=1
        r-=1
        #print("same")
    else:
        if ( s[r]=="x" ):
            cnt+=1
            r-=1
            #print("r")
        elif ( s[l]=="x" ):
            cnt+=1
            l+=1
            #print("l")
        else:
            cnt=-1
            break

print(cnt)

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))
def plis(a): return " ".join(list(map(str, a)))


s = input()
ans = 0
s_nox = s.replace('x', '')
if(s_nox != s_nox[::-1]): ans = -1
else:
    need = (len(s_nox)+1) // 2
    s = list(s)
    s_rev = s[::-1]
    si = 0
    srevi = 0
    si_tmp = -1
    srevi_tmp = -1
    for i in range(need):
        si_tmp = 0
        srevi_tmp = 0
        while(s[si] == 'x'):
            si += 1
            si_tmp += 1
        while(s_rev[srevi] == 'x'):
            srevi += 1
            srevi_tmp += 1
        ans += abs(si_tmp - srevi_tmp)
        si += 1
        srevi += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()

s_wo_x = s.replace("x", "")
for i in range(len(s_wo_x)):
    if s_wo_x[i] != s_wo_x[len(s_wo_x)-i-1]:
        print("-1")
        break
else:
    i = 0
    j = len(s)-1
    ans = 0
    while(i < j):
        if s[i] == "x" and s[j] != "x":
            i += 1
            ans += 1
        elif s[i] != "x" and s[j] == "x":
            j -= 1
            ans += 1
        elif s[i] == s[j]:
            i += 1
            j -= 1
            
    print(ans)

Submission status: Accepted

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

s = raw_input()

#alist = [chr(i) for i in range(97,97+26)]
#anum = []

#for al in alist:
#    num = s.count(al)
#    anum.append(num)

status = 0

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



print status

Submission status: Accepted

Solution code:
import sys
s = raw_input()

ss = s.replace("x","")

if(ss == ss[::-1]):
    pass

else:
    print "-1"
    sys.exit()


count = 0

right = 0
left = len(s)-1

for i in range(len(s)*2):

    if ( s[right] != s[left]):
        count += 1
        if(s[right]!="x"):
            right -= 1

        else:
            left += 1
            
    right += 1
    left -= 1
    if(right>=left):
        print count
        sys.exit()

Submission status: Accepted

Solution code:
def ch(s):
	a = True
	for k, x in enumerate(s):
		if x != s[-k-1]:
			a = False
			break
	return a

s = input()
ns = s.replace("x", "")
if ch(ns):
	a = [0]*(len(ns)+1)
	k = 0
	b = True
	for i in s:
		if b:
			if i == 'x':
				a[k] += 1
			else:
				b = False
				k += 1
		else:
			if i == 'x':
				b = True
				a[k] += 1
			else:
				k += 1
	k = 0
	for i in range(len(a)//2):
		if a[i] != a[-i-1]:
			k += abs(a[i] - a[-i-1])
	print(k)
else:
	print(-1)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
non_x = []
s_notx = []
for i, s in enumerate(S):
    if s != 'x':
        non_x.append(i)
        s_notx.append(s)
s_notx = ''.join(s_notx)
if s_notx != s_notx[::-1]:
    print(-1)
    quit()
if not non_x:
    print(0)
    quit()
ans = 0
L = len(non_x)
if L % 2 == 0:
    left, right = L // 2 - 1, L // 2
else:
    left = right = L // 2
left, right = left - 1, right + 1
for i in range(left + 1):
    l, r = left - i, right + i
    ans += abs((non_x[l + 1] - non_x[l]) - (non_x[r] - non_x[r - 1]))
ans += abs(non_x[0] - (N - non_x[-1] - 1))
print(ans)

Submission status: Accepted

Solution code:
def solve(s) :
    f = 0
    b = len(s)-1
    ans = 0
    while True :
        if abs(f-b) <= 1 and s[f] == s[b]:
            return ans
        elif s[f] != s[b] :
            if s[f] == 'x' :
                f += 1
                ans += 1
            elif s[b] == 'x' :
                b -= 1
                ans += 1
            else :
                return -1
        else :
            f += 1
            b -= 1

def main() :
    s = input()
    print(solve(s))

main()

Submission status: Accepted

Solution code:
S = input()

if len(set(S)) == 1:
    print(0)

else:
    top = 0
    bottom = len(S) - 1
    count = 0
    while top < bottom:
        if S[top] == S[bottom]:
            top += 1
            bottom -= 1
        else:
            if S[top] == "x":
                top += 1
                count += 1
            elif S[bottom] == "x":
                bottom -= 1
                count += 1
            else:
                print(-1)
                break
    else:
        print(count)

Submission status: Accepted

Solution code:

s = input()

a = s.replace("x","")

if a == a[::-1]:
	pass
else:
	print("-1")
	exit()

a = s
count1 = 0
count2 = len(a)-1
count = 0
while(count1 < count2):
	if a[count1] == a[count2]:
		count1 += 1
		count2 -= 1
	if a[count1] == "x" and a[count2] != "x":
		count += 1
		count1 += 1
	if a[count1] != "x" and a[count2] == "x":
		count += 1
		count2 -= 1


print(count)

Submission status: Accepted

Solution code:
import sys

S = sys.stdin.readline().strip()

N = len(S)

pl = 0
pr = N

res = 0

while pl < pr:
    cnt = 0

    while pl < N and S[pl] == 'x':
        cnt += 1
        pl += 1

    while 0 < pr and S[pr - 1] == 'x':
        cnt -= 1
        pr -= 1

    res += abs(cnt)

    if 0 < pr and pl < N and S[pl] != S[pr - 1]:
        print(-1)
        sys.exit()

    pl += 1
    pr -= 1

print(res)

Submission status: Accepted

Solution code:
# C
s = list(input())

x_list = []
x_cnt = 0
nonx_list = []

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

x_list.append(x_cnt)

if "".join(nonx_list) == "".join(reversed(nonx_list)):    
    res = 0
    for i in range(len(x_list) // 2):
        res += max(x_list[i], x_list[len(x_list)-i-1]) - min(x_list[i], x_list[len(x_list)-i-1])
    print(res)
else:
    print(-1)

Submission status: Accepted

Solution code:
# coding: utf-8

import math
import fractions
import heapq
import collections
import re
import array
import bisect

from collections import Counter, defaultdict


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]


def palindrome(s):
    for i in range(len(s)):
        if not s[i] == s[-i-1]:
            return False
    return True


def main():
    s = input().strip()
    i = 0
    sdx = ""
    x = defaultdict(int)
    for c in s:
        if c == "x":
            x[i] += 1
        else:
            sdx += c
            i += 1
    if not palindrome(sdx):
        return -1

    cnt = 0
    k = list(x.keys())
    for d in k:
        cnt += max(0, x[d] - x[i-d])
    return cnt


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

Submission status: Accepted

Solution code:
def solve(s):
    r = s[::-1]
    l = len(s)
    sn, rn = 0, 0
    ins = 0

    while True:
        if sn + rn > l:
            return ins
        sc = s[sn]
        rc = r[rn]
        if sc == rc:
            sn += 1
            rn += 1
            continue
        if sc == 'x':
            sn += 1
            ins += 1
            continue
        if rc == 'x':
            rn += 1
            ins += 1
            continue
        return -1


print(solve(input()))

Submission status: Accepted

Solution code:
s = list(input())

c = []
a = [0]
for i in range(len(s)):
    if s[i] != 'x':
        a.append(i)
        c.append(s[i])
a.append(len(s) - 1)
ans = 0
for i in range(len(c)):
    if c[i] != c[-i - 1]:
        ans = -1
        break
if ans != -1:
    x = []
    for i in range(len(a) - 1):
        x.append(a[i + 1] - a[i])
    for i in range(len(x) // 2):
        ans += abs(x[i] - x[-i - 1])
print(ans)

Submission status: Accepted

Solution code:
def solve():
    s = raw_input()
    n = len(s)
    ans = 0
    i, j = 0, n - 1
    while i < j:
        if s[i] != s[j]:
            if s[i] == 'x':
                ans += 1
                i += 1
            elif s[j] == 'x':
                ans += 1
                j -= 1
            else:
                return -1
        else:
            i += 1
            j -= 1
    return ans

print solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
S_inv = S[::-1]
if S == S_inv:
	print(0)
else:
	i = 0
	j = len(S) - 1
	cnt = 0
	while i <= j:
		if S[i] == S[j]:
			i += 1
			j -= 1
		else:
			if S[i] == 'x':
				i += 1
				cnt += 1
			elif S[j] == 'x':
				j -= 1
				cnt += 1
			else:
				cnt = -1
				break
	print(cnt)

Submission status: Accepted

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


S = list(input())


i = 0
j = len(S) - 1

insert = 0

while True:
	if i >= j:
		break

	elif S[i] == S[j]:
		i += 1
		j -= 1

	elif S[i] == 'x':
		i += 1
		insert += 1

	elif S[j] == 'x':
		j -= 1
		insert += 1

	else:
		insert = -1
		break

print(insert)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())

cnt = 0
while len(s) > 1:
  a,b = s[0],s[-1]
  if a == b:
    s.pop()
    s.popleft()
  elif a == 'x':
    cnt += 1
    s.popleft()
  elif b == 'x':
    cnt += 1
    s.pop()
  else:
    break
else:
  print(cnt)
  exit()

print(-1)

Submission status: Accepted

Solution code:
def main():
    s = list(input())
    wox = [i for i in s if i != "x"]
    if len(wox) == 0: return 0
    wowr = list(reversed(wox))
    for i in range(len(wox)//2+1):
        if wox[i] != wowr[i]:
            return -1
    sr = list(reversed(s))
    si = ri = 0
    ret = 0
    while si < len(s) and ri < len(sr):
        if s[si] == sr[ri]:
            si += 1
            ri += 1
        elif s[si] == "x":
            si += 1
            ret += 1
        else:
            ri += 1
            ret += 1
    return (ret + len(s) - min(si,ri))//2
print(main())

Submission status: Accepted

Solution code:
s = raw_input()
l = 0
r = len(s)-1
c = 0
while l<r:
  if s[l]==s[r]:
    l += 1
    r -= 1
  elif s[l]=='x' and s[r]!='x':
    l += 1
    c += 1
  elif s[l]!='x' and s[r]=='x':
    r -= 1
    c += 1
  elif s[l]!='x' and s[r]!='x':
    print -1
    exit(0)
print c

Submission status: Accepted

Solution code:
from collections import deque
def main():
    s = deque(raw_input().strip())
    ans = 0
    while len(s) > 1:
        st = s[0]
        nd = s[-1]
        if st == nd:
            s.pop()
            s.popleft()
        elif st == 'x':
            s.popleft()
            ans += 1
        elif nd == 'x':
            s.pop()
            ans += 1
        else:
            print -1
            return
    print ans
main()

Submission status: Accepted

Solution code:
S = raw_input()

def solve(S):
    i = 0
    j = len(S) - 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:
            return -1
    return ans
            

print solve(S)

Submission status: Accepted

Solution code:
S = input()

comp = []
other = []
cnt = 0
for c in S:
    if c == 'x':
        cnt += 1
    else:
        other.append(c)
        comp.append(cnt)
        cnt = 0
comp.append(cnt)

#print(other)
L = len(other)

dame = False
for i in range(L // 2):
    if other[i] != other[L - i - 1]:
        dame = True

if dame:
    print(-1)
else:
    K = len(comp)
    ans = 0
    for i in range(K // 2):
        ans += abs(comp[i] - comp[K - i - 1])
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string

S = input()
SwoX = S.replace('x', '')

if SwoX != SwoX[::-1]:
    print(-1)
else:
    i = 0
    j = len(S)-1
    ans = 0
    while(i < j):
        if (S[i] == S[j] == 'x') or (S[i] != 'x' and S[j] != 'x'):
            i += 1
            j -= 1
        else:
            ans += 1 
            if S[i] == 'x':
                i += 1
            else:
                j -= 1
    print(ans)

Submission status: Accepted

Solution code:
s= input()


ans = 0
left = 0
right = len(s)-1

while left<right:
    #print(left,right)
    if s[left]==s[right]:
        left+=1
        right-=1
    elif s[left]=="x":
        left+=1
        ans +=1
    elif s[right]=="x":
        right-=1
        ans +=1
    else:
        print(-1)
        exit(0)

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    s = S()
    l = len(s)
    i = 0
    j = l-1
    r = 0
    while i<j:
        if s[i] == s[j]:
            i+=1
            j-=1
        elif s[i] == 'x':
            r += 1
            i+=1
        elif s[j] == 'x':
            r += 1
            j -= 1
        else:
            return -1
    return r


print(main())

Submission status: Accepted

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

l = 0; r = n-1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
l = 0
r = len(s) - 1
cnt = 0
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:
        cnt = -1
        break
print(cnt)

Submission status: Runtime Error

Solution code:
import sys 
S = input()
A = []
for i,s in enumerate(S):
    if s != 'x':  A.append(i)
for i in range(len(A)//2):
    if S[A[i]] != S[A[-i-1]]:
        print(-1)
        sys.exit()
ans = abs((len(S)-1-A[-1])-A[0])
for i in range(len(A)//2):
    cnt = abs(A[i+1]-A[i] - (A[-1-i]-A[-2-i]))
    ans += cnt
print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())
t = [i for i in s if i != "x"]
n = len(t)
if "".join(t) != "".join(t[::-1]):
    print(-1)
else:
    dic = {}
    cnt = 0
    if n % 2 == 0:
        for j in range(len(s)):
            if s[j] != "x":
                cnt += 1
                if cnt == n//2:
                    a = s[:j+1]
                    a = a[::-1]
                if cnt == n//2 + 1:
                    b = s[j:]
                    break
        c = 0
        fa = []
        fb = []
        for k in range(len(a)):
            if a[k] == "x":
                c += 1
            else:
                fa.append(c)
                c = 0
        fa.append(c)
        c = 0
        for p in range(len(b)):
            if b[p] == "x":
                c += 1
            else:
                fb.append(c)
                c = 0
        fb.append(c)
        ans = 0
        for q in range(len(fa)):
            ans += max(fa[q],fb[q]) - min(fa[q],fb[q])
        print(ans)
    else:
        for j in range(len(s)):
            if s[j] != "x":
                cnt += 1
                if cnt == int(n/2) + 1:
                    a = s[:j+1]
                    a = a[::-1]
                    b = s[j:]
                    break
        c = 0
        fa = []
        fb = []
        for k in range(len(a)):
            if a[k] == "x":
                c += 1
            else:
                fa.append(c)
                c = 0
        fa.append(c)
        c = 0
        for p in range(len(b)):
            if b[p] == "x":
                c += 1
            else:
                fb.append(c)
                c = 0
        fb.append(c)
        ans = 0
        for q in range(len(fa)):
            ans += max(fa[q],fb[q]) - min(fa[q],fb[q])
        print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
S=input()
deque_S=deque(S)
flag=0
ans=0
while True:
    if deque_S[0] == deque_S[-1]:
        deque_S.pop()
        deque_S.popleft()
    else:
        if deque_S[0] == 'x':
            ans+=1
            deque_S.pop()
        elif deque_S[-1] == 'x':
            ans+=1
            deque_S.popleft()
        else:
            flag=1
            break
    if len(deque_S)<=1:
        break
if flag==1:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    N = len(S)
    #solve
    left = 0
    right = N-1

    ans = 0
    while True:
        if right==left:
            break
        if S[left]==S[right]:
            left+=1
            right-=1
            continue
        if S[left]!='x' and S[right]!='x':
            print(-1);exit()
        if S[left]=='x':
            left+=1
            ans+=1
        if S[right]=='x':
            right-=1
            ans+=1
    print(ans)


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
s = input()

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

while l < r:
    lx = 0
    while s[l] == "x":
        lx += 1
        l += 1

    rx = 0
    while s[r] == "x":
        rx += 1
        r -= 1

    if s[l] == s[r]:
        cnt += abs(rx-lx)

        l += 1
        r -= 1
    else:
        cnt = -1
        break

print(cnt)