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 main():
    S = input()
    no_x = ''.join(S.split('x'))
    if no_x != no_x[::-1]:
        print(-1)
        return
    left, right = 0, len(S) - 1
    ans = 0
    while left <= right:
        if S[left] != S[right]:
            if S[left] == 'x':
                while S[left] != S[right]:
                    left += 1
                    ans += 1
            else:  # S[right] == 'x'
                while S[left] != S[right]:
                    right -= 1
                    ans += 1
        left += 1
        right -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

 #ランレングス圧縮
def rle_comp(S):
     rle = []
     chain = 0
     for c in S:
         if c == 'x':
             chain += 1
         else:
            rle.append(chain)
            chain = 0
     rle.append(chain)
 
     #print(rle)
     return rle

s = input()
ans = 0

#xを除外した文字列の生成,回文判定
t = [i for i in s if i != 'x']
t2 = t[::-1]
if t != t2:
    ans = -1

#xを挿入する回数の計算
if ans != -1:
    N = len(t)
    #ランレングス圧縮して 'x' の個数ベクトルを求める
    nums = rle_comp(s)

    for i in range(len(nums)//2):
        #左端と右端の要素をを内側に向けて１つずつ比較していく
        ans += max(nums[i], nums[ len(nums)-1-i]) - min(nums[i], nums[ len(nums)-1-i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
snx = S.replace('x', '')
ls = len(S)
if ls == 1 or len(snx) == 0:
    print(0)
    exit(0)
l = 0
r = ls-1

ans = 0
while l < r:
    lxc = 0
    while S[l] == 'x':
        l += 1
        lxc += 1
    rxc = 0
    while S[r] == 'x':
        r -= 1
        rxc += 1
    ans += abs(lxc-rxc)

    if S[l] == S[r]:
        l += 1
        r -= 1
    else:
        print(-1)
        break
else:
    print(ans)

Submission status: Accepted

Solution code:
s = input()
S = []
for i in range(len(s)):
    S.append(s[i])

check = []
for i in S:
    if i == 'x':
        continue
    else:
        check.append(i)

if check == check[::-1]:
    # 先頭の文字と最後の文字が一致しているかを調べ一致していれば、除く、一致していなければ、xを加える
    first = 0
    last = len(S) - 1
    count = 0
    while first < last:
        if S[first] != S[last] and S[first] == 'x':
            count += 1
            first += 1
            continue
        elif S[first] != S[last] and S[last] == 'x':
            count += 1
            last -= 1
            continue
        else:
            first += 1
            last -= 1
            continue
    print(count)

else:
    print(-1)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50

def run():
    S = input()

    l = 0
    r = len(S) - 1

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

if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
S=input()
xs=S.split('x')
xs=''.join(xs)

if xs!=xs[::-1]:
    print(-1)
else:
    ans=0
    N=len(S)
    M=len(xs)
    count1=[0]*(M+1)
    count2=[0]*(M+1)
    rs=S[::-1]
    i=j=0
    a=b=0
    while i<N:
        if S[i]!='x':
            a+=1
        else:
            count1[a]+=1
        if rs[i]!='x':
            b+=1
        else:
            count2[b]+=1
        i+=1
    for a,b in zip(count1,count2):
        ans+=abs(a-b)
    print(ans//2)

Submission status: Accepted

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

# In[20]:


from collections import deque


# In[26]:


s = input()


# In[28]:


S = [x for x in s]
ans = 0
l = 0
r = len(S)-1
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)


# In[ ]:

Submission status: Accepted

Solution code:
S=input()

T=S.replace("x","")
if T!=T[::-1]:
    print(-1)
    exit()

S="*"+S+"*"
U=S[::-1]

a,b=0,0
K=0
for _ in range(len(T)+2):
    old_a,old_b=a,b
    while S[a]=="x":
        a+=1

    while U[b]=="x":
        b+=1

    delta_a=a-old_a
    delta_b=b-old_b

    K+=max(delta_a,delta_b)-min(delta_a,delta_b)

    a+=1
    b+=1
    assert 1

print(K//2)

Submission status: Accepted

Solution code:
S = input()

def solver(S):
    l = 0   
    r = len(S)-1

    ans = 0
    while l < r:
        if S[l] == S[r]:
            l += 1
            r -= 1
            continue

        if S[l] == "x":
            l += 1
            ans += 1
            continue
        
        if S[r] == "x":
            r -= 1
            ans += 1
            continue
        
        return -1
    
    return ans


print(solver(S))

Submission status: Accepted

Solution code:
s=input()
t=s.replace('x','')
n=len(t)
if not(n==1 or t[:n//2]==t[(n+1)//2:][::-1]):
  print(-1)
  exit()
n=len(s)
r=n-1
l=0
ans=0
while r>l:
  nr,nl=r,l
  while s[r]=='x' and r>0:r-=1
  while s[l]=='x' and l<n-1:l+=1
  ans+=abs(nr-r+nl-l)
  while s[r]==s[l] and l<r:
    r-=1
    l+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
ans = 0
while len(s) > 1:
    if s[0] == s[-1]:
        s.popleft()
        s.pop()
    elif s[0] == 'x':
        ans += 1
        s.popleft()
    elif s[-1] == 'x':
        ans += 1
        s.pop()
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

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

t = []
for a in s:
    if a != "x":
        t.append(a)
t = "".join(t)
m = len(t)

sw = 1
for i in range(m//2):
    if t[i] == t[-1-i]:
        pass
    else:
        sw = 0
        
if sw == 0:
    ans = -1
else:
    ans = 0
    i,j = 0,0
    while i+j<n:
        if s[i] == s[-1-j]:
            i += 1
            j += 1
        elif s[i] == "x":
            i += 1
            ans += 1
        elif s[-1-j] == "x":
            j += 1
            ans += 1
            
        else:
            ans = -float("Inf")
            
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
l, r = 0, len(s) - 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:
s = list(input())
l = 0
r = len(s)-1
ans = 0
while l < r:
    if s[l] == s[r]:
        l+=1
        r-=1
    elif s[l] == 'x':
        l+=1
        ans+=1
    elif s[r] == 'x':
        r-=1
        ans+=1
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    S = input()
    noX = S.replace('x','')
    if list(noX) == list(reversed(noX)):
        l = 0
        r = len(S)-1
        count = 0
        while l<r:
            if S[l] == S[r]:
                l += 1
                r -= 1
            else:
                if S[l] == 'x':
                    count += 1
                    l += 1
                else:
                    count += 1
                    r -= 1
        print(count)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
s2 = ""
for i in s:
    if i != "x":
        s2 += i
if s2 != s2[::-1]:
    print(-1)
else:
    temp = s2[:(len(s2) + 1) // 2]
    ans = 0
    num1 = 0
    num2 = len(s) - 1
    for i in range(len(temp)):
        count1 = 0
        while s[num1] != temp[i]:
            count1 += 1
            num1 += 1
        else:
            num1 += 1
        count2 = 0
        while s[num2] != temp[i]:
            count2 += 1
            num2 -= 1
        else:
            num2 -= 1
        ans += abs(count1 - count2)
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
ans = 0
f = 0
b = -1
while True:
    if f - b > n - 1:
        break
    nf = s[f] in ("x", s[b])
    nb = s[b] in ("x", s[f])
    ans += "x" in (s[b], s[f]) and s[b] != s[f]
    if nf + nb < 1:
        ans = -1
        break
    f += nf
    b -= nb
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
ans = 0
f = 0
b = -1
while True:
    if f - b > n - 1:
        break
    if s[f] != s[b]:
        if "x" in (s[f], s[b]):
            ans += 1
            f += "x" == s[f]
            b -= "x" == s[b]
        else:
            ans = -1
            break
    else:
        f += 1
        b -= 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
check_ls = []
for i in range(n):
    if s[i] != 'x':
        check_ls.append(s[i])
n_check = len(check_ls)
is_kaibun = True
for i in range(n_check//2):
    if check_ls[i] != check_ls[-i-1]:
        is_kaibun = False
if not is_kaibun:
    print(-1)
else:
    num_target = -(-n_check//2)
    not_x_count = 0
    for i in range(n):
        if s[i] != 'x':
            not_x_count += 1
        if not_x_count == num_target:
            center_ind = i
            break
    ans = 0
    #if n_check % 2 ==0:
        #right = center_ind+1
        #left = center_ind
    right = n-1
    left = 0
    while right > left:
        if s[right] == 'x' and s[left] != 'x':
            ans += 1
            left -= 1
        elif s[right] != 'x' and s[left] == 'x':
            ans += 1
            right += 1
        right -= 1
        left += 1
    print(ans)

Submission status: Accepted

Solution code:
s=input()

left=0
right=len(s)-1
cnt=0
while left<right:
    if s[left]==s[right]:
        left+=1
        right-=1
    elif s[left]=='x':
        left+=1
        cnt+=1
    elif s[right]=='x':
        right-=1
        cnt+=1
    else:
        print(-1)
        exit(0)
        
print(cnt)

Submission status: Accepted

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

cnt = 0
i = 0
j = N-1
while i < j:
  if S[i] == 'x':
    if S[j] == 'x':
      i += 1
      j -= 1
    else:
      i += 1
      cnt += 1
  else:
    if S[i] == S[j]:
      i += 1
      j -= 1
    elif S[j] == 'x':
      cnt += 1
      j -= 1
    else:
      print(-1)
      exit()
print(cnt)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
nx = S.replace('x', '')
N2 = len(nx)
for i in range(N2):
  if nx[i] != nx[N2-i-1]:
    print(-1)
    exit()

if N == N2:
  # x not inclueded
  print(0)
else:
  cnt = 0
  i = 0
  j = N-1
  while i < j:
    if S[i] == 'x':
      if S[j] == 'x':
        i += 1
        j -= 1
      else:
        i += 1
        cnt += 1
    else:
      if S[i] == S[j]:
        i += 1
        j -= 1
      elif S[j] == 'x':
        cnt += 1
        j -= 1
  print(cnt)

Submission status: Accepted

Solution code:
s = '0' + input() + '0'
prev = 0
n = len(s)
t = ''
L = []
for i in range(n):
  if s[i] != 'x':
    L.append(i)
    t = t[:] + s[i]
flg = True
n = len(t)
for i in range(n//2):
  flg *= (t[i] == t[n-i-1])
for i in range(n-1):
  L[i] = L[i+1] - L[i] - 1
n -= 1
if flg:
  cnt = 0
  for i in range(n//2):
    cnt += abs(L[i] - L[n-i-1])
  print(cnt)
else:
  print(-1)

Submission status: Accepted

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

if check == check[::-1]:
  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':
        s = s[:right+1] + 'x' + s[right+1:]
      else:
        s = s[:left] + 'x' + s[left:]
      left += 1
      ans += 1
  print(ans)
else:
  print(-1)

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    S = getS()
    l, r = 0, len(S) - 1
    ans = 0
    while(r > l + 1):
        if S[l] == S[r]:
            l += 1
            r -= 1
        else:
            if S[l] == "x":
                ans += 1
                l += 1
            elif S[r] == "x":
                ans += 1
                r -= 1
            else:
                print(-1)
                return

    if r == l + 1:
        if S[l] == S[r]:
            l += 1
            r -= 1
        else:
            if S[l] == "x":
                ans += 1
                l += 1
            elif S[r] == "x":
                ans += 1
                r -= 1
            else:
                print(-1)
                return

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    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:
        print(-1)
        exit()
        
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
temp=""
for c in s:
     if c!="x": temp+=c
if temp!=temp[::-1]: print(-1)
elif len(temp)==0:  print(0)
else:
    a=temp[0:len(temp)//2]
    l,r=0,len(s)-1
    ans=0
    while l<r:
        if s[l]==s[r]:
            l+=1
            r-=1
            continue
        elif s[l]=="x": l+=1
        else: r-=1
        ans+=1
    print(ans)

Submission status: Accepted

Solution code:
S = input()

S_rmv =S.replace('x','')

count = 0
if S_rmv == S_rmv[::-1]:
    i = 0
    j = len(S)-1
    while i<j:
        if S[i] == S[j]:
            i +=1
            j -=1
        elif S[i] == 'x':
            i +=1
            count +=1
        elif S[j] == 'x':
            j -=1
            count +=1
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
s=list(input())
ans=0
for i in range(len(s)):
    if len(s)<=1:
        break
    if s[0]==s[-1]:
        del s[0]
        del s[-1]
    else:
        if s[0]=="x":
            ans+=1
            del s[0]
        elif s[-1]=="x":
            ans+=1
            del s[-1]
        else:
            ans=-1
            break
print(ans)

Submission status: Accepted

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

if s1 != s1[::-1]:
    print(-1)
else:
    li = [0] * (len(s1) + 1)
    cnt = 0
    ans = 0
    for i in range(len(s)):
        if s[i] == 'x':
            li[cnt] += 1
        else:
            cnt += 1

    for i in range((len(s1)+1) // 2):
        a = li[i]
        b = li[-i-1]
        ans += abs(a-b)

    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
l, r = 0, len(s) - 1
c = 0
while l < r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == "x":
        l += 1
        c += 1
    elif s[r] == "x":
        r -= 1
        c += 1
    else:
        print(-1)
        exit()
print(c)

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    s = input()
    n = len(s)
    sr = s.replace('x', '')
    if sr == '':
        print(0)
        return
    elif sr != sr[::-1]:
        print(-1)
        return
    cur_l = 0
    cur_r = n-1
    ans = 0
    cnt = 0
    while cur_l < cur_r:
        while s[cur_l] == 'x':
            cnt += 1
            cur_l += 1
        while s[cur_r] != s[cur_l]:
            cur_r-=1
            cnt -= 1
        ans += abs(cnt)
        cur_l += 1
        cur_r -= 1
        cnt = 0
        
    print(ans)   
    
        

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    s = input()
    t = ""

    for c in s:
        if c != "x":
            t += c

    lt = len(t)
    lh = lt // 2
    tf = t[:lh]
    ts = t[lt-lh:][::-1]

    if tf == ts:
        cnt_end = lh + 1

        cnt = 0
        xf = []
        cur = 0
        for c in s:
            if c != "x":
                xf.append(cur)
                cur = 0
                cnt += 1
                if cnt == cnt_end:
                    break
            else:
                cur += 1

        cnt = 0
        xs = []
        cur = 0
        for c in s[::-1]:
            if c != "x":
                xs.append(cur)
                cur = 0
                cnt += 1
                if cnt == cnt_end:
                    break
            else:
                cur += 1

        ans = 0
        for x1, x2 in zip(xf, xs):
            ans += abs(x1 - x2)

        print(ans)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

s = input()
slen = len(s)

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

Submission status: Accepted

Solution code:
def main():
    s = input()
    N = len(s)

    nox = []
    idxs = []
    ans = 0
    for i, c in enumerate(s):
        if c != "x":
            nox.append(c)
            idxs.append(i)
    
    if nox != nox[::-1]:
        print(-1)
        exit()
    
    if len(nox) == 0:
        print(0)
        exit()

    if len(nox)%2 == 0:
        left = len(nox)//2 - 1
        right = len(nox)//2
        
        left_idx = idxs[left]
        right_idx = idxs[right]

    else:
        center = len(nox)//2
        left_idx = idxs[center]
        right_idx = idxs[center]

    while not(left_idx == 0 and right_idx == N-1):
        if s[left_idx] == s[right_idx]:
            if left_idx > 0 and right_idx < N - 1:
                left_idx -= 1
                right_idx += 1
            else:
                if left_idx <= idxs[0] and right_idx >= idxs[-1] and (left_idx > 0 or right_idx < N - 1):
                    ans += 1
                if left_idx > 0:
                    left_idx -= 1
                if right_idx < N-1:
                    right_idx += 1

        elif s[left_idx] == "x":
            ans += 1
            if left_idx > 0:
                left_idx -= 1
        elif s[right_idx] == "x":
            ans += 1
            if right_idx < N-1:
                right_idx += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

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

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

while l < r:
    # print("left", l, "right", r)
    if s[l] == "x" and s[r] == "x":
        l += 1
        r -= 1
    # rにxを足したとみなし、lを進める
    elif s[l] == "x":
        l += 1
        ans += 1
    # lにxを足したとみなし、rを進める
    elif s[r] == "x":
        r -= 1
        ans += 1
    elif s[l] != s[r]:
        print(-1)
        sys.exit()
    else:
        l += 1
        r -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

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

while left < right:
    if s[left] == s[right]:
        left += 1
        right -= 1
    else:
        if s[left] == 'x':
            x_cnt += 1
            left += 1
        elif s[right] == 'x':
            x_cnt += 1
            right -= 1
        else:
            print(-1)
            exit()

print(x_cnt)

Submission status: Accepted

Solution code:
s=input()
i=0
j=len(s)-1
ans=0
while i<j:
    while s[i]==s[j]=="x":
        i+=1
        j-=1
        if i>j:break
    if i>j:break    
    while s[i]=="x":
        i+=1
        ans+=1
        if i>j:break
    if i>j:break
    while s[j]=="x":
        j-=1
        ans+=1
        if i>j:break
    if i>j:break
    if s[i]!=s[j]:
        ans=-1
        break
    i+=1
    j-=1
    if i>j:break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def read():
    S = input().strip()
    return S,


def is_palindrome(seq):
    n = len(seq)
    for i in range(n//2):
        if seq[i] != seq[n-i-1]:
            return False
    return True


def solve(S):
    S = "*" + S + "*"
    T = [a for a in S if a != "x"]
    N = len(T) // 2 + len(T) % 2
    if not is_palindrome(T):
        return -1
    l = 0
    r = len(S)-1
    c = 0
    while c < N:
        if S[l] != "x":
            c += 1
        l += 1
    c = 0
    while c < N:
        if S[r] != "x":
            c += 1
        r -= 1
    
    L = S[:l]
    R = S[r+1:][::-1]
    ans = 0
    l, r = 0, 0
    for i in range(N+1):
        lc = 0
        rc = 0
        while l < len(L) and L[l] == "x":
            lc += 1
            l += 1
        while r < len(R) and R[r] == "x":
            rc += 1
            r += 1
        ans += abs(lc-rc)
        l += 1
        r += 1
    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

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

    l = 0
    r = len(s) - 1

    ans = 0
    while l < r:
        if s[l] == s[r]:
            l += 1
            r -= 1
            continue

        if s[l] == 'x':
            l += 1
            ans += 1
            continue

        if s[r] == 'x':
            r -= 1
            ans += 1
            continue

        print(-1)
        return

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    left = 0
    right = n - 1
    while left <= right:
        if s[left] == s[right]:
            left += 1
            right -= 1
        else:
            if s[left] == "x":
                left += 1
            else:
                right -= 1
            res += 1
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
*s, = input()

i = 0
j = -1
n = len(s)
ans = 0

while n > 0:
    if s[i] == s[j]:
        i += 1
        j -= 1
        n -= 2
    else:
        ans += 1
        if s[i] == 'x':
            i += 1
            n -= 1
        elif s[j] == 'x':
            j -= 1
            n -= 1
        else:
            ans = -1
            break

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())
new = []
for s in S:
    if s=='x':
        continue
    new.append(s)

leng = len(new)
for n in range(leng//2):
    if new[n]!=new[-n-1]:
        print(-1)
        break
else:
    lis = []
    cnt = 0
    for s in S:
        if s!='x':
            lis.append(cnt)
            cnt = 0
        else:
            cnt += 1
    lis.append(cnt)
    leng = len(lis)
    ans = 0
    for i in range(leng//2):
        ans += abs(lis[i]-lis[-i-1])
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

que = deque(s)
ans = 0
while que.__len__() > 1:
    if que[0] == que[-1]:
        que.popleft()
        que.pop()
    elif que[0] == 'x' and que[-1] != 'x':
        que.append('x')
        ans += 1
    elif que[0] != 'x' and que[-1] == 'x':
        que.appendleft('x')
        ans += 1
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


s = input()
from queue import deque
q = deque(s)
ans = 0
while q:
    if q[0]==q[-1]:
        q.popleft()
        if q:
            q.pop()
    elif q[0]=="x":
        ans += 1
        q.popleft()
    elif q[-1]=="x":
        ans += 1
        q.pop()
    else:
        ans = -1
        break
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    S = input()
    r = 0
    l = len(S)-1

    counter = 0
    while (l - r) >= 1 :
        if S[r] == S[l]:
            r += 1
            l -= 1
        elif S[r] == "x":
            r += 1
            counter += 1
        elif S[l] == "x":
            l -= 1
            counter += 1
        else:
            counter = -1
            break
    print(counter)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()

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

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

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

Submission status: Accepted

Solution code:
s = input()
l,r = 0,len(s)-1
c = 0

while l<r:
	if s[l] == s[r]:
		l,r = l+1,r-1
	elif s[l] == 'x':
		l,c = l+1,c+1
	elif s[r] == 'x':
		r,c = r-1,c+1
	else:
		print(-1)
		exit()
print(c)