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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    sa = s[::1]
    sa = sa.replace("x", "")
    for i in range(len(sa)):
        if sa[i] == sa[-i - 1]:
            continue
        print(-1)
        return
    ans = 0
    i = 0
    j = len(s) - 1
    while i < j:
        if s[i] == s[j]:
            i += 1
            j -= 1
            continue
        ans += 1
        if s[i] == "x":
            i += 1
        if s[j] == "x":
            j -= 1
    print(ans)



    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
s=input()
n=len(s)
arr1=[]
arr2=[]
for i in range(n):
  if s[i]!='x':
    arr1.append(s[i])
    arr2.append(i)
if len(arr1)==0:
  print(0)
  exit()
for i in range(len(arr1)):
  if arr1[i]!=arr1[len(arr1)-i-1]:
    print(-1)
    exit()
arr2=[0]+arr2+[n-1]
arr3=[]
for i in range(len(arr2)-1):
  arr3.append(arr2[i+1]-arr2[i])
ans=0
for i in range(len(arr3)//2):
  ans+=max(arr3[i],arr3[len(arr3)-i-1])-min(arr3[i],arr3[len(arr3)-i-1])
print(ans)
#print(arr3)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    s = S()
    d = deque(s)
    ans = 0

    # print(d)

    while len(d) > 1:
        if d[0] == d[-1]:
            d.popleft()
            d.pop()
        elif d[0] == 'x':
            d.append('x')
            ans += 1
        elif d[-1] == 'x':
            d.appendleft('x')
            ans += 1
        else:
            print(-1)
            return

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
# C


S = input()
N = len(S)
# 回分判定
ss = ""
for s in S:
    if s == 'x':
        pass
    else:
        ss += s
if ss != ss[::-1]:
    print(-1)
    exit()
if ss == "":
    print(0)
    exit()
# 真ん中を決める
# x の位置と個数を求める
n = len(ss)
if n % 2 == 0:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:
                cnt = j+1
                break
        if i == n//2-1:
            left_center = cnt-1
    right_center = cnt-1
    # 真ん中の分を先に計算する
    ans = (abs(right_center - left_center) % 2)
    # centerから両側に展開していく
    count = 0
    x_left = []
    x_right = []
    for i in range(left_center):
        if S[left_center-1 - i] == 'x':
            count += 1
        else:
            x_left.append(count)
            count = 0
    x_left.append(count)
    count = 0
    for i in range(right_center+1, N, 1):
        if S[i] == 'x':
            count += 1
        else:
            x_right.append(count)
            count = 0
    x_right.append(count)
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i] - x_right[i])
    print(ans)
else:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:       # S[j] != "x" なら ss[i] と S[j] は一致する
                cnt = j + 1
                break
    center = cnt-1
    # print(center)
    # center から両側に展開していき，center からの位置と x の個数を求める
    distans = 0     # x 以外の文字が出るたびに更新する
    count = 0
    x_left = []
    x_right = []
    for i in range(center):
        if S[center-1 - i] == "x":
            count += 1
        else:
            x_left.append([distans, count])
            distans += 1
            count = 0
    x_left.append([distans, count])
    distans = 0
    count = 0
    for i in range(center+1, N):
        if S[i] == "x":
            count += 1
        else:
            x_right.append([distans, count])
            distans += 1
            count = 0
    x_right.append([distans, count])
    # print(x_left)
    # print(x_right)
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i][1] - x_right[i][1])
    print(ans)

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:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
s = input()

l = 0
r = len(s) - 1

ans = 0
while r > l:
    if s[l] == s[r] == "x":
        l += 1
        r -= 1
    elif s[l] == "x":
        l += 1
        ans += 1
    elif 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()

ss = ""
cnt = 0
for e in s:
    if e == "x":
        cnt += 1
    else:
        ss += e

