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:
s = input()
ss = s.replace("x", "")
ssr = ss[::-1]
if  ss != ssr:
    print(-1)
else:
    l = 0
    r = len(s) - 1
    cnt = 0
    while l < r:
        if s[l] == s[r]:
            r -= 1
            l += 1
        elif s[l] == "x":
            cnt += 1
            l += 1
        elif s[r] == "x":
            cnt += 1
            r -= 1
    print(cnt)

Submission status: Accepted

Solution code:
s = input()
"""
xabxa
"""
l = 0
r = len(s)-1
cnt = 0
while r - l >= 1:
    if s[r] == s[l]:
        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:
s = input()
n = len(s)

sr = s.replace("x", "")

if sr != "".join(reversed(sr)):
    print(-1)
else:
    ans = 0
    l = 0
    r = -1
    while l - r < n:
        if s[l] == s[r]:
            l += 1
            r -= 1
            continue
        elif s[l] == "x" and s[r] != "x":
            ans += 1
            l += 1
        elif s[l] != "x" and s[r] == "x":
            ans += 1
            r -= 1
        
    print(ans)

Submission status: Accepted

Solution code:
s = input()

ans = 0

l, r = 0, len(s)-1
if len(s) != 1:
    while l < r:
        # print("l", l, "r", 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:
from collections import deque

def main():
    s = input()
    t = s.replace('x', '')
    if t != ''.join(list(reversed(t))):
        print(-1)
        return
    if len(s) == 1:
        print(0)
        return
    
    que = deque(s)
    gnr = []
    l = que.popleft()
    r = que.pop()
    tmp = '0'
    cnt = 0
    for _ in range(len(s)):
        if l == r:
            gnr.append(l)
            if len(que)>=2:
                l = que.popleft()
                r = que.pop()
            elif len(que) == 1:
                tmp = que.pop()
            else:
                break
        else:
            if l == 'x':
                cnt += 1
                gnr.append(l)
                if len(que) == 0:
                    tmp = r
                    break
                l = que.popleft()
            elif r == 'x':
                cnt += 1
                gnr.append(r)
                if len(que) == 0:
                    tmp = l
                    break
                r = que.pop()
            else:
                print(-1)
                return
    rgnr = gnr
    rgnr.reverse()
    if tmp == '0':
        ans = ''.join(gnr) + ''.join(rgnr)
    else:
        ans = ''.join(gnr) + tmp + ''.join(rgnr)
    #print(ans)
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = input()
ss = s.replace('x', '')
ans = 0
i, j = 0, len(s)-1

if ss == ss[::-1]:
    while i<j:
        if s[i] != s[j]:
            ans += 1
            if s[i]=='x': i += 1
            else: j -= 1
        else:
            i += 1
            j -= 1
    print(ans)
else: print(-1)

Submission status: Accepted

Solution code:
s = input()
t = "".join([m for m in s if m != "x"])
u = t[::-1]
#print(t,u)
if t != u:
    print(-1)
    exit()
else:
    i = 0
    j = len(s)-1
    ans = 0
    while i <= j:
        if s[i] != s[j]:
            if s[i] == "x":
                ans += 1
                i += 1
            else:
                ans += 1
                j -= 1
        else:
            i += 1
            j -= 1
    print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

s = list(input())
ans = 0
left = 0
right = len(s)-1

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

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':
    ans+=1
    i+=1
  elif s[j]=='x':
    ans+=1
    j-=1
  else:
    print(-1)
    exit()
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

s = deque(s)

ans = 0
while len(s) > 0:
    if s[0] == s[-1]:
        if len(s) == 1:
            s.popleft()
        else:
            s.popleft()
            s.pop()

    elif s[0] == "x":
        s.popleft()
        ans += 1
    elif s[-1] == "x":
        s.pop()
        ans += 1
    else:
        print(-1)
        exit(0)

print(ans)

Submission status: Accepted

Solution code:
import re
s=input()
c=''
for i in s:
    if i!='x':
        c+=i
if c!=c[::-1]:
    print(-1)
    exit()
n=len(s)
l,r=0,n-1
ans=0
while(r-l>0):
    if s[l]!=s[r]:
        if s[l]=='x':
            ans+=1
            l+=1
        elif s[r]=='x':
            ans+=1
            r-=1
    else:
        l+=1
        r-=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
