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:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

S = input()[:-1]
N = len(S)
ans = 0
left = 0
right = N-1

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)
        break
else:
    print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
s = input()
ns = s.replace("x","")
l = len(ns)

if ns[:l//2] == ns[l-1:(l+1)//2-1:-1]:
    ans = 0
    left = 0
    right = len(s) - 1
    while left <= right:
        if s[left] != s[right]:
            if s[left] == "x":
                right += 1
            else:
                left -= 1
            ans += 1
        left += 1
        right -= 1
else:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
import sys

s = list(map(str, input()))

ans = 0
while s:
	if s[0] == s[-1]:
		s.pop(0)
		try:
			s.pop()
		except IndexError:
			break
	elif s[0] == 'x':
		ans += 1
		s.pop(0)
	elif s[-1] == 'x':
		ans += 1
		s.pop()
	else:
		print(-1)
		sys.exit()

print(ans)

Submission status: Accepted

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

x_count = []
cnt = 0
for i in range(len(s)):
	if s[i] != 'x':
		x_count.append(cnt)
		cnt = 0
	else:
		cnt += 1
x_count.append(cnt)

ans = 0
for i in range(len(x_count)):
	ans += abs(x_count[i] - x_count[-(1+i)])

print(ans//2)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
s = readline().rstrip().decode('utf-8')

t = ""
for i in s:
    if not i == "x":
        t += i

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

else:
    ans = 0
    left,right = 0,len(s)-1
    while True:
        if right - left <= 0:
            break
        if s[left] == "x" and s[right] != "x":
            ans += 1
            left += 1
        elif s[left] != "x" and s[right] == "x":
            ans += 1
            right -= 1
        else:
            left += 1
            right -= 1
    print(ans)

Submission status: Accepted

Solution code:
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(i<len(s)-1 and s[i]=='x'):
        i+=1
        xi+=1
#        print(i,xi)

    while(j>0 and s[j]=='x'):
        j-=1
        yj+=1
#        print(j,yj)

#    print(i,xi,j,yj)
    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
l = 0
r = len(s) - 1

cnt = 0
while len(s) != 0:
    if s[0] == s[-1]:
        s = s[1:-1]
    elif s[0] == 'x':
        s += 'x'
        cnt += 1
    elif s[-1] == 'x':
        s = 'x' + s
        cnt += 1
    else:
        print(-1)
        sys.exit()

print(cnt)

Submission status: Accepted

Solution code:
import sys
s = input()

l = s.replace("x", "")
ll = len(l) // 2
lr = len(l) - ll
if l[:ll] != l[lr:][::-1]:
    print(-1)
    sys.exit()

xcnt = [0] * lr
c = 0
cnt = 0
i = 0
while c != lr:
    if s[i] != "x":
        xcnt[c] = cnt
        c += 1
        cnt = 0
    else:
        cnt += 1
    i += 1
    
c = 0
cnt = 0
i = len(s) - 1
while c != lr:
    if s[i] != "x":
        xcnt[c] -= cnt
        xcnt[c] = abs(xcnt[c])
        c += 1
        cnt = 0
    else:
        cnt += 1
    i -= 1

print(sum(xcnt))

Submission status: Accepted

Solution code:
S = input()
S1 = [s for s in S if s != 'x']
# print(S, S1)

ans = 0
ls = len(S)
l = 0
r = ls - 1
while l < r:
    if S[l] == S[r]:
        l += 1
        r -= 1
        continue
    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:
from collections import deque

s = input()
s_list = deque(list(s))

x_count = 0
flag = True
while len(s_list) > 1:

    s_head = s_list.popleft()
    s_tail = s_list.pop()

    if s_head == s_tail:
        continue
    elif s_head == "x":
        x_count += 1
        s_list.append(s_tail)
        continue
    elif s_tail == "x":
        x_count += 1
        s_list.appendleft(s_head)
        continue
    else:
        flag = False
        break

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

Submission status: Accepted

Solution code:
import sys


S = input()
length_S = len(S)

cursor_head = 0
cursor_tail = -1

ret = 0

while length_S + cursor_tail > cursor_head:
  if S[cursor_head] == S[cursor_tail]:
    cursor_head += 1
    cursor_tail -= 1
  else:
    if S[cursor_head] == 'x':
      cursor_head += 1
      ret += 1
    elif S[cursor_tail] == 'x':
      cursor_tail -= 1
      ret += 1
    else:
      print(-1)
      sys.exit()
    
print(ret)

Submission status: Accepted

Solution code:
S = input()
ans = 0
while True:
  if len(S) <= 1:
    break
  if S[0] == 'x' and S[-1] != 'x':
    ans += 1
    S = S[1:]
  elif S[0] != 'x' and S[-1] == 'x':
    ans += 1
    S = S[:-1]
  elif S[0] == S[-1]:
    S = S[1:-1]
  else:
    ans = -1
    break
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
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)
    exit()
print(c)

Submission status: Accepted

Solution code:
s=input()
l=0
r=len(s)-1
ans=0
while r>l:
    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
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)
          exit()