if ss == ss[::-1]:
    l = 0
    r = len(s) - 1
    ans = 0
    while r > l:
        if s[l] == s[r] == "x":
            l += 1
            r -= 1
        elif s[l] == "x":
            l += 1
            ans += 1
        elif s[r] == "x":
            r -= 1
            ans += 1
        else: # s[l] == s[r]
            l += 1
            r -= 1

else:
    ans = -1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections

s = input()
a = collections.deque(s)
ans = 0
while 1 < len(a):
	if a[0] == a[len(a) - 1]:
		a.popleft()
		a.pop()
	elif a[0] == 'x':
		a.append('x')
		ans += 1
	elif a[len(a) - 1] == 'x':
		a.appendleft('x')
		ans += 1
	else:
		ans = -1
		break
print(ans)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    string = deque(input())
    answer = 0
    while string:
        if string[0] == string[-1]:
            string.pop()
            if string:
                string.popleft()
            answer += 0
        else:
            if string[0] == "x":
                string.append("x")
                answer += 1
            elif string[-1] == "x":
                string.appendleft("x")
                answer += 1
            else:
                answer = -1
                break
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    l = 0
    r = len(s) - 1

    ret = 0
    while l < r:
        lc, rc = s[l], s[r]

        if lc == rc:
            l += 1
            r -= 1
            continue

        if lc == 'x':
            ret += 1
            l += 1
            continue

        if rc == 'x':
            ret += 1
            r -= 1
            continue

        print(-1)
        return

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
li=list(s)

i=0
j=len(s)-1
answer=0

while i<j:
    if li[i]==li[j]:
        i+=1
        j-=1
    else:
        if li[i]!="x" and li[j]!="x":
            answer=-1
            break
        elif li[i]=="x" and li[j]!="x":
            answer+=1
            i+=1
        elif li[i]!="x" and li[j]=="x":
            answer+=1
            j-=1

print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


s = S()
no_x = ''.join(s.split('x'))
if no_x[::-1] != no_x:
    print(-1)
