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:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, W, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
S = input().strip()

q = deque(S)
c = 0
while len(q):
     l = q[0]
     r = q[len(q)-1]

     if l == r:
          if len(q) != 1:
               q.pop()
          q.popleft()
     elif l == 'x':
          q.append('x')
          c += 1
     elif r == 'x':
          q.appendleft('x')
          c += 1
     else:
          print(-1)
          break

print(c)

Submission status: Wrong Answer

Solution code:
# x無視したやつが回文なってればいいんじゃね？
# 真ん中にしたい(x以外の)文字が決まれば、もとのsにインデックスを適応して、左右のxの数の差をとればいい
# と思ったけど差をとっただけだと駄目だな xaaxxxbxxcxxxxbxaxxaxx はこの考えだと3だけど実際は7だし
# もうしかたねぇから無理やりsplitした（アホか？）
# これでいけると思いきや駄目

import re

s = input()
if len(set(s)) == 1:
    # 1文字でのみ構成されている場合
    print(0)
    exit()
t = ""
for sv in s:
    if sv != "x":
        t += sv

if t != t[::-1]:
    # 回文にならない場合
    print(-1)
    exit()

mid_char = t[len(t) // 2]
mid_count = t.count(mid_char) // 2
mid_idx = -1
target_idx = 0
for i in range(len(s)):
    if s[i] == mid_char:
        mid_idx += 1
    if mid_idx == mid_count:
        target_idx = i
        break
ss, tt = s[:target_idx], s[target_idx + 1 :]
tt = tt[::-1]

ss = re.split("[abcdefghijklmnopqrstuvwyz]", ss)
tt = re.split("[abcdefghijklmnopqrstuvwyz]", tt)
ans = 0
if len(ss) != len(tt):
    # ほんまかいな
    print(-1)
    exit()
for i in range(len(ss)):
    ans += abs(len(ss[i]) - len(tt[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
# x無視したやつが回文なってればいいんじゃね？
# 真ん中にしたい(x以外の)文字が決まれば、もとのsにインデックスを適応して、左右のxの数の差をとればいい
# と思ったけど差をとっただけだと駄目だな xaaxxxbxxcxxxxbxaxxaxx はこの考えだと3だけど実際は7だし
# もうしかたねぇから無理やりsplitした（アホか？）
# これでいけると思いきや駄目

import re

s = input()
if len(set(s)) == 1:
    # 1文字でのみ構成されている場合
    print(0)
    exit()
t = ""
for sv in s:
    if sv != "x":
        t += sv

if t != t[::-1]:
    # 回文にならない場合
    print(-1)
    exit()

mid_char = t[len(t) // 2]
mid_count = t.count(mid_char) // 2
mid_idx = -1
target_idx = 0
for i in range(len(s)):
    if s[i] == mid_char:
        mid_idx += 1
    if mid_idx == mid_count:
        target_idx = i
        break
ss, tt = s[:target_idx], s[target_idx + 1 :]
tt = tt[::-1]

ss = re.split("[abcdefghijklmnopqrstuvwyz]", ss)
tt = re.split("[abcdefghijklmnopqrstuvwyz]", tt)
ans = 0
if len(ss) != len(tt):
    print("omggggggggggggg")
    exit()
for i in range(len(ss)):
    ans += abs(len(ss[i]) - len(tt[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
# x無視したやつが回文なってればいいんじゃね？
# 真ん中にしたい(x以外の)文字が決まれば、もとのsにインデックスを適応して、左右のxの数の差をとればいい
s = input()
if len(set(s)) == 1:
    # 1文字でのみ構成されている場合
    print(0)
    exit()
t = ""
for sv in s:
    if sv != "x":
        t += sv

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

mid = t[len(t) // 2]
mid_c = t.count(mid) // 2
leftx, rightx = 0, 0
count = 0
for sv in s:
    if sv == "x" and count < mid_c:
        leftx += 1
    elif sv == "x" and mid_c <= count:
        rightx += 1
    elif sv == mid and count < mid_c:
        count += 1

print(abs(rightx - leftx))

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(0)
    exit()
while True:
    if (len(s) + ftmp) % 2 == 0:
        if findex + ftmp >= (len(s) + ftmp) // 2:
            break
    else:
        if findex + ftmp > (len(s) + ftmp) // 2:
            break

    if s[findex] == s[eindex]:
        if len(s) - eindex == findex:
            cnt = -1
            break
        
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(0)
    exit()
while True:
    if len(s) % 2 == 0:
        if findex + ftmp >= (len(s) + ftmp) // 2:
            break
    else:
        if findex + ftmp > (len(s) + ftmp) // 2:
            break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(0)
    exit()
while True:
    if len(s) % 2 == 0:
        if findex + ftmp >= len(s) // 2:
            break
    else:
        if findex + ftmp > len(s) // 2:
            break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(0)
    exit()
while True:
    if findex + ftmp > len(s) // 2:
        break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        ftmp += 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(0)
    exit()
while True:
    if findex + ftmp > len(s) // 2:
        break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
if len(s) == 1:
    print(1)
    exit()
while True:
    if findex + ftmp > len(s) // 2:
        break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
findex = 0
eindex = -1
cnt = 0
ftmp = 0
while True:
    if findex + ftmp > len(s) // 2:
        break

    if s[findex] == s[eindex]:
        if len(s) // 2 <= abs(eindex) - 1:
            cnt = -1
            break
        elif len(s) // 2 <= findex:
            cnt = -1
            break
        findex += 1
        eindex -= 1
        continue

    else:
        if s[eindex] != 'x' and s[findex] == 'x':
            findex += 1
            cnt += 1
        elif s[findex] != 'x' and s[eindex] == 'x':
            eindex -= 1
            ftmp += 1
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def codef(S):
    # 条件に合う文字列か判別
    s_tmp = S.replace("x", "")
    for i, j in zip(s_tmp, s_tmp[::-1]):
        if i != j:
            return -1

    cnt = 0
    x_cnt = []
    for s in S:
        if s == "x":
            cnt += 1
        else:
            x_cnt.append(cnt)
            cnt = 0
    x_cnt.append(cnt)

    res = sum(x + y for x, y in zip(x_cnt, x_cnt[::-1])) // 2
    return res


if __name__ == "__main__":
    s = input()
    ans = codef(s)
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

i = 0
j = len(s) - 1
ans = 0
while i != j:
    if i > len(s) - 1 or j < 0:
        break
    if s[i] != s[j]:
        if s[i] != "x" and s[j] != "x":
            print(-1)
            exit()
        elif s[i] != "x":
            j -= 1
        else:
            i += 1
        ans += 1
    else:
        i += 1
        j -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

t = s.replace("x", "")
if t == t[::-1]:
    if s == s[::-1]:
        print(0)
    else:
        print(s.count("x"))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
S=input()
A=S.replace("x","")
N=len(A)
if N % 2 ==0:
    if A[:N//2] == A[N//2:]:
        flag=True
    else:
        flag=False
else:
    if A[:N//2] == A[N//2+1:]:
        flag=True
    else:
        flag=False

if flag:
    print(S.count("x"))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = s.replace('x', '')

if t != t[::-1] :
    print(-1)
    
else :
    sp = []
    cnt = 0
    for i in s :
        if i == 'x' :
            sp.append(cnt)
            cnt = 0
        else :
            cnt += 1
    
    ret = 0
    n = len(sp)
    for i in range(n // 2) :
        ret += abs(sp[i] - sp[n-1-i])
        
    print(ret)

Submission status: Wrong Answer

Solution code:
N=list(input())
L=[]
N2=N
for i in range(len(N)):
    if(N[i]!="x"):
        L.append(N[i])

c=0
R=L[::-1]

if(L==R):
    if(len(N)==1):
        print(1)
        exit()
        
    if(N2[0]=="x"):
        j=len(N2)
        if(N2[j-1]!="x"):
            N2.append("x")
            c+=1
    i=1
    while(True):
        if(N2[i]=="x"):
            j=len(N2)-i-1
            if(N2[j]!="x"):
                N2.insert(j,"x")
                c+=1
        i+=1
        if(len(N2)==i):
            break
    print(c)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()

ans = S.count('x')
t = S.replace('x', '')
if t == t[::-1]:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
S = input()

#
can_solve = True
m = [s for s in S if s != 'x']
len_m = len(m)
for i in range(len_m):
    if m[i] != m[len_m - i - 1]:
        can_solve = False
        break

#
if can_solve:
    nums = [0 for _ in range(len_m+1)]
    i, n = 0, 0
    for s in S:
        if s == 'x':
            n += 1
        else:
            nums[i] = n
            i += 1
            n = 0
    nums[i] = n

    len_nums = len_m+1
    ans = 0
    for i in range(len_nums // 2):
        ans += max(nums[i], nums[len_nums - i - 1])
    # print(nums)
    print(ans)


else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s_ = input()
s = []
c = []
cnt = 0
if s_[0]!="x":
    c.append(0)
for i in range(len(s_)):
    if s_[i]!="x":
        c.append(cnt)
        cnt = 0
        s.append(s_[i])
    else:
        cnt += 1
c.append(cnt)

l = len(s)
for i in range(l//2):
    if s[i]!=s[l-i-1]:
        print(-1)
        exit()
ans = 0
l2 = len(c)
for i in range(l2):
    ans += abs(c[i]-c[l2-i-1])

print(ans//2)

Submission status: Wrong Answer

Solution code:
S = list(input())
i, j = 0, len(S) - 1
ans = 0

while (i != j) and (i < len(S)) and (j >= 0):
    if S[i] == S[j]:
        i += 1
        j -= 1

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

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

    else:
        print('-1')
        break

else:
    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

base = ""
cnt = 0

for c in S:
    if c == "x":
        cnt += 1
    else:
        base += c

base_rev = "".join(list(reversed(base)))

if base == base_rev:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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+=max(checklist[i],checklist[-(i+1)])
    print(ans)
    
else:
    print("-1")

Submission status: Wrong Answer

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
print(string)

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+=max(checklist[i],checklist[-(i+1)])
    print(ans)
    
else:
    print("-1")

Submission status: Wrong Answer

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

for i in range(len(s)):
    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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
l = 0
r = 1
ans = 0
exit = (len(s)+1)//2
for i in range(len(s)):
    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

    if l == (r-1) == exit:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
l = 0
r = 1
ans = 0
exit = (len(s)+1)//2
for i in range(len(s)):
    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

    if l == r == exit:
        break

print(ans)

Submission status: Wrong Answer

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

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while 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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i <= len(S)//2 and j <= len(S)//2:
    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: Wrong Answer

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i < len(S)//2+1 and j < len(S)//2+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: Wrong Answer

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i < len(S)//2+1 and j < len(S)//2+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
    loop += 1
    if i == len(S)//2+1 and j == len(S)//2+1 and loop > len(S):
        cnt = -1
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i <= len(S)//2+1 and j <= len(S)//2+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
    loop += 1
    if loop >= len(S):
        cnt = -1
        break
print(cnt)

Submission status: Wrong Answer

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:
                count+=1 
        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:
                count+=1 
        print(count)
    else:
        print("-1")

Submission status: Wrong Answer

Solution code:
s = list(input())

s_list = []
for x in s:
    if x != "x":
        s_list.append(x)

count = len(s) - len(s_list)
s = s_list

if len(s) % 2 == 0:
    if s[:len(s)//2] == list(reversed(s[len(s)//2:])):
        print(count)
        exit(0)
else:
    if s[:len(s)//2] == list(reversed(s[len(s)//2+1:])):
        print(count)
        exit(0)

print(-1)

Submission status: Wrong Answer

Solution code:
s = input()

t = "".join(s.split("x"))
m = len(t)

if (m % 2 and t[:m//2] != t[m//2+1:][::-1]) or (not m%2 and t[:m//2] != t[m//2:][::-1]):
    print(-1)
    exit()
elif m == 0:
    print(0)
    exit()

cnt = 0
for i in range(len(s)):
    if s[i] != "x":
        cnt += 1
    if cnt == (m+1)//2:
        left = s[:i+1]
        right = s[i+1:]
        break

ans = 0
cnt = 0
for i in right:
    if i != "x":
        break
    cnt += 1
right = right[cnt:][::-1]
if m%2:
    ans += cnt
    left = left[:-1]

# rle
rle_left = []
rle_right = []
cnt = 0
for i in left:
    if i == "x":
        cnt += 1
    else:
        if cnt:
            rle_left.append(cnt)
        cnt = 0

for i in right:
    if i == "x":
        cnt += 1
    else:
        if cnt:
            rle_right.append(cnt)
        cnt = 0

if len(rle_left) > len(rle_right):
    rle_right = [0] + rle_right
elif len(rle_left) < len(rle_right):
    rle_left = [0] + rle_left

for i, j in zip(rle_left, rle_right):
    ans += abs(i-j)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():

    s = input()
    t = ""
    xcount = 0
    for c in s:
        if c == "x": xcount += 1
        else: t += c
    if t == t[::-1]:
        return xcount
    return -1

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

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()

  if len(s)%2==0:
    if list(s[len(s)//2:])==list(s[:len(s)//2])[::-1]:
      return 0
  else:
    if list(s[len(s)//2+1:])==list(s[:len(s)//2])[::-1]:
      return 0

  _s=s.replace('x','')
  if len(_s)%2==0:
    if list(_s[len(_s)//2:])==list(_s[:len(_s)//2])[::-1]:

      a=list(s[len(s)//2:])
      b=list(s[:len(s)//2])[::-1]
      ans=0

      while True:
  
        if len(a)>0 and len(b)>0:
          if a[-1]==b[-1]:
            a.pop()
            b.pop()
          else:
            ans+=1
            if a[-1]!='x':
              b.pop()
            else:
              a.pop()
        else:
          if len(a)>0:
            a.pop()
            ans+=1
          elif len(b)>0:
            b.pop()
            ans+=1
          else:
            return ans

    else:
      return -1
  else:
    if list(_s[len(_s)//2+1:])==list(_s[:len(_s)//2])[::-1]:

      a=list(s[len(s)//2+1:])
      b=list(s[:len(s)//2])[::-1]
      ans=0

      while True:
  
        if len(a)>0 and len(b)>0:
          if a[-1]==b[-1]:
            a.pop()
            b.pop()
          else:
            ans+=1
            if a[-1]!='x':
              b.pop()
            else:
              a.pop()
        else:
          if len(a)>0:
            a.pop()
            ans+=1
          elif len(b)>0:
            b.pop()
            ans+=1
          else:
            return ans

    else:
      return -1

# main()
print(main())

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()

  _s=s.replace('x','')
  if len(_s)%2==0:
    if list(_s[len(_s)//2:])==list(_s[:len(_s)//2])[::-1]:

      a=list(s[len(s)//2:])
      b=list(s[:len(s)//2])[::-1]
      ans=0

      while True:
  
        if len(a)>0 and len(b)>0:
          if a[-1]==b[-1]:
            a.pop()
            b.pop()
          else:
            ans+=1
            if a[-1]!='x':
              b.pop()
            else:
              a.pop()
        else:
          if len(a)>0:
            a.pop()
            ans+=1
          elif len(b)>0:
            b.pop()
            ans+=1
          else:
            return ans

    else:
      return -1
  else:
    if list(_s[len(_s)//2+1:])==list(_s[:len(_s)//2])[::-1]:

      a=list(s[len(s)//2+1:])
      b=list(s[:len(s)//2])[::-1]
      ans=0

      while True:
  
        if len(a)>0 and len(b)>0:
          if a[-1]==b[-1]:
            a.pop()
            b.pop()
          else:
            ans+=1
            if a[-1]!='x':
              b.pop()
            else:
              a.pop()
        else:
          if len(a)>0:
            a.pop()
            ans+=1
          elif len(b)>0:
            b.pop()
            ans+=1
          else:
            return ans

    else:
      return -1

# main()
print(main())

Submission status: Wrong Answer

Solution code:
s = input()
num_x = s.count('x')
s_remove_x = s.replace('x', '', num_x)

# print(num_x, s_remove_x)

ans = num_x

# s から x を除いたものが回文になっているか判定
half_len = len(s_remove_x) // 2
for i in range(half_len):
  if s_remove_x[i] != s_remove_x[len(s_remove_x)-1-i]:
    ans = -1

print(ans)

Submission status: Wrong Answer

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()

def check(s):
    if s == s[::-1]:
        return True
    else:
        return False
if check(s):
    print(0)
else:
    t = ''
    for i in range(len(s)):
        if s[i] != 'x':
            t += s[i]
    lst = list(Counter(t).items())
    lst.sort(key = itemgetter(1))
    if len(lst) != 1:
        if lst[0][1] == 1 and lst[1][1] == 1:
            print(-1)
            quit()
    mid = s.index(lst[0][0])
    front = defaultdict(lambda : 0)
    back = defaultdict(lambda : 0)
    for i in range(mid):
        if s[i] == 'x':
            front[i] = 1
    for i in range(mid,len(s)):
        if s[-(i-mid)-1] == 'x':
            back[i - mid] = 1
    ans1 = 0
    for i in front:
        if back[i] != 1:
            ans1 += 1
    ans2 = 0
    for i in back:
        if front[i] != 1:
            ans2 += 1
    ans = ans1 + ans2
    print(ans)