Problem:


Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and  abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx



Sample input:
xabxa


Sample output:
2

Submission status: Wrong Answer

Solution code:
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):
        if(si_tmp == 0): si += 1
        if(srevi_tmp == 0): srevi += 1
        
        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 - srevi)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

s_wo_x = s.replace("x", "")
for i in range(len(s_wo_x)//2):
    if s_wo_x[i] != s_wo_x[-i]:
        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: Wrong Answer

Solution code:
s = raw_input()

def precheck():
    res = ""
    for c in s:
        if c != "x":
            res += c
    
    
    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:
        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 not pc:
    print -1
else:
    print calc(pc)

Submission status: Wrong Answer

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
    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 - srevi)
print(ans)

Submission status: Wrong Answer

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

ss = s.replace('x', '')
l = len(s)
ls = len(ss)
#print(s,len(s),'\n',ss,len(ss))

f=True 
for i in range(ls//2):
    if (ss[i]!=ss[ls-i-1]):
        f = False
        break

if f:
    if l%2==1 and s[l//2]=='X':
        print(l - ls - 2)
    else:
        print(l-ls)
else:
    print(-1)

Submission status: Wrong Answer

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 = []
    for c in s:
        if c == "x":
            x.append(i)
        else:
            sdx += c
            i += 1
    if not palindrome(sdx):
        return -1
    if i % 2:
        return len(x)
    return len(x) - x.count(i // 2)


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

Submission status: Wrong Answer

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


def count_operations(s):
    core = s.replace("x", "")
    if core != core[::-1]:
        return -1
    return s.count("x")


def main():
    print(count_operations(input()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

s = input()
slen = len(s)

sl = list(s)
sl_inv = list(s[::-1])
cnt = 0
while sl:
    if sl[0]== sl_inv[0]:
        sl.pop(0)
        sl_inv.pop(0)
    elif sl[0] == 'x' and sl_inv[0] !='x':
        sl.pop(0)
    elif sl[0] != 'x' and sl_inv[0] == 'x':
        cnt += 1
        sl_inv.pop(0)
    elif sl[0] != 'x' and sl_inv[0] != 'x' and sl[0]!=sl_inv[0]:
        print(-1)
        exit()
    if sl_inv ==[]:
        break
    #print(sl,sl_inv,cnt)
print(cnt+len(sl_inv))

Submission status: Time Limit Exceeded

Solution code:
import collections

s = input()
slen = len(s)
swox = s.replace('x','')
swox_rev = swox[::-1]
if swox != swox_rev:
    print(-1)
else:
    sl = list(s)
    sl_inv = list(s[::-1])
    cnt = 0
    while sl:
        if sl[0]== sl_inv[0]:
            sl.pop(0)
            sl_inv.pop(0)
        elif sl[0] == 'x' and sl_inv[0] !='x':
            sl.pop(0)
        elif sl[0] != 'x' and sl_inv[0] == 'x':
            cnt += 1
            sl_inv.pop(0)
        if sl_inv ==[]:
            break
        #print(sl,sl_inv,cnt)
    print(cnt+len(sl_inv))

Submission status: Time Limit Exceeded

Solution code:
s = input()

if s == s[::-1]:
    print(0)
    exit()

t = []

for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])

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

central_char = t[len(t)//2]

s = list(s)
s.insert(0, '*')
s.append('*')

central_position = 0

for i in range(len(s)):
    if s[i] == central_char:
        central_position = i

left = central_position - 1
right = central_position + 1
x_cnt = 0

while left > 0 or right < len(s)-1:
    if s[max(0, left)] == s[min(len(s)-1, right)]:
        left -= 1
        right += 1
    else:
        x_cnt += 1
        if s[max(0, left)] == 'x':
            left -= 1
        else:
            right += 1

print(x_cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip())
N = len(s)
a = ""
for i in range(N):
    if s[i]!="x":
        a += s[i]
if a==a[::-1]:
    flag = 1
else:
    flag = 0
if flag==1:
    y = list(s[:])
    cur = 0
    cnt = 0
    while True:
        if y[cur]==y[len(y)-1-cur]:
            cur += 1
        else:
            cnt += 1
            if y[cur]=="x":
                y = y[:len(y)-cur]+["x"]+y[len(y)-cur:]
            else:
                y = y[:cur]+["x"]+y[cur:]
        if y==y[::-1]:break
if flag==0:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

S = list(s())
cnt = 0
N = len(S)
for i in range(N):
    if not S:
        break
    N = len(S)
    if S[0] == 'x':
        if S[N-1] == 'x':
            S = S[1:N-1]
        else:
            cnt += 1
            S.pop(0)
    else:
        a = S[0]
        if S[N-1] == 'x':
            cnt += 1
            S.pop(-1)
        elif S[N-1] == a:
            S = S[1:N-1]
        else:
            print(-1)
            exit()
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
a, b = '', ''
for i in range(len(s)):
    if s[i] != 'x':
        a += s[i]
        b = s[i] + b
if a != b:
    print(-1)
ans = 0
while len(s) != 0:
    if s[0] == s[-1]:
        s = s[1:-1]
    elif s[0] == 'x' and s[-1] == 'x':
        if len(s) == 1:
            s = ''
        else:
            s = s[1:-1]
    elif s[0] == 'x':
        s = s[1:]
        ans += 1
    elif s[-1] == 'x':
        s = s[:-1]
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())

ans = 0
while len(S):
    if S[0] == S[-1]:
        if len(S) > 1:
            S.popleft()
            S.pop()
        else:
            S.pop()
    else:
        if S[0] != "x" and S[1] != "x":
            print(-1)
            exit()
        elif S[0] != "x":
            S.appendleft("x")
            ans += 1
        elif S[1] != "x":
            S.append("x")
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
from collections import deque
s=deque(s)
ans=0
while len(list(s)):
  if s[0]==s[-1]:
    if len(list(s))==1:
      s.pop()
      break
    else:
      s.pop()
      s.popleft()
  else:
    if s[0]=="x" and s[-1]!="x":
      s.append("x")
      ans+=1
    elif s[0]!="x" and s[-1]=="x":
      s.appendleft("x")
      ans+=1
    elif s[0]!="x" and s[-1]!="x":
      print(-1)
      exit(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = list(map(str, input().rstrip()))

ns = [x for x in s if x != "x"]


def is_palin(s):
    result = []
    for i in range((len(s) + 1) // 2):
        result.append(s[i] == s[- i - 1])
    return all(result)


if not is_palin(ns):
    print(-1)
    sys.exit()

i = 0
cnt = 0
while not is_palin(s):
    if s[i] != s[- i - 1]:
        if s[i] == "x":
            if i == 0:
                s.append("x")
            else:
                s.insert(-i, "x")
        else:
            s.insert(i, "x")
        cnt += 1
    i += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
cnt=0

def ispalindrome(str): 
  return True if str == str[::-1] else False

while not ispalindrome("".join(s)):
  if 'x' not in s:
    print(-1)
    exit()
  
  if s[0]==s[-1]:
    del s[0]
    del s[-1]
    
  elif s[0]=='x':
    del s[0]
    cnt+=1
  elif s[-1]=='x':
    del s[-1]
    cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
c = 0
while s:
  if s[0] == s[-1]:
    s = s[1:-1]
  else:
    if s[0] == "x":
      s += ["x"]
      c += 1
    elif s[-1] == "x":
      s = ["x"] + s
      c += 1
    else:
      print(-1)
      exit()
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    new_s=""
    for ss in s:
        if ss!='x':
            new_s+=ss
    if new_s[::-1]!=new_s:
        print(-1)
        sys.exit()
    right=len(s)-1
    left=0
    ans=0
    while right-left>0:
        if s[right]!=s[left]:
            if s[right]=='x':
                ans+=1
                right-=1
            elif s[left]=='x':
                ans+=1
                left+=1
        else:
            right-=1
            left+=1
    print(ans)



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

kb = []

for i in range(num):
    kb.append(s[i])

mae = 0
usi = num-1

while mae < usi:
    #print(i)
    if s[mae] != s[usi]:
        if s[mae] == 'x':
            kb.append('x')
            mae += 1
        elif s[usi] == 'x':
            kb.insert(mae,'x')
            usi -= 1
        else:
            print(-1)
            exit()
    else:
        mae += 1
        usi -= 1

print(len(kb)-num)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=s.replace('x','')
ret = -1
if t==t[::-1]:
  ret+=1
  a=list(s)
  b=list(s[::-1])
  while a!=b:
    if len(a)==0:
      ret+=len(b)
      break
    elif len(b)==0:
      ret+=len(a)
      break
    elif a[-1]!=b[-1]:
      if a[-1]=='x':
        del a[-1]
        ret+=1
      elif b[-1]=='x':
        del b[-1]
        ret+=1
    else:
      del a[-1]
      del b[-1]
print(ret//2)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()
s_x = s.replace('x', '')

if len(s) == 1:
	print(0)
	sys.exit()

middle, mod = divmod(len(s_x),2)
middle += mod
if s_x[:middle] != s_x[::-1][:middle]:
	print(-1)
	sys.exit()

pali_orig = s_x[:middle]

x_count_l, x_count_r = [0]*middle, [0]*middle

i = 0
for c in range(middle):
	cnt = 0
	while s[i] != pali_orig[c]:
		cnt += 1
		i += 1
	x_count_l[c] = cnt
	i += 1

i = 0
for c in range(middle):
	cnt = 0
	while s[::-1][i] != pali_orig[c]:
		cnt += 1
		i += 1
	x_count_r[c] = cnt
	i += 1


ans = 0

for i in range(middle):
	ans += abs(x_count_l[i] - x_count_r[i])

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
nox_S = S.replace('x','')
if nox_S == nox_S[::-1]:
  si, ei = 0, N-1
  ans = 0
  while si <= ei:
    cnt1 = 0
    for i in range(si,N):
      if S[i] == 'x':cnt1 += 1
      else:
        si = i+1
        break
    
    cnt2 = 0
    for i in range(ei,-1,-1):
      if S[i] == 'x':cnt2 += 1
      else:
        ei = i-1
        break
    ans += abs(cnt1-cnt2)
  print(ans)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
nox_S = S.replace('x','')
if nox_S == nox_S[::-1]:
  si, ei = 0, -1
  ans = 0
  while si <= N+ei:
    if (S[si] == 'x' and S[ei] != 'x'):
      ans += 1
      si += 1
      ei -= 1
      N += 1
      S = S + 'x'
    elif (S[si] != 'x' and S[ei] == 'x'):
      ans += 1
      si += 1
      ei -= 1
      N += 1
      S = 'x' + S
    else:
      si += 1
      ei -= 1
  print(ans)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = ''
for i in range(len(s)):
    if s[i]!='x':
        t += s[i]
for i in range(len(t)):
    if t[i]!=t[-1-i]:
        print(-1)
        break
else:
    ans = 0
    L = []
    R = []
    prevl = -1
    prevr = len(s)
    for i in range(len(s)):
        if s[i]!='x':
            L.append(i-prevl-1)
            prevl = i
        if s[-1-i]!='x':
            R.append(prevr-(len(s)-1-i)-1)
            prevr = len(s)-1-i
    L.append(len(s)-prevl-1)
    R.append(prevr)
    for i in range(len(L)):
        ans += abs(L[i]-R[i])
    print(ans//2)

Submission status: Time Limit Exceeded

Solution code:
# 2019-11-21 01:26:03(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 

def is_palindrome_in(s):
    s = s.replace('x', '')
    if list(s) == list(reversed(s)):
        return True
    return False

def main():
    s = sys.stdin.readline().rstrip()
    
    if not is_palindrome_in(s):
        print(-1)
        sys.exit()
    
    s = list(s)
    # deque で insertが使えるのがver3.5以降のようです(AtCoder はver3.4)
    # 初めdequeでやっててなぜRuntime Errorになるのか全くわからなかった
    # でもこれだとTLEになってしまうかもしれません

    count = 0
    i, j = 0, 0
    while i < len(s) - 1 - j:
        if s[i] == 'x':
            if s[-1-j] != 'x':
                s.insert(len(s)-j, 'x')
                count += 1
        elif s[-1-j] == 'x':
            s.insert(i, 'x')
            count += 1
        
        i += 1
        j += 1

    print(count)
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
S = input()

answer = 0

def F(s):
    global answer
    if len(s) <= 1:
        return
    if s[0] == 'x':
        if s[-1] == 'x':
            F(s[1:-1])
        else:
            answer += 1
            F(s[1:])
    else:
        if s[-1] == 'x':
            answer += 1
            F(s[:-1])
        elif s[0] == s[-1]:
            F(s[1:-1])
        else:
            print(-1)
            exit()

F(S)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=0
while s:
    a=s[0]
    b=s[-1]
    if a==b:
        s=s[1:-1]
        continue
    elif a=="x":
        s=s[1:]
        ans+=1
        continue
    elif b=="x":
        s=s[:-1]
        ans+=1
        continue
    else:
        print(-1)
        exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
ans=0
while s:
    a=s[0]
    b=s[-1]
    if a==b:
        s=s[1:-1]
    elif a=="x":
        s=s[1:]
        ans+=1
    elif b=="x":
        s=s[:-1]
        ans+=1
    else:
        print(-1)
        exit()

print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()

res=0
l=list(s)
while len(l)>1:
  if l[0]==l[-1]:
    l.pop(0)
    l.pop(-1)
  elif l[0]=="x":
    l.append("x")
    res+=1
  elif l[-1]=="x": 
    l.insert(0,"x")
    res+=1
  else:
    print(-1)
    exit()
    
print(res)

Submission status: Time Limit Exceeded

Solution code:

s = input()

a = ''
for i in range(len(s)):
    if s[i]!='x':
        a+=s[i]

b = ''
for i in range(len(a)):
    b+=a[-1-i]

if a!=b:
    print(-1)
else:
    ss = ''
    for i in range(len(s)):
        ss += s[-1-i]
    i = 0
    j=0
    ans = 0
    while True:
        if i>=len(s):
            ans += len(ss)-j
            break
        elif j>=len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[j]:
            if s[i]=='x':
                i+=1
            else:
                j+=1
            ans += 1
        else:
            i+=1
            j+=1
    print(ans//2)

Submission status: Time Limit Exceeded

Solution code:
s = input()

a = ''
ss = ''
b = ''
for i in range(len(s)):
    ss += s[-1-i]
    if s[i]!='x':
        a+=s[i]
        b = s[i]+b

if a!=b:
    print(-1)
else:
    i = 0
    ans = 0
    while s!=ss:
        if i>=len(s):
            ans += len(ss)-i
            break
        elif i>= len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[i]:
            if s[i]=='x':
                ss = ss[:i]+'x'+ss[i:]
            else:
                s = s[:i]+'x'+s[i:]
            ans += 1
        i+=1
    print(ans//2)

Submission status: Time Limit Exceeded

Solution code:
s = input()

a = ''
for i in range(len(s)):
    if s[i]!='x':
        a+=s[i]

b = ''
for i in range(len(a)):
    b+=a[-1-i]

if a!=b:
    print(-1)
else:
    ss = ''
    for i in range(len(s)):
        ss += s[-1-i]
    i = 0
    ans = 0
    while s!=ss:
        if i>=len(s):
            ans += len(ss)-i
            break
        elif i>= len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[i]:
            if s[i]=='x':
                ss = ss[:i]+'x'+ss[i:]
            else:
                s = s[:i]+'x'+s[i:]
            ans += 1
        i+=1
    print(ans//2)

Submission status: Time Limit Exceeded

Solution code:
S = input()

ans = 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
        ans += 1
    elif S[r] == "x" :
        r -= 1
        ans += 1
    else :
        ans = -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

ans = 0

while S :
    if S[0] == S[-1] :
        S = S[1:-1]
    elif S[0] == "x" :
        S += "x"
        ans += 1
    elif S[-1] == "x" :
        S = "x" + S
        ans += 1
    else :
        ans = -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
S = list(input())
s = copy.copy(S)
ans = 1
i = 0
while True:
    if i >= len(s):
        break
    if s[i] == "x":
        del s[i]
        i -= 1
    i += 1
def main(s):
    for i in range(int((len(s)) / 2)):
        if s[i] != s[len(s) - 1 - i]:
            print(-1)
            return 0
    i = 0
    s = copy.copy(S)
    cnt = 0
    while True:
        if i >= int(len(s) / 2):
            break
        if s[i] != s[len(s) - 1 - i]:
            if s[i] != "x":
                s.insert(i, "x")
            else:
                s.insert(len(s) - i, "x")
            i -= 1
            cnt += 1
        i += 1
    print(cnt)
    return 0
        
            
main(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=s.replace('x','')
if t!=t[::-1] and len(t)!=1:
  print(-1)
  exit()
c=0
while True:
  if s==s[::-1]:
    print(c)
    exit()
  for i in range(len(s)//2):
    if s[i]=='x'and s[len(s)-i-1]!='x':
      s=s[:len(s)-i]+'x'+s[len(s)-i:]
      break
  else:
    for i in range(len(s)//2,len(s)):
      if s[i]=='x'and s[len(s)-i-1]!='x':
        s=s[:len(s)-i-1]+'x'+s[len(s)-i-1:]
        break
  c+=1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from math import ceil

S = list(input())
i = 0
ans = 0

while True:
    if i == ceil((len(S)-1)/2):
        break
    else:
        if S[i] == S[-i-1]:
            i += 1
        elif S[i] == 'x':
            S.insert(len(S)-i,'x')
            ans += 1
        elif S[-i-1] == 'x':
            S.insert(i,'x')
            ans += 1
        else:
            ans = -1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
S=list(raw_input())

cnt=0
while len(S)>1:
	#print S,cnt
	if S[0]==S[-1]:
		S.pop(0)
		S.pop()
	elif S[0]!=S[-1] and S[0]=="x":
		cnt+=1
		S.append("x")
	elif S[0]!=S[-1] and S[-1]=="x":
		cnt+=1
		S.insert(0,"x")
	else:
		print "-1"
		sys.exit()
print cnt

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()
s_no_x = s.replace('x', '')
s_no_x_inv = s_no_x[::-1]

if not s_no_x == s_no_x_inv:
    print(-1)
    sys.exit()

count = 0
for i in range(len(s)):
    if i > len(s):
        break
    if not s[i] == s[len(s) - 1 - i]:
        count += 1
        if s[i] == 'x':
            s = s[:len(s) - i] + 'x' + s[len(s) - i:]
        else:
            s = s[:i] + 'x' + s[i:]
print(count)

Submission status: Time Limit Exceeded

Solution code:

value = list(input())
res = 0
while len(value) != 1 and len(value) != 0:
    zero = value[0]
    one = value[-1]
    if zero == one:
        value.pop(0)
        value.pop()
    else:
        if zero != "x" and one != "x":
            res = -1
            break
        value.extend("x") if zero == "x" else value.insert(0, "x")
        value.pop(0)
        value.pop()
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
value = list(input())
res = 0
while len(value) != 1 and len(value) != 0:
    if value[0] == value[-1]:
        value.pop(0)
        value.pop()
    else:
        if value[0] != "x" and value[-1] != "x":
            res = -1
            break
        value.extend("x") if value[0] == "x" else value.insert(0, "x")
        value.pop(0)
        value.pop()
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
value = list(input())
res = 0
while True:
    if len(value) == 1 or len(value) == 0:
        break
    elif value[0] != value[-1] and value[0] != "x" and value[-1] != "x":
        res = -1
        break
    elif value[0] == value[-1]:
        value.pop(0)
        value.pop()
    else:
        value.extend("x") if value[0] == "x" else value.insert(0, "x")
        value.pop(0)
        value.pop()
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
def calc(value):
    res = 0
    while True:
        if len(value) == 1 or len(value) == 0:
            break
        elif value[0] != value[-1] and value[0] != "x" and value[-1] != "x":
            res = -1
            break
        elif value[0] == value[-1]:
            value.pop(0)
            value.pop()
        else:
            value.extend("x") if value[0] == "x" else value.insert(0, "x")
            value.pop(0)
            value.pop()
            res += 1
    return res


def main():
    value = list(input())
    print(calc(value))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

s = list(input())
# s_rem = list(s)
#
# rem_num = 0
# for i in range(len(s)):
#     if(s[i] == "x"):
#         rem_num += 1
#
# for i in range(rem_num):
#     s_rem.remove("x")
#
output = 0
# length = len(s_rem)
# for i in range(length//2):
#     if(s_rem[i] != s_rem[length - 1 - i]):
#         output = -1
#         break

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


i = 0
j = len(s) - 1

if(output == 0):
    while(i < j):
        if(s[i] == 'x'):
            if(s[j] == 'x'):
                i += 1
                j += -1
            else:
                i += 1
                output += 1
        else:
            if(s[j] == 'x'):
                j += -1
                output += 1
            else:
                i += 1
                j += -1

print(output)

Submission status: Time Limit Exceeded

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

s = list(input())
s_rem = list(s)

rem_num = 0
for i in range(len(s)):
    if(s[i] == "x"):
        rem_num += 1

for i in range(rem_num):
    s_rem.remove("x")

output = 0
length = len(s_rem)
for i in range(length//2):
    if(s_rem[i] != s_rem[length - 1 - i]):
        output = -1
        break

i = 0
j = len(s) - 1

if(output == 0):
    while(i < j):
        if(s[i] == 'x'):
            if(s[j] == 'x'):
                i += 1
                j += -1
            else:
                i += 1
                output += 1
        else:
            if(s[j] == 'x'):
                j += -1
                output += 1
            else:
                i += 1
                j += -1

print(output)

Submission status: Time Limit Exceeded

Solution code:
def palindrome(str):
  return True if str == str[::-1] else False

str, count, i = input(), 0, 1
default_strlen = len(str)

if palindrome(str):
  print(count)
else:
  while not palindrome(str):
    if i > default_strlen and count == 0:
      count = -1
      break

    check_str = str[0:i]

    if not palindrome(check_str):
      appended_check_str = check_str + 'x'
      returned_appended_check_str = 'x' + check_str

      if palindrome(appended_check_str) or palindrome(returned_appended_check_str):
        if palindrome(appended_check_str):
          str = appended_check_str + str[i:]
        elif palindrome(returned_appended_check_str):
          str = returned_appended_check_str + str[i:]
        count += 1

    i += 1

  print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
i,j = 0,len(s)-1
flag = True
count=0

while(i<=j):
    if s[i]==s[j]:
        i+=1
        j-=1
        continue
    else:
        if s[i]=='x':
            s.insert(j+1,'x')
        elif s[j]=='x':
            s.insert(i,'x')
        else:
            flag = False
            break
        count+=1
        i+=1

print(count if flag else -1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
sList = []

for i in range(len(s)):
    sList.append(s[i])

while True:
    if 'x' in sList:
        sList.remove('x')
    else:
        break

end = 0
while True:
    if len(sList) == 0 or len(sList) == 1:
        break
    elif sList[0] != sList[-1]:
        print(-1)
        end = 1
        break
    else:
        del sList[-1]
        del sList[0]  

        
count = 0
F0 = 0
R0 = 0
if end == 0:
    sList2 = []
    sListF = []
    sListR = []
    
    for i in range(len(s)):
        sList2.append(s[i])
        sListF.append(s[i])
        sListR.append(s[-(i+1)])
        
    while True:
        if 'x' in sList2:
            sList2.remove('x')
        else:
            break
    
    
    if len(sList2)%2==0:
        number = len(sList2)//2
    else:
        number = (len(sList2)+1)//2
    
    for i in range(number):
        F = sListF.index(sList2[i])-F0
        R = sListR.index(sList2[i])-R0
        count += max([F, R])-min([F, R])
        F0 += F
        R0 += R


    
    print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s_x = [i for i in s if 'x' not in i]

cnt = 1
a, b = ' ', ''
for c in s:
    if c == 'x':
        pass
    elif c != s_x[len(s_x)-cnt]:
        print(-1)
        break
    else:
        cnt += 1
    d = len(s_x)/2
    if cnt <= d+1:
        a += c
        if c == 'x' and cnt > d:
            b += c
    elif cnt > d:
        b += c
else:
    b = ' ' + b[::-1]
    ans = 0
    i, j = 0, 0
    while i < len(a)-1 or j < len(b)-1:
        if i >= len(a):
            i -= 1
        if j >= len(b):
            j -= 1
        if a[i] == b[j]:
            i += 1
            j += 1
        elif a[i] == 'x':
            i += 1
            ans += 1
        elif b[j] == 'x':
            j += 1
            ans += 1
    print(ans)