else:
    L = []
    cnt = 0
    if not no_x:
        print(0)
        exit()
    for i in s:
        if i == 'x':
            cnt += 1
        else:
            L += [cnt]
            cnt = 0
    L += [cnt]
    ans = 0
    for i in range(len(no_x) + 1):
        ans += abs(L[i] - L[len(no_x) - i])
    print(ans // 2)

Submission status: Accepted

Solution code:
s = input()
S = []
xid = [0] * (10 ** 5 + 1)
xcnt = 0
for c in s:
    if c != "x":
        S.append(c)
        xcnt += 1
    else:
        xid[xcnt] += 1
xid = xid[:xcnt + 1]
ans = 0
if S[0:xcnt // 2] == S[-1:-(xcnt // 2) - 1:-1]:
    for i in range(-(-xcnt // 2)):
        ad = xid[i] - xid[xcnt - i]
        if ad < 0:
            ad = -ad
        ans += ad
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()
sr = s[-1::-1]
left = 0
right = 0
ans = 0
while (1):
    if left + right >= len(s) - 1:
        break
    if s[left] != sr[right]:
        if s[left] == 'x':
            left += 1
        elif sr[right] == 'x':
            right += 1
        else:
            print(-1)
            exit()
        ans += 1
    elif s[left] == sr[right]:
        left += 1
        right += 1

print(ans)

Submission status: Accepted

Solution code:
S = input()
head, tail, ans = 0, len(S)-1, 0

while head < tail:
    if S[head] == S[tail]:
        head += 1
        tail -= 1
    elif S[head] == 'x':
        head += 1
        ans += 1
    elif S[tail] == 'x':
        tail -= 1
        ans += 1
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
s1=s[::-1]
ans = 0
cnt = 0
cnt1 = 0
while cnt<len(s) and cnt1<len(s) and cnt<=len(s)-1-cnt1:
  if s[cnt]!=s1[cnt1]:
    if s[cnt]=="x" and s1[cnt1]!="x":
      cnt+=1
      ans+=1
    elif s1[cnt1]=="x" and s[cnt]!="x":
      cnt1+=1
      ans+=1
    elif s[cnt]!="x" and s1[cnt1]!="x":
      print(-1)
      exit()
  elif s[cnt]==s1[cnt1]:
    cnt+=1
    cnt1+=1
print(ans)

Submission status: Accepted

Solution code:
import collections

s = input()
a = collections.deque(s)
ans = 0
while 1 < len(a):
	if a[0] != a[len(a) - 1]:
		ans += 1
		if a[0] != 'x':
			a.appendleft('x')
			if a[len(a) - 1] != 'x':
				ans = -1
				break
		else:
			a.append('x')
	else:
		a.popleft()
		a.pop()
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
ans = 0

fst = 0
lst = len(S)-1
flg = 1

while(1):
    if fst>=lst:
        break

    if S[fst]==S[lst]:
        fst += 1
        lst -= 1
        continue
    
    elif S[fst]=="x":
        ans += 1
        fst += 1
    
    elif S[lst]=="x":
        ans += 1
        lst -= 1
    
    else:
        flg = 0
        break

if flg:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
def main():
    s = input().rstrip()
    ans = 0
    f = True
    l, r = 0, len(s)-1
    while l < r:
        if s[l] != s[r]:
            if s[l] != "x" and s[r] != "x":
                f = False
                break
            elif s[l] == "x":
                ans += 1
                l += 1
            else:
                ans += 1
                r -= 1
        else:
            l += 1
            r -= 1
    if f:
        print(ans)
    else:
        print("-1")

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
S = list(S)
S_no_x = []
def is_kaibun(kaibun):
    for i in range(len(kaibun)//2):
        if kaibun[i] != kaibun[-i-1]:
            return False
    return True
inds = []
for i,c in enumerate(S):
    if c != "x":
        S_no_x.append(c)
        inds.append(i)
if not is_kaibun(S_no_x):
    print(-1)
else:
    l = len(S_no_x)

    if l == 0:
        print(0)
    else:
        mae = ""
        usiro = ""
        if l % 2 == 0:
            mae_ind = inds[l // 2 - 1]
            usiro_ind = inds[l // 2]
            mae = S[:mae_ind +1]
            usiro = S[usiro_ind :]
        else:
            center = inds[l // 2]
            mae = S[:center]
            usiro = S[center +1:]

        usiro.reverse()
        usiro_x = []
        mae_x = []
        cot = 0
        usiro.append("@")
        for c in usiro:
            if c == "x":
                cot += 1
            else:
                usiro_x.append(cot)
                cot = 0
        cot = 0
        mae.append("@")
        for c in mae:
            if c == "x":
                cot += 1
            else:
                mae_x.append(cot)
                cot = 0
        ans = 0
        for a,b in zip(mae_x,usiro_x):
            ans += abs(a-b)
        print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    from collections import deque
    S = deque(S)
    ans = 0
    while S:
        if S[0] == S[-1]:
            S.popleft()
            if S:
                S.pop()
        elif S[0] != "x" and S[-1] != "x":
            return print(-1)
        elif S[0] == "x":
            S.append("x")
            ans += 1
        else:
            S.appendleft("x")
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s=input()
    l=len(s)
    i,j=0,l-1
    ans,cnti,cntj=0,0,0
    while i+1<j:
        if s[i]=='x':
            i+=1
            cnti+=1
        elif s[j]=='x':
            j-=1
            cntj+=1
        else:
            if s[i]==s[j]:
                ans+=abs(cnti-cntj)
                cnti,cntj=0,0
                i+=1
                j-=1
            else:
                print(-1)
                exit()
    if i==j:
        print(ans)
    else:
        if s[i]==s[j]:
            print(ans)
        else:
            if s[i]=='x':
                print(ans+abs(cnti+1-cntj))  
            elif s[j]=='x':
                print(ans+abs(cnti-cntj-1))
            else:
                print(-1)          
    
if __name__=='__main__':
    main()

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()
left=0
right=len(s)-1
cnt=0
while right-left>0:
  if (s[right]==s[left]):
    right-=1
    left+=1
  elif (s[right]!='x')and(s[left]!='x'):
    print(-1)
    exit()
  elif (s[right]!='x')and(s[left]=='x'):
    left+=1
    cnt+=1
  else:
    right-=1
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
s=input()

left=0
right=len(s)-1

ans=0
while left<right:
  c1=s[left]
  c2=s[right]
  if c1==c2:
    left+=1
    right-=1
    continue

  elif c1!='x' and c2!='x':
    print(-1)
    exit()
  elif c1=='x':
    ans+=1
    left+=1
  elif c2=='x':
    ans+=1
    right-=1

print(ans)

Submission status: Accepted

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

def main():
    s = input()
    l,r = 0,len(s)-1
    count = 0
    while l < r:
        if s[l] == s[r]:
            l += 1
            r -= 1
        else:
            if s[l] == "x":
                l += 1
            elif s[r] == "x":
                r -= 1
            else:
                print("-1")
                exit()
            count += 1
            
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
l, r=0, len(s)-1
ret = 0
while l<r:
  L, R=s[l], s[r]
  if L==R:
    l+=1
    r-=1
  elif L=='x':
    l+=1
    ret+=1
  elif R=='x':
    r-=1
    ret+=1
  else:
    ret=-1
    break
print(ret)

Submission status: Accepted

Solution code:
S = list(input())

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
S=input()
T=S.replace("x","")
if all(s==t for s,t in zip(T,T[::-1])):
    ans=0
    i=0
    j=len(S)-1
    while i<j:
        pi=i
        pj=j
        while i<len(S) and S[i]=="x":
            i+=1
        while j>=0 and S[j]=="x":
            j-=1
        if i>j:
            break
        ans+=abs(i-pi-pj+j)
        i+=1
        j-=1
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()
l,r = 0, len(s)-1
res = 0
if s==s[::-1]:
    print(res)
    exit()

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

Submission status: Accepted

Solution code:
s = input()
count = 0
if len(set(s)) == 1:
    print(0)
    exit()
left = 0
right = len(s) - 1

ans = 0
while True:
    if (s[left] == 'x' and s[right] == 'x') or s[left] == s[right]:
        left += 1
        right -= 1
    elif s[left] == 'x' and s[right] != 'x':
        ans += 1
        left += 1
    elif s[left] != 'x' and s[right] == 'x':
        ans += 1
        right -= 1
    elif s[left] != 'x' and s[right] != 'x' and s[left] != s[right]:
        print(-1)
        exit()
    if right <= left:
        break
print(ans)

Submission status: Accepted

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

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

for i in range(len(no_x)//2):
  if no_x[i] != no_x[-i-1]:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  i_f=0
  i_b=len(s)-1

  ans=0
  while True:
    if i_f>=i_b:
      return ans

    # print(i_f,i_b)
    if s[i_f]==s[i_b]:
      i_f+=1
      i_b-=1
    else:
      if s[i_f]=='x':
        i_f+=1
        ans+=1
      elif s[i_b]=='x':
        i_b-=1
        ans+=1
      else:
        return -1

# main()
print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main() -> None:
    S = input()
    left, right = 0, len(S) - 1
    ans = 0
    while left < right:
        if S[left] == S[right]:
            left, right = left + 1, right - 1
        elif S[left] == 'x':
            ans += 1
            left += 1
        elif S[right] == 'x':
            ans += 1
            right -= 1
        else:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
t = s.replace("x","")
if not t:
  print(0)
  exit()
l = len(t)
if t != t[::-1]:
  print(-1)
  exit()
plc = [-1]
for i in range(n):
  if s[i] != "x":
    plc.append(i)
plc.append(n)
l = len(plc)
if l%2 == 0:
  fm = plc[:l//2]
  lt = plc[l//2:]
else:
  fm = plc[:l//2+1]
  lt = plc[l//2:]
lt = lt[::-1]
ans = 0
for i in range(1,len(fm)):
  ans += abs((fm[i]-fm[i-1])-(lt[i-1]-lt[i]))
print(ans)

Submission status: Accepted

Solution code:
def main() -> None:
    S = input()
    N = len(S)
    pos = []
    ss = []
    for i, s in enumerate(S):
        if s != 'x':
            ss.append(s)
            pos.append(i)
    if ss != ss[::-1]:
        print(-1)
        return

    L = len(ss)
    if len(ss) == 0:
        print(0)
        return
    if L % 2 == 0:
        left, right = (L-1) // 2, L // 2
    else:
        left = right = L // 2
    left, right = left - 1, right + 1
    ans = 0
    while left >= 0 and right < L:
        ans += abs((pos[left + 1] - pos[left] - 1) -
                   (pos[right] - pos[right-1] - 1))
        left, right = left - 1, right + 1
    ans += abs((pos[0] - 0) - (N - 1 - pos[-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
t = s.replace('x', '')
if all(t[i] == t[len(t) - i - 1] for i in range(len(t) // 2)):
    l = 0
    r = len(s) - 1
    count = 0
    while r - l > 0:
        if s[l] != s[r]:
            count += 1
            if s[l] == 'x':
                l += 1
            if s[r] == 'x':
                r -= 1
        else:
            l += 1
            r -= 1
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()
def isp(str): return 1 if str == str[::-1] else 0

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

if not isp(nox_s):
    print("-1")
    exit()

x_lis=[]
cnt=0
for c in s:
    if c=="x":
        cnt+=1
    else:
        x_lis.append(cnt)
        cnt=0
x_lis.append(cnt)
ans=0

for i in range(len(x_lis)):
    if x_lis[i] != x_lis[-i-1]:
        ans+= abs(x_lis[i] - x_lis[-i-1])

print(int(ans/2))

Submission status: Accepted

Solution code:
s=input()
s_exit_x=''
ans = 0
for n,i in enumerate(s):
    if i !='x':
        s_exit_x += i
if s_exit_x != s_exit_x[::-1]:
    print(-1)
else:
    left=0
    right=len(s)-1
    while left < right:
        if s[left] == s[right]:
            left += 1
            right -=1
        elif s[left]=="x":
            left += 1
            ans += 1
        else:
            right -=1
            ans += 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

d = S

left = 0
right = len(d) - 1

count = 0
while left < right:
    #print(f'{left=}, {right=}')
    if d[right] == d[left]:
        left += 1
        right -= 1
    else:
        if d[right] == 'x':
            count += 1
            right -= 1
        elif d[left] == 'x':
            count += 1
            left += 1
        else:
            count = -1
            print(-1)
            break

if count != -1:
    print(count)

Submission status: Accepted

Solution code:
from collections import deque

S = input()

d = deque(S)


left = 0
right = len(d) - 1

count = 0
while left < right:
    #print(f'{left=}, {right=}')
    if d[right] == d[left]:
        left += 1
        right -= 1
    else:
        if d[right] == 'x':
            count += 1
            right -= 1
        elif d[left] == 'x':
            count += 1
            left += 1
        else:
            count = -1
            print(-1)
            break

if count != -1:
    print(count)

Submission status: Accepted

Solution code:
S = input()

N = len(S)
tcnt =[0] * 128
for c in S:
    tcnt[ord(c)] += 1 if c != 'x' else 0
odd = 0
for i in range(128):
    if tcnt[i] % 2 == 1:
        if odd != 0:
            print(-1)
            exit()
        else:
            odd = i

cl = -1
cr = -1
cnt = [0] * 128
for i in range(N):
    cnt[ord(S[i])] += 1 if S[i] != 'x' else 0
    if cnt[ord(S[i])] > (tcnt[ord(S[i])] + 1) // 2:
        cr = i
        break
    elif S[i] != 'x':
        cl = i
if cl == -1:
    print(0)
    exit()

if odd != 0:
    cr = cl

ans = 0
while cl >= 0 and cr < N:
    l = cl - 1
    while l >= 0 and S[l] == 'x':
        l -= 1
    lx = cl - l - 1
    r = cr + 1
    while r < N and S[r] == 'x':
        r += 1
    rx = r - cr - 1
    ans += abs(lx - rx)

    a = ord(S[l]) if l >= 0 else 0
    b = ord(S[r]) if r < N else 0
    if a != b:
        print(-1)
        exit()
    else:
        cl = l
        cr = r

print(ans)

Submission status: Accepted

Solution code:
s = input()
flag = True
top = 0
tale = len(s)-1
ans = 0
while top < tale:
  if s[top] == 'x' and s[tale] == 'x':
    top += 1
    tale -= 1
  elif s[top] == 'x' and s[tale] != 'x':
    top += 1
  elif s[top] != 'x' and s[tale] == 'x':
    tale -= 1
  else:
    if s[top] != s[tale]:
      flag = False
      break
    else:
      top += 1
      tale -= 1
if not flag:
  print(-1)
else:
  top = 0
  tale = len(s)-1
  while top < tale:
    if s[top] == s[tale]:
      top += 1
      tale -= 1
    elif s[top] == 'x' and s[tale] != 'x':
      ans += 1
      top += 1
    elif s[top] != 'x' and s[tale] == 'x':
      ans += 1
      tale -= 1
  print(ans)

Submission status: Accepted

Solution code:
s = str(input())
n = len(s)
ans = 0
j = n-1
for i in range(n):
    if i>=j:
        break

    if s[i]=='x' and s[j]=='x':
        j-=1
        continue
    elif s[i]=='x' and s[j]!='x':
        ans +=1
        continue
    while i<j:
        if s[j]=='x':
            j-=1
            ans +=1
        elif s[i]==s[j]:
            j-=1
            break
        else:
            print(-1)
            exit()


print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
L = list(S)
left_idx = 0
right_idx = N - 1
ret = 0
while left_idx < right_idx:
    l = S[left_idx]
    r = S[right_idx]
    if l == r:
        left_idx += 1
        right_idx -= 1
    elif l == "x":
        ret += 1
        left_idx += 1
    elif r == "x":
        ret += 1
        right_idx -= 1
    else:
        ret = -1
        break

print(ret)

Submission status: Accepted

Solution code:
from sys import stdin

s = stdin.readline().rstrip()

r = len(s)-1
l = 0
count = 0
while r-l > 0:
    if s[l] == s[r]:
        r -= 1
        l += 1
    elif s[l] == "x" and s[r] != "x":
        count += 1
        l += 1
    elif s[r] == "x" and s[l] != "x":
        count += 1
        r -= 1
    else:
        print(-1)
        exit()
print(count)

Submission status: Accepted

Solution code:
from sys import stdin

s = stdin.readline().rstrip()
k = ""
for i in range(len(s)):
    if s[i] != "x":
        k += s[i]

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

man = (len(k)+1)//2
point = 0
for i,j in enumerate(s):
    if j != "x":
        point += 1
    if point == man:
        kotae = i
        break

a = s[:kotae+1]
b = s[kotae:][::-1]

A = len(a)
B = len(b)

point_a = 0
point_b = 0

total = 0

while point_a < A and point_b < B:
    if a[point_a] == b[point_b]:
        point_a += 1
        point_b += 1
    elif a[point_a] == "x" and b[point_b] != "x":
        point_a += 1
        total += 1
    else:
        point_b += 1
        total += 1

print(total)

Submission status: Accepted

Solution code:
s = input()
se = s.replace('x','')
if se != se[::-1]:
    print(-1)
else:
    xc = []
    now = 0
    for i in s:
        if i == 'x':
            now += 1
        else:
            xc.append(now)
            now = 0
    xc.append(now)
    ans = 0
    for i in range(len(xc)//2):
        ans += abs(xc[i]-xc[-i-1])
    print(ans)