l = 0
r = n - 1
ng = 0
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:
        ng = 1
        break
if ng:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())

    characters = []
    cnt = []
    counter = 0
    for si in s:
        if si != "x":
            characters.append(si)
            cnt.append(counter)
            counter = 0
        else:
            counter += 1
    cnt.append(counter)

    flg = True
    odd = len(characters) % 2
    for i in range(len(characters) // 2):
        if characters[i] != characters[-i - 1]:
            flg = False

    if not flg:
        print(-1)
        exit(0)

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


if __name__ == '__main__':
    main()

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
        continue
    if 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 = list(input())
N = len(S)

cnt = 0
l = 0
r = N-1
while l<=r:
    if S[l]=="x" and S[r]!="x":
        l += 1
        cnt += 1
    elif S[l]!="x" and S[r]=="x":
        r += -1
        cnt += 1
    elif S[l]=="x" and S[r]=="x":
        l += 1
        r += -1
    elif S[l]!="x" and S[r]!="x":
        if S[l]==S[r]:
            l += 1
            r += -1
        else:
            cnt = -1
            break
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
s_ = s.replace('x', '')
if s_ != s_[::-1]:
    print(-1)
    exit()
else:
    cnt = 0
    num = 0
    for i, a in enumerate(s):
        if i + cnt >= len(s)-1:
            break
        if a == s[-1-cnt]:
            cnt += 1
            continue
        if a == 'x':
            num += 1
            continue
        else:
            while s[-1-cnt] == 'x':
                cnt += 1
                num += 1
            if a == s[-1 - cnt]:
                cnt += 1
                continue

    print(num)

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()

    if S == S[::-1]:
        print(0)
        return

    S2 = [s for s in S if s != 'x']

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

    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    S = deque(input())
    N = len(S)
    ans = 0
    if len(S) == 1:
        print(0)
        return
    left = S.popleft()
    right = S.pop()
    while True:
        if left == right:
            if len(S) >= 2:
                left = S.popleft()
                right = S.pop()
                continue
            else:
                break
            
        if left != 'x' and right!='x':
            print(-1)
            return

        ans += 1
        if left == 'x':
            if len(S)>0:
                left = S.popleft()
            else:
                break
        elif right == 'x':
            if len(S)>0:
                right = S.pop()
            else:
                break

    print(ans)



            

        

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

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

print(ans)

Submission status: Accepted

Solution code:
s = input()

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

print(c)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
from collections import deque

inf = 10 ** 15
mod = 10 ** 9 + 7

s = input()
n = len(s)
ss = []
for c in s:
    if c != 'x':
        ss.append(c)
leng = len(ss)
flag = True
for i in range(leng//2):
    if ss[i] != ss[leng-1-i]:
        flag = False
        break
if flag:
    count=0
    l = 0
    r = n-1
    while l < r:
        if s[r] != s[l]:
            if s[l] == 'x':
                count+=1
                l+=1
            else:
                count+=1
                r-=1
        else:
            r -= 1
            l += 1
    print(count)


else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,sin,cos
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

s=list(ss())

cntx=0

chk=True
i=0
j=len(s)-1



while chk:
    if s[i]==s[j]:
        i+=1
        j-=1
    
    elif s[i]!=s[j] and s[i]!="x" and s[j]!="x":
        print(-1)
        exit()
    
    elif s[i]!=s[j] and s[i]=="x" and s[j]!="x":
        cntx+=1
        i+=1
    
    elif s[i]!=s[j] and s[i]!="x" and s[j]=="x":
        cntx+=1
        j-=1
    
    #print(i,s[i],j,s[j])
    
    if i>=j:
        print(cntx)
        exit()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    s = IS()
    n = len(s)
    t = s.replace('x', '')
    if t != t[::-1]:
        print(-1)
        return None
    l = 0
    r = n-1
    cnt = 0
    while l < r:
        if s[l] != s[r]:
            if s[l] == 'x':
                l += 1
            elif s[r] == 'x':
                r -= 1
            cnt += 1
        else:
            l += 1
            r -= 1
    print(cnt)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
N = len(S)
l = 0
r = N-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()
N = len(S)
ans = []
check = []
for i in range(N):
    if S[i] != 'x':
        ans.append(S[i])
        check.append(i)
if len(ans) == 0:
    print(0)
    exit()
if ans != ans[::-1]:
    print(-1)
    exit()
if len(ans)%2:
    cl,cr = check[len(ans)//2],check[len(ans)//2]
else:
    cl,cr = check[len(ans)//2-1],check[len(ans)//2]
cnt = 0
while 0 <= cl and cr <= N-1:
    if S[cl] == S[cr]:
        cl -= 1
        cr += 1
    elif S[cl] == 'x':
        cnt += 1
        cl -= 1
    else:
        cnt += 1
        cr += 1
cnt += max(cl+1,N-cr)
print(cnt)

Submission status: Accepted

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

flag = 1
for i in range(len(t)):
    if t[i] != t[~i]:
        flag = 0

if flag == 0:
    print(-1)
else:
    ans = 0
    t = s[::-1]
    c = 0
    for i in range(len(s)):
        if c >= len(t):
            if s[i] == "x":
                ans += 1
            continue
        if t[c] == s[i]:
            c += 1
            continue
        else:
            if s[i] == "x":
                ans += 1
                continue
            else:
                while t[c] == "x":
                    ans += 1
                    c += 1
                else:
                    c += 1
    ans += len(t) - c
    print(ans//2)

Submission status: Accepted

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

S = input()
N = len(S)
count = 0
is_ok = True

right_index = 0
left_index = N - 1

while right_index < left_index:

    if right_index == left_index:
        break

    if S[right_index] == S[left_index]:
        right_index += 1
        left_index -= 1
        continue
    if S[right_index] == "x":
        count += 1
        right_index += 1
        continue

    if S[left_index] == "x":
        count += 1
        left_index -= 1
        continue

    count = -1
    break

ans = count

print(ans)

Submission status: Accepted

Solution code:
S = list(input())

notX = []
for s in S:
    if not s == 'x':
        notX.append(s)

if not notX[::-1] == notX:
    print(-1)
    exit()

i = 0
Xcnt = []
cnt = 0
N = len(notX)
for s in S:
    if i < N:
        if s == notX[i]:
            i += 1
            Xcnt.append(cnt)
            cnt = 0
        else:
            cnt += 1

    else:
        cnt += 1

Xcnt.append(cnt)
before = sum(Xcnt)
Xcnt2 = []
N = len(Xcnt)
for i in range(N):
    Xcnt2.append(max(Xcnt[i],Xcnt[N-1-i]))

after = sum(Xcnt2)

print(after-before)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

s = ns()
n = len(s)

ans = 0
l, r = 0, n - 1
while r - l >= 1:
    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:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    new_s=""
    for ss in s:
        if ss!='x':
            new_s+=ss
    if new_s[::-1]!=new_s:
        print(-1)
        sys.exit()
    right=len(s)-1
    left=0
    ans=0
    while right-left>0:
        if s[right]!=s[left]:
            if s[right]=='x':
                ans+=1
                right-=1
            elif s[left]=='x':
                ans+=1
                left+=1
        else:
            right-=1
            left+=1
    print(ans)



if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
def codefes17qc_c():
    s = str(input())

    # x除去して回文であることが成立条件
    nox = s.replace('x', '')  # x除去
    noxrev = nox[::-1]  # x除去の反転
    if nox != noxrev:
        print(-1)
        return

    # 順sと逆sを見ていき、合わないところはxが入るところ
    srev = s[::-1]  # s反転
    ins = 0  # x挿入した回数の管理
    i, j = 0, 0
    while i < len(s) and j < len(s):
        if s[i] == srev[j]:
            i += 1
            j += 1
        else:
            if s[i] != 'x' and srev[j] == 'x':
                ins += 1
                j += 1
            if s[i] == 'x' and srev[j] != 'x':
                ins += 1
                i += 1

    ins += len(s) - min(i, j)  # 端っこのxのぶんを足す
    ans = ins // 2  # ぜんぶなめたので答えはその半分
    print(ans)

codefes17qc_c()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = deque(input())

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

Submission status: Accepted

Solution code:
s=list(input())

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

if len(ch)==0:
    print(0)
    exit()
for i in range(len(ch)//2):
    if ch[i]!=ch[len(ch)-i-1]:
        print(-1)
        exit()

ans=0
li=0
ri=0
for i in range(len(ch)//2+1):
    x=0
    while s[li]!=ch[i]:
        x += 1
        li += 1
    li += 1
    y=0
    while s[len(s)-1-ri]!=ch[len(ch)-1-i]:
        y += 1
        ri += 1
    ri += 1
    ans += abs(x-y)

print(ans)

Submission status: Accepted

Solution code:
def solve(s):
    l, r = 0, len(s) - 1

    ans = 0
    while l < r:
        if s[l] != s[r] and s[l] != 'x' and s[r] != 'x':
            return -1
        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
    return ans


if __name__ == '__main__':
    print(solve(input()))

Submission status: Accepted

Solution code:
s=input()

ll=0
rr=len(s)-1

cnt=0
while ll<=rr:
    if s[ll]==s[rr]:
        ll+=1
        rr-=1
    elif s[ll]=="x":
        cnt+=1
        ll+=1
    elif s[rr]=="x":
        cnt+=1
        rr-=1
    else:
        print(-1)
        exit()
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S = input()
ans = 0
left, right = 0, len(S)-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)
        exit()

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
t = s.replace("x","")
rt = t[::-1]
m = len(t)
if t != rt:
    print(-1)
    exit()

kouho = [0 for i in range(m+1)]
cou = 0
for i in range(n):
    if s[i] == "x":
        kouho[cou] += 1
    else:
        cou += 1

ans = 0
for j in range(-(-m//2)):
    ans += abs(kouho[j]-kouho[m-j])
    
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/code-festival-2017-qualc/tasks/code_festival_2017_qualc_c

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()

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

    is_palindrome = True
    for i in range(len(T) // 2):
        if T[i] != T[-(i+1)]:
            is_palindrome = False

    if is_palindrome:
        ans = 0
        for i in range(len(x_cnt) // 2):
            ans += abs(x_cnt[i] - x_cnt[-(i+1)])
    else:
        ans = -1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n=len(s)
ch = 0
ans = 0
left=0
right=n-1
while left<right:
    if s[left] == s[right]:
        left+=1
        right-=1
    else:
        if s[left] == 'x':
            ans+=1
            left+=1
        elif s[right] =='x':
            ans+=1
            right-=1
        else:
            print(-1)
            exit()
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = input().strip()
    N = len(s)
    l = -1
    r = N
    if len(set(s)) == 1:
        print(0)
        exit()
    while l < r:
        l += 1
        r -= 1
        while s[l] == 'x':
            l += 1
        while s[r] == 'x':
            r -= 1
        if s[l] != s[r]:
            print(-1)
            exit()
    
    ans = 0
    while l != -1 or r != N:
        if l == -1:
            ans += N - r
            break
        if r == N:
            ans += l + 1
            break
        if s[l] == s[r]:
            l -= 1
            r += 1
            continue
        elif s[l] == 'x':
            ans += 1
            l -= 1
        elif s[r] == 'x':
            ans += 1
            r += 1
        
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import ceil
s=list(input())
n=len(s)
l=[]
m=[]
buf=[]
for i in range(n):
    if s[i]!="x":
        l.append(s[i])
m=l[::-1]
#print(l,m)
left=[]
right=[]
ans=0
if m==l:
    flag=1
    for i in range(ceil(len(l)/2)):
        buf.append(l[i])
    piv=0
    cnt=0
    #print(buf)
    for i in range(n):
        if piv==len(buf):
            break
        if s[i]==buf[piv]:
            left.append(cnt)
            cnt=0
            piv=piv+1
        else:
            cnt=cnt+1
    piv=0
    cnt=0
    for i in range(n-1,-1,-1):
        if piv==len(buf):
            break
        if s[i]==buf[piv]:
            right.append(cnt)
            cnt=0
            piv=piv+1
        else:
            cnt=cnt+1
    #print(left,right)
    for i in range(len(left)):
        ans=ans+abs(left[i]-right[i])
    print(ans)
else:
    print("-1")

Submission status: Accepted

Solution code:
s = 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":
        ans += 1
        i += 1
    elif s[j] == "x":
        ans += 1
        j -= 1
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
s=input()
a=s.replace('x','')
for i in range(len(a)):
    if a[i]!=a[-1-i]:
        print(-1)
        exit()
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
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
s = input()
def calc(s):
  ans = 0
  l = 0
  r = len(s)-1
  for i in range(len(s)):
    if l >= r:
      return ans
    if s[l] == s[r]:
      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
    else:
      return -1
  return ans

a = calc(s)
print(a)