print(c)

Submission status: Accepted

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input().strip()

# 左右から詰めていく
l = 0
r = len(S)-1
ans = 0
while l != r and l <= r:
  if S[l] == 'x' and S[r] != 'x':
    l += 1
    ans += 1
  elif S[l] != 'x' and S[r] == 'x':
    r -= 1
    ans += 1
  elif S[l] != S[r]:
    print(-1)
    exit()
  else:
    l += 1
    r -= 1
  
print(ans)

Submission status: Accepted

Solution code:
s = input()
if len(s) == 1:
  print(0)
  exit()
l = 0
r = len(s) - 1
cnt = 0
while l < r:
  if s[l] == s[r]:
    l += 1
    r -= 1
  else:
    if 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:
import sys
input = sys.stdin.readline

S = list(input().rstrip())
L = len(S)

T = []
Inds = [-1]
for i, s in enumerate(S):
    if s != "x":
        T.append(s)
        Inds.append(i)
Inds.append(L)

if T != T[::-1]:
    print(-1)
else:
    R = len(T)
    ans = 0
    for i in range((R+1)//2):
        l = Inds[i+1] - Inds[i]
        r = Inds[-i-1] - Inds[-i-2]
        ans += abs(r-l)
    print(ans)

Submission status: Accepted

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

s = input()
# めんどくさいけどここで確実になるときのやつと回文にならんときのやつを判定すっか
if len(s) == 1:
    print(0)
    exit()
t = ""
for sv in s:
    if sv != "x":
        t += sv
if t != t[::-1]:
    print(-1)
    exit()

l, r = 0, len(s) - 1
ls, rs = "", ""
ans = 0
x = False
while l < r:
    if s[l] == s[r]:
        ls += s[l]
        rs += s[r]
        l += 1
        r -= 1
    else:
        if s[l] == "x":
            while l < r and s[l] != s[r]:
                l += 1
                ans += 1
        else:
            while l < r and s[r] != s[l]:
                r -= 1
                ans += 1
print(ans)

Submission status: Accepted

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

    if s[findex] == s[eindex]:
        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
            cnt += 1
        else:
            cnt = -1
            break

print(cnt)

Submission status: Accepted

Solution code:
s = input()
ans = 0
while s:
  if s[0] == s[-1]:
    s = s[1:-1]
  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: Accepted

Solution code:
import re
s = input()
t = s.replace("x", "")
if t != t[::-1]:
  print(-1)
else:
  k = re.split('[abcdefghijklmnopqrstuvwyz]', s)
  l = len(k)
  ans = 0
  for i in range(l//2):
    ans += abs(len(k[i]) - len(k[l-1-i]))
  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:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
S = input()
rev_S = S.replace("x", "")

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

res = 0

N = len(S)
l, r = 0, N - 1
while l < r:
    if S[l] == S[r]:
        l, r = l + 1, r - 1
    elif S[l] == "x":
        l += 1
        res += 1
    elif S[r] == "x":
        r -= 1
        res += 1

print(res)

Submission status: Accepted

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

Solution code:
def main():
    s = input()
    chars = list(s)
    length = len(s)
    l = 0
    r = length - 1
    xcount = 0
    while l <= r and 0 <= r and l < length:
        if chars[l] == chars[r]:
            l += 1
            r -= 1
            continue

        if chars[l] == 'x':
            xcount += 1
            l += 1
            continue

        if chars[r] == 'x':
            xcount += 1
            r -= 1
            continue
        print(-1)
        return

    print(xcount)


main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s = list(input())[: -1]
i = 0
j = len(s) - 1
res = 0
while i < j:
  if s[i] != s[j]:
    res += 1
    if s[i] == "x":
      i += 1
    elif s[j] == "x":
      j -= 1
    else:
      print(-1)
      exit(0)
  else:
    i += 1
    j -= 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
t=s.replace('x','')
if t!=t[::-1]:
    print(-1)
else:
    l=[]
    k=0
    for c in s:
        if c=='x':
            k+=1
        else:
            l.append(k)
            k=0
    l.append(k)

    ans=0
    for i in range(len(l)//2):
        ans+=abs(l[i]-l[-(i+1)])

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
l = 0
r = len(s)-1
c = 0
if s.count("x") == len(s):
  print(0)
  exit()
while l < r:
  c_l = 0
  c_r = 0
  while s[l] == "x":
    c_l += 1
    l += 1
  while s[r] == "x":
    c_r += 1
    r -= 1
  if s[l] != s[r]:
    print(-1)
    exit()
  c += abs(c_l-c_r)
  l += 1
  r -= 1
print(c)

Submission status: Accepted

Solution code:
s = input()

i = 0
j = len(s) - 1
ans = 0
while i != j:
    if abs(i - j) == 1:
        if s[i] == s[j]:
            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: Accepted

Solution code:
import math
s = input().strip()
A = []
B = [0 for _ in range(len(s)+1)]
y = ""
for i in range(len(s)):
    if s[i]!="x":
        y += s[i]
        A.append(i)
        B[i+1] = B[i]
    else:
        B[i+1] = B[i]+1
flag=0
cnt = 0
for i in range(len(y)//2):
    if y[i]!=y[-(i+1)]:
        flag = 1
        cnt = -1
        break
if flag==0 and len(y)>0:
    if B[A[0]]!=B[-1]-B[A[-1]]:
        cnt += abs(B[A[0]]-(B[-1]-B[A[-1]]))
    for i in range(1,math.ceil(len(A)/2)):
        if B[A[i]]-B[A[i-1]] != B[A[-i]]-B[A[-(i+1)]]:
            cnt += abs(B[A[i]]-B[A[i-1]]-(B[A[-i]]-B[A[-(i+1)]]))
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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


def main():
    s = input().strip()
    import collections
    q = collections.deque(s)
    ans = 0
    while q:
        s, t = q[0], q[-1]
        if s == t:
            q.popleft()
            if q:
                q.pop()
        elif s == "x" and t != "x":
            ans += 1
            q.popleft()
        elif s != "x" and t == "x":
            ans += 1
            q.pop()
        else:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    s = input().strip()
    l, r = 0, len(s)-1
    ans = 0
    while l < r:
        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:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()
_S=[]
for s in S:
    if s != "x":
        _S.append(s)
len_S=len(_S)
if  sum([_S[i]!=_S[-i-1] for i in range(len_S//2)]):
    print(-1)
else:
    count=0
    i,m=0,len(S)-1
    while m-i>0:
        if S[i]=="x" and S[m]!="x":
            i+=1
            count+=1
        elif S[i]!="x" and S[m]=="x":
            m-=1
            count+=1
        else:
            i+=1
            m-=1
    print(count)

Submission status: Accepted

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
    
    sp.append(cnt)
    
    ret = 0
    n = len(sp)
    for i in range(n // 2) :
        ret += abs(sp[i] - sp[n-1-i])
        
    print(ret)

Submission status: Accepted

Solution code:
N=list(input())

l=0
r=len(N)-1

c=0
while(True):
    if(r<=l):
        print(c)
        exit()
    
    if(N[l]==N[r]):
        l+=1
        r-=1
    elif(N[l]=="x" and N[r]!="x"):
        l+=1
        c+=1
    elif(N[l]!="x" and N[r]=="x"):
        r-=1
        c+=1
    else:
        print(-1)
        exit()

Submission status: Accepted

Solution code:
S=input()
ans=0
indf=0
indl=len(S)-1
while indf<indl:
  if S[indf]==S[indl]:
    indf+=1
    indl-=1
    continue
  if S[indf]=="x":
    indf+=1
    ans+=1
  elif S[indl]=="x":
    indl-=1
    ans+=1
  else:
    print(-1)
    break
else:
  print(ans)

Submission status: Accepted

Solution code:
S=input()
import collections
counter=collections.Counter(S)

# 後ろと前からxを無視して比較していく
# 同じ文字なら続行、indexで出逢えばOK

ans=0
indf=0
indl=len(S)-1
while indf<indl:
  if S[indf]==S[indl]:
    indf+=1
    indl-=1
    continue
  if S[indf]=="x":
    indf+=1
    ans+=1
  elif S[indl]=="x":
    indl-=1
    ans+=1
  else:
    print(-1)
    break
else:
  print(ans)

Submission status: Accepted

Solution code:
s = input()

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

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

print(answer)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#末端の処理を変更
s_ = "x"+input()+"x"
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: Accepted

Solution code:
s = input()
start = 0
end = len(s) -1
insertions = 0
while(start < end):
    if s[start] == s[end]:
        start += 1
        end -= 1
        continue

    if 'x' == s[end]:
        insertions += 1
        end -= 1
    elif 'x' == s[start]:
        insertions += 1
        start += 1
    else:
        print(-1)
        exit(0)

print(insertions)

Submission status: Accepted

Solution code:
w=list(input())
b=True
n=len(w)
left=0
right=n-1
count=0
while left<=right:
  if w[left]==w[right]:
    left+=1
    right-=1
  elif w[left]=="x":
    left+=1
    count+=1
  elif w[right]=="x":
    right-=1
    count+=1
  else:
    b=False
    break
if b:
  print(count)
else:
  print(-1)

Submission status: Accepted

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 = collections.deque(s)
    
    count = 0
    while len(s) > 1:
        if s[0] == 'x':
            if s[-1] == 'x':
                s.popleft(); s.pop()
            else:
                s.popleft()
                count += 1
        else:
            if s[-1] != 'x':
                s.popleft(); s.pop()
            else:
                s.pop()
                count += 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from heapq import heappop, heappush
#input = sys.stdin.readline

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])
def graphm():
    n=inp()
    g=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b,w=inpm()
        a-=1
        b-=1
        g[a].append((b,w))
        g[b].append((a,w))
    return n,g


def main():
    s=input()
    n=len(s)
    i_l = 0
    i_r = n-1
    cnt = 0
    flag = False
    while True:
        if i_l >= i_r:
            if i_l == i_r:
                flag = True
            if flag:
                print(cnt)
                return
            else:
                print(-1)
                return
        if s[i_l] == s[i_r]:
            i_l += 1
            i_r -= 1
            flag = True
        elif s[i_l] == 'x' and s[i_r] != 'x':
            cnt += 1
            i_l +=1
            flag = False
        elif s[i_l] != 'x' and s[i_r] == 'x':
            cnt += 1
            i_r -=1
            flag = False
        elif s[i_l] != 'x' and s[i_r] != 'x' and s[i_l] != s[i_r]:
            print(-1)
            return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = input()
    Q = deque(S)
    cnt = 0
    while Q:
        if len(Q) == 1:
            break
        front = Q.popleft()
        back = Q.pop()
        if front == back:
            continue
        if front == 'x':
            cnt += 1
            Q.append(back)
            continue
        if back == 'x':
            cnt += 1
            Q.appendleft(front)
            continue
        print(-1)
        exit()

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()

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

Submission status: Accepted

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

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

Submission status: Accepted

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

    left = 0
    right = n - 1

    ret = 0
    while left < right:
        if s[left] == s[right]:
            left += 1
            right -= 1
        elif s[left] == 'x':
            ret += 1
            left += 1
        elif s[right] == 'x':
            ret += 1
            right -= 1
        else:
            return -1
    return ret


print(solve())

Submission status: Accepted

Solution code:
s = str(input())

r = 0
l = len(s)-1
result = 0
h = True
while r < l:
    if s[r] == s[l]:
        r += 1
        l -= 1
    elif s[l] == 'x':
        result += 1
        l -= 1
    elif s[r] == 'x':
        result += 1
        r += 1
    else:
        h = False
        break

if h:
    print(result)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

s = input()

rem = []
remrev = []

for i in s:
    if i != "x":
        rem.append(i)
        remrev.append(i)

remrev.reverse()

for i in range(len(rem)):

    if rem[i] != remrev[i]:
        print (-1)
        sys.exit()

nx = 0
xnum = []

for i in s:

    if i == "x":
        nx += 1
    else:
        xnum.append(nx)
        nx = 0

xnum.append(nx)

xnc = xnum.copy()
xnc.reverse()

ans = 0

for i in range((len(xnum) + 1) // 2):

    ans += abs(xnum[i]-xnc[i])

print (ans)

Submission status: Accepted

Solution code:
S = list(input())
i, j = 0, 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:
        print('-1')
        break

else:
    print(ans)

Submission status: Accepted

Solution code:
S = input()


l = 0
r = len(S)-1
cnt = 0
while l < r:
    #print(l,r)
    if S[l] == S[r]:
        l += 1
        r -= 1
    else:
        if S[l] == "x":
            #末尾にxを追加し削除
            l += 1
            cnt += 1
        elif S[r] == "x":
            r -= 1
            cnt += 1
        else:
            print(-1)
            exit()


print(cnt)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
s=input()[:-1]
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: Accepted

Solution code:
s = input()
lens = len(s)
l = 0
r = lens - 1
cnt = 0
while l != r:
  if s[l] == s[r]:
    l += 1
    r -= 1
    if l > r:
      break
  elif s[l] == 'x':
    cnt += 1
    l += 1
  elif s[r] == 'x':
    cnt += 1
    r -= 1
  else:
    print(-1)
    exit()
print(cnt)