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 = list(input())
X = []
hoge = 0
for i in range(len(s)):
    if s[i] == 'x':
        hoge += 1
    else:
        X.append(hoge)
        X.append(s[i])
        hoge = 0
X.append(hoge)
ans = 0

for i in range(len(X)//2):
    if i % 2 == 0:
        ans += abs(X[i]-X[-i-1])
    elif X[i] != X[-i-1]:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

left = 0
right = N - 1

ans = 0
while left < right :
    if S[left] == S[right] :
        left += 1
        right -= 1
        continue
    if S[left] == 'x' :
        ans += 1
        left += 1
        continue
    if S[right] == 'x' :
        ans += 1
        right -= 1
        continue
    print('-1')
    break
else :
    print(ans)

Submission status: Accepted

Solution code:
def solve(s):
    n = len(s)
    t = "".join(filter(lambda _: _!= "x", s))
    if t != t[::-1]:
        return -1
    l = 0
    r = n-1
    res = 0
    while l < r:
        nl = 0
        while (l < r) and (s[l] == "x"):
            nl += 1
            l += 1
        nr = 0
        while (l < r) and (s[r] == "x"):
            nr += 1
            r -= 1
        if (s[l] == s[r]) and (s[l] != "x"):
            res += abs(nl - nr)
        l += 1
        r -= 1
    return res

assert solve("xabxa") == 2
assert solve("ab") == -1
assert solve("a") == 0
assert solve("oxxx") == 3
assert solve("axxxa") == 0

s = input()
print(solve(s))

Submission status: Accepted

Solution code:
s = input()
cnt = 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
        cnt += 1
    elif s[r] == 'x':
        r -= 1
        cnt += 1
    else:
        print(-1)
        exit()
print(cnt)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


# 回文判定
def judge(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    if news == news[::-1]:
        return True
    else:
        return False

# 操作の最小値
def min_insertion(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    n = len(news)
    fir = [0] * ((n + 1)//2)
    lat = [0] * ((n + 1) // 2)

    # 前半
    cursor = 0
    for si in s:
        if si == 'x':
            fir[cursor] += 1
        else:
            cursor += 1

        if cursor == (n+1)//2:
            break

    # 後半
    cursor = 0
    for si in s[::-1]:
        if si == 'x':
            lat[cursor] += 1
        else:
            cursor += 1

        if cursor == (n + 1) // 2:
            break

    ans = 0
    for firi, lati in zip(fir, lat):
        ans += abs(firi - lati)

    return ans


# 入力
s = ns()

# x以外の回文判定
is_kaibun = judge(s)

# 回文でないならおわり
# 回文ならば最小値出す
if is_kaibun:
    if len(set(list(s))) == 1:
        print(0)
    else:
        print(min_insertion(s))
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()
t = "".join(s.split("x"))
if t!=t[::-1]:print(-1);exit()
ans = 0
i,j=0,len(s)-1
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
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = s.replace("x", "")
    M = len(t)
    for i in range(M//2):
        if t[i] != t[M-i-1]:
            print(-1)
            return
    xcnt = [0] * (M+1)
    i = 0
    for c in s:
        if c == "x":
            xcnt[i] += 1
        else:
            i += 1
    ans = 0
    for i in range((M+1)//2):
        ans += abs(xcnt[i] - xcnt[M-i])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
S="a"+s[::]+"a"
s=s.replace("x","")
if s[:len(s)//2]!=s[:-(len(s)//2)-1:-1]:
    print(-1)
    quit()
r=0
l=len(S)-1
ans=0
while r<l:
    R,L=0,0
    while S[r+R]=="x":
        R+=1
    while S[l-L]=="x":
        L+=1
    ans+=abs(R-L)
    r+=R+1
    l-=L+1
print(ans)

Submission status: Accepted

Solution code:
import re

S = input()
without_x = ''.join([s for s in S if s != 'x'])
if without_x != without_x[::-1]: print(-1)
elif S == S[::-1]: print(0)
else:
    ans = 0
    x_lists = re.split('[{}]'.format(''.join(list(set(without_x)))), S)
    n = len(x_lists); i = 0
    while i < n-1-i:
        ans += abs(len(x_lists[i]) - len(x_lists[n-1-i]))
        i += 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
if S==S[::-1]:
    print(0)
else:
    S1 = ""
    x = []
    a = 0
    for i in range(len(S)):
        if S[i]=="x":
            a += 1
            if i==len(S)-1:
                x.append(a)
        else:
            S1+=S[i]
            x.append(a)
            a = 0
            if i==len(S)-1:
                x.append(a)
    if S1!=S1[::-1]:
        print(-1)
    else:
        ans = 0
        y = x[::-1]
        for i in range(len(x)//2):
            ans += abs(x[i]-y[i])
        print(ans)

Submission status: Accepted

Solution code:
S=input()
X=[]
SS=""
cnt=0
for s in S:
    if s=="x":
        cnt+=1
    else:
        SS+=s
        X.append(cnt)
        cnt=0
X.append(cnt)
flag=True
for i in range(len(SS)//2):
    if SS[i]!=SS[-i-1]:
        flag=False
        break
ans=0
for i in range(len(X)//2):
    ans+=abs(X[i]-X[-i-1])
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
import numpy as np


def main():
    s = input()
    snotx = s.replace('x', '')
    if snotx != snotx[::-1]:
        print(-1)
        sys.exit()
    ans = 0
    ileft = 0
    iright = len(s) - 1
    while ileft < iright:
        if s[ileft] != s[iright]:
            if s[ileft] == 'x':
                ans += 1
                ileft += 1
            elif s[iright] == 'x':
                ans += 1
                iright -= 1
            else:
                print("Error!")
                sys.exit()
        else:
            ileft += 1
            iright -= 1
    print(ans)



main()

Submission status: Accepted

Solution code:
S = input()

T = []

x_list = []

temp = 0
for i in range(len(S)):
    if S[i]!="x":
        T.append(S[i])
        x_list.append(temp)
        temp=0
    else:
        temp+=1
x_list.append(temp)

U = T[::-1]
flag=False
for i in range(len(T)):
    if T[i]!=U[i]:
        flag=True
        break

if flag:
    print(-1)
else:
    count=0
    for i in range(len(x_list)//2):
        count+=abs(x_list[i]-x_list[len(x_list)-1-i])

    print(count)

Submission status: Accepted

Solution code:
s=input()
l=len(s)
s2=''
for i in range(l):
  if s[i]!='x':
    s2+=s[i]
l2=len(s2)
for i in range(l2):
  if s2[i]!=s2[-(i+1)]:
    print(-1)
    break
else:
  lpos=0
  rpos=l-1
  cnt=0
  while rpos-lpos>=1:
    if s[lpos]==s[rpos]:
      lpos+=1
      rpos-=1
      continue
    else:
      if s[lpos]=='x':
        lpos+=1
        cnt+=1
      else:
        rpos-=1
        cnt+=1
  print(cnt)

Submission status: Accepted

Solution code:
S=input()
X=[]
SS=""
cnt=0
for s in S:
    if s!='x':
        SS+=s
        X.append(cnt)
        cnt=0
    else:
        cnt+=1
X.append(cnt)
flag=True
for i in range(len(SS)//2):
    if SS[i]!=SS[-1-i]:
        flag=False
ans=0
for i in range(len(X)//2):
    ans+=abs(X[i]-X[-1-i])
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input().rstrip()
n = len(s)
l = 0; r = n-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:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
s = input()
invs = s[::-1]
s = s


i=0
j=0
ans = 0 #sへのｘの挿入をカウント
while i<len(s) and j<len(s):
  if s[i]==invs[j]:
    i+=1
    j+=1
  elif s[i]=='x':
    i+=1
  elif invs[j]=='x':
    # sにxを挿入する想定
    j+=1
    ans+=1
  else:
    print(-1)
    break
else:
  if i==j:
    print(ans)
  elif i>j and all([c=='x' for c in invs[j:]]):
    print(ans+i-j)
  elif i<j and all([c=='x' for c in s[i:]]):
    print(ans)
  else:
    print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Inserting 'x'
https://atcoder.jp/contests/code-festival-2017-qualc/tasks/code_festival_2017_qualc_c

"""
import sys


from collections import deque

def solve(s):
    d = deque(s)
    ans = 0
    while len(d) > 1:
        if d[0] == 'x' or d[-1] == 'x':
            if d[0] == 'x' and d[-1] == 'x':
                d.popleft()
                d.pop()
            elif d[0] == 'x':
                d.popleft()
                ans += 1
            else:
                d.pop()
                ans += 1
        else:
            if d[0] == d[-1]:
                d.popleft()
                d.pop()
            else:
                return -1
    return ans


def main(args):
    s = input()
    ans = solve(s)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

s = input()

x_num = collections.deque()
x_num.append(0)
others = collections.deque()
for s_i in s:
    if s_i == 'x':
        x_num[-1] += 1
    else:
        others.append(s_i)
        x_num.append(0)
possible = 1
while len(others) > 1:
    if others.pop() != others.popleft():
        possible = 0
        break
if possible:
    ans = 0
    while len(x_num) > 1:
        ans += abs(x_num.pop() - x_num.popleft())
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
#n=int(input())
#N,M=map(int,input().split())
s=input()
l,r=0,len(s)-1
ans=0

while r-l>0:
    a,b=s[l],s[r]
    if a==b:
        l+=1
        r-=1
    elif a=='x' and b!='x':
        ans+=1
        l+=1
    elif b=='x' and a!='x':
        ans+=1
        r-=1
    else:
        ans=-1
        break

print(ans)

Submission status: Accepted

Solution code:
a = input()
b = a[::-1]
s ,t ,r= 0,0,0

while s+t<len(a):
    if a[s] == b[t]:
        s+=1
        t+=1
    elif a[s] =="x":
        r+=1
        s+=1
    elif b[t] =="x":
        r+=1
        t+=1
    else:
        print(-1)
        exit()
print(r)

Submission status: Accepted

Solution code:
s = input()
d = s.replace('x','')
l = ''
r=''
if d!= d[::-1]:
    print(-1)
    exit()
ans =0
l=[]
cnt = 0
for i in s:
    if i=='x':
        cnt+=1
    else:
        l.append(cnt)
        cnt=0
l.append(cnt)
l1 = l[:len(l)//2]
l2 = l[::-1]
for i in range(len(l1)):
    ans+=abs(l1[i]-l2[i])
print(ans)

Submission status: Accepted

Solution code:
s = input()

a = ''
for i in range(len(s)):
    if s[i]!='x':
        a+=s[i]

b = ''
for i in range(len(a)):
    b+=a[-1-i]

if a!=b:
    print(-1)
else:
    ss = ''
    for i in range(len(s)):
        ss += s[-1-i]
    i = 0
    j=0
    ans = 0
    while True:
        if i>=len(s):
            ans += len(ss)-j
            break
        elif j>=len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[j]:
            if s[i]=='x':
                i+=1
            else:
                j+=1
            ans += 1
        else:
            i+=1
            j+=1
    print(ans//2)

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()
t=s.translate(str.maketrans('','','x'))
n=len(t)
if t!=t[::-1]:
  print(-1)
else:
  A=[0]*(n+1)
  cur,ct=0,0
  for i in range(len(s)):
    if s[i]=="x":
      ct+=1
    else:
      A[cur]=ct
      cur+=1
      ct=0
    A[n]=ct
  B=A[::-1]
  ans=0
  for i in range((n+1)//2):
    ans+=abs(A[i]-B[i])
  print(ans)

Submission status: Accepted

Solution code:
s = input()
t = s[::-1]
N = len(s)

ss = ""
for i in s:
	if i != "x":
		ss += i

if ss != ss[::-1]:
	print(-1)
else:
	i = 0
	j = 0
	xcount = 0
	while i < N and j < N:
		if s[i] == t[j]:
			i += 1
			j += 1
		else:
			if s[i] == "x":
				i += 1
			else:
				j += 1
				xcount += 1
	print(xcount + N - j)

Submission status: Accepted

Solution code:
s = input()

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

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:
from collections import deque

s = deque(input())

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

left = deque(s+"@")
right = deque(s[::-1]+"@")

ans = 0

while left or right:
    if left[0] == right[0]:
        left.popleft()
        right.popleft()
    elif left[0] == "x":
        ans += 1
        left.popleft()
    elif right[0] == "x":
        ans += 1
        right.popleft()
    else:
        ans = -1
        break

ans //= 2
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

ns = ""

for s_i in s:
    if s_i != "x":
        ns += s_i

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

if ns == "":
    print(0)
    exit()

if len(ns) % 2 == 1:
    cnt = -1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2:
            note = i
            break
    left = deque(s[:note][::-1] + "@")
    right = deque(s[note+1:] + "@")
else:
    cnt = -1
    f = 1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2 - 1 and f:
            l = i
            f = 0
        if cnt == len(ns)//2:
            r = i
            break
    left = deque(s[:l][::-1] + "@")
    right = deque(s[r+1:] + "@")

ans = 0

while left or right:
    if left[0] == right[0]:
        left.popleft()
        right.popleft()
    elif left[0] == "x":
        left.popleft()
        ans += 1
    elif right[0] == "x":
        right.popleft()
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()
data=[]
X=''
cnt=0
for s in S:
    if s=='x':
        cnt+=1
    else:
        data.append(cnt)
        cnt=0
        X+=s
data.append(cnt)
ans=0
for i in range(len(data)//2):
    ans+=abs(data[i]-data[-1*i-1])
flag=True
for i in range(len(X)//2):
    if X[i]!=X[-1*i-1]:
        flag=False
if flag:print(ans)
else:print(-1)

Submission status: Accepted

Solution code:
s = input()

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

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

Submission status: Accepted

Solution code:
s = input()

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

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

Submission status: Accepted

Solution code:
s = input()

N = len(s)

score = 0
j = 0
left_len = 0
right_len = 0
flag = 0

for i in range(N):
    flag = 0
    if i + j >= N:
        continue
    left = s[i]
    if left == "x":
        left_len += 1
    else:
        while flag == 0:
            right = s[N-1-j]
            if right == "x":
                right_len += 1
            else:
                if left != right:
                    print(-1)
                    exit()
                else:
                    score += max(left_len,right_len) - min(left_len,right_len)
                    left_len = 0
                    right_len = 0
                    flag = 1
            j += 1

print(score)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
t = s.replace('x', '')
if t != t[::-1]:
    print(-1)
else:
    u = s[::-1]
    cnt = 0
    l = r = 0
    while l < n or r < n:
        if l >= n:
            cnt += 1
            r += 1
        elif r >= n:
            cnt += 1
            l += 1
        elif s[l] == u[r]:
            l += 1
            r += 1
        elif s[l] == 'x':
            cnt += 1
            l += 1
        else:
            cnt += 1
            r += 1
    print(cnt // 2)

Submission status: Accepted

Solution code:
s=input()

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

v=len(t)
if len(t)%2==1:
    if t[v//2+1:]!=t[:v//2][::-1]:
        print(-1)
        exit()
else:
    if t[v//2:]!=t[:v//2][::-1]:
        print(-1)
        exit()
v=len(s)-1
w=0
news=0
while v-w>0:
    #print(news)
    #print(s[w],s[v])
    if s[w]!=s[v]:
        if s[w]=="x":
            news+=1
            w+=1
        else:
            news+=1
            v-=1
    else:
        w+=1
        v-=1
print(news)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from collections import deque

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S=input()

S2=S.replace('x', '')
for i in range(len(S2)//2):
    if S2[i]!=S2[-(1+i)]:
        print(-1)
        exit()

que=deque(S)
i=cnt=0
while len(que)>1:
    if que[0]!=que[-1]:
        cnt+=1  
        if que[0]=='x':
            que.popleft()
        elif que[-1]=='x':
            que.pop()
    else:
        que.popleft()
        que.pop()
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
le = len(s)
cnt = 0
while le>1:
  if s[0] == s[-1]:
    s.pop()
    s.popleft()
    le -= 2
  elif s[0] == "x":
    s.popleft()
    cnt += 1
    le -= 1
  elif s[-1] == "x":
    s.pop()
    cnt += 1
    le -= 1
  else:
    print(-1)
    break
else:
  print(cnt)

Submission status: Accepted

Solution code:

import sys
from collections import deque
import copy
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]
    head = 0
    tail = len(S) - 1
    count = 0
    #for i in range(len(S)/2 + len(S) % 2):
    while 1:
        if head >= tail:
            break
        if S[head] != S[tail]:
            if S[head] == "x":
                count += 1
                head += 1
            elif S[tail] == "x":
                count += 1
                tail -= 1
            else:
                print -1
                return
        else:
            head += 1
            tail -= 1
    print count


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
rs = "".join(reversed(s))
n = len(s)

res = 0
i = 0
j = 0
while i+j<n:
    if s[i]==rs[j]:
        i += 1
        j += 1
    elif s[i]=="x":
        res += 1
        i += 1
    elif rs[j]=="x":
        res += 1
        j += 1
    else:
        print(-1)
        exit()

print(res)

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:  break
if i <=j :
  print(-1)
  exit(0) 
else: print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
mae = 0
ushiro = n - 1
ans = 0

while mae != ushiro:
    if s[mae] == s[ushiro]:
        mae += 1
        ushiro -= 1
        if mae == ushiro + 1:
            break
    elif s[mae] != s[ushiro]:
        if s[mae] == "x":
            mae += 1
            ans += 1
        elif s[ushiro] == "x":
            ushiro -= 1
            ans += 1
        else:
            print(-1)
            exit()

print(ans)

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
    else:
        if 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()
N = len( s)
ans = 0
l = 0
r = N-1
while l < r:
    lx = 0
    rx = 0
    while l <= N-1:
        if s[l] == "x":
            lx += 1
            l += 1
        else:
            break
    while 0 <= r:
        if s[r] == "x":
            rx += 1
            r -= 1
        else:
            break
    if l == r:
        ans += abs( lx - rx)
    if l < r:
        if s[l] == s[r]:
            ans += abs(lx - rx)
        else:
            ans = -1
            break
    l += 1
    r -= 1
print( ans)

Submission status: Accepted

Solution code:
s = input()
ans = 0
lb = 0
rb = len(s) - 1
while rb - lb > 0:
    if s[lb] == s[rb]:
        lb += 1
        rb -= 1
    elif s[lb] == 'x':
        lb += 1
        ans += 1
    elif s[rb] == 'x':
        rb -= 1
        ans += 1
    else:
        ans = -1
        break
print(ans)
# https://img.atcoder.jp/code-festival-2017-qualc/editorial.pdf

Submission status: Accepted

Solution code:
s = input()
ans = 0
while s:
    if s[0] == s[-1]:
        s = s[1:-1]
    elif s[0] == 'x':
        s = s[1:]
        ans += 1
    elif s[-1] == 'x':
        s = s[:-1]
        ans += 1
    else:
        ans = -1
        break
print(ans)
# https://img.atcoder.jp/code-festival-2017-qualc/editorial.pdf

Submission status: Accepted

Solution code:
s = list(input())
without_x = []
notx_index = []
for i in range(len(s)):
	if s[i] != "x":
		without_x.append(s[i])
		notx_index.append(i)
if not without_x:
	print(0)
elif without_x != without_x[::-1]:
	print(-1)
else:
	n = len(notx_index)
	#center_ind = (n-1) // 2
	x_num = [notx_index[0]]
	for i in range(n-1):
		x_num.append(notx_index[i+1] - notx_index[i] - 1)
	x_num.append(len(s)-1 - notx_index[n-1])
	#print(x_num)
	ans = 0
	for i in range(n // 2 + 1):
		ans += abs(x_num[i] - x_num[n-i])

	print(ans)

Submission status: Accepted

Solution code:
s=list(input())
S=[]
for i in range(len(s)):
    if s[i]=="x":
        pass
    else:
        S.append(s[i])
        
#print(S)
sr=s[::-1]
SR=S[::-1]
if S!=SR:
    print(-1)
else:
    if len(S)%2==0:
        L=len(S)//2
    else:
        L=len(S)//2+1
    SF=[]
    cntx=0
    for y in range(len(s)):
        if s[y]=="x":
            cntx+=1
        else:
            SF.append(cntx)
            cntx=0
    SF.append(cntx)
    SE=SF[::-1]
    ans=0
    for x in range(len(SE)):
        ans+=abs(SE[x]-SF[x])
    print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
s = input()
t = s.replace('x', '')
if t != t[::-1]:
    print(-1)
    sys.exit()
if not t:
    print(0)
    sys.exit()
H = []
ctr = 0
for i in s:
    if i != 'x':
        H.append(ctr)
        ctr = 0
        continue
    ctr += 1
H.append(ctr)
print(sum([abs(i-j) for i, j in zip(H, H[::-1])])//2)

Submission status: Accepted

Solution code:
from math import ceil
S=list(input())
num_x=S.count("x")
N=len(S)
num=N-num_x
cf,cr=0,0
F,R=[],[]

f,r=0,N-1
while len(F)<ceil(num/2):
  if S[f]=="x":
    cf+=1
  else:
    F.append((S[f],cf))
    cf=0
  f+=1
      
while len(R)<ceil(num/2):
  if S[r]=="x":
    cr+=1
  else:
    R.append((S[r],cr))
    cr=0
  r-=1
ans=0  
for i in range(ceil(num/2)):   
  if F[i][0]!=R[i][0]:
    print(-1)
    exit()
  else:
    ans+=abs(F[i][1]-R[i][1])
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
X, A = [], []
NX = ""

for i in range(len(S)):
    if S[i] == "x": X.append(i)
    else: 
        NX += S[i]
        A.append(i)
    
for i in range(len(NX)//2):
    if NX[i] != NX[-1-i]:
        print(-1)
        break
else:
    if NX == "": print(0)
    else:
        l, r = 0, len(S)-1
        midl = A[(len(NX)-1)//2]
        midr = (A[(len(NX)-1)//2] if len(NX)%2==1 else A[(len(NX)-1)//2 + 1])
        Add = 0
        while l < midl or r > midr:
            if S[l] == S[r] == "x":
                l += 1
                r -= 1
            elif S[l] == "x":
                l += 1
                Add += 1
            elif S[r] == "x":
                r -= 1
                Add += 1
            else:
                l += 1
                r -= 1
        print(Add)

Submission status: Accepted

Solution code:
S = input()

l,r = 0, len(S)-1
cnt = 0

while l<r:
  if S[l] == S[r]:
    l, 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(0)
    
print(cnt)

Submission status: Accepted

Solution code:
import math
s = str(input())
cou = 0
co = 0
ans = 0
while cou + co +2 <= len(s):
  if s[cou] == "x":
    if s[-1-co] == "x":
      cou += 1
      co += 1
    else:
      cou += 1
      ans += 1
  else:
    if s[-1-co] == "x":
      co += 1
      ans += 1
    else:
      if s[cou] == s[-1-co]:
        cou += 1
        co += 1
      else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
S=input()
X=S.replace("x","")
N=len(X)
M=len(S)
for i in range(N):
    if X[i]!=X[-i-1]:
        print(-1)
        exit()
T=""
for i in range(M):
    if S[i]!="x":
        T=T+"o"
    else:
        T=T+"x"
U=[len(i) for i in T.split("o")]
ans=0
K=len(U)
for i in range(K):
    ans+=abs(U[i]-U[-i-1])
    tmp=max(U[i],U[-i-1])
    U[i]=tmp
    U[-1-i]=tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
s1=s[::-1]
a,b=0,0
ca,cb=0,0
while a<len(s) and b<len(s):
    if s[a]==s1[b]:
        a+=1
        b+=1
    else:
        if s[a]=='x':
            ca+=1
            a+=1
        elif s1[b]=='x':
            cb+=1
            b+=1
        else:
            ca,cb=-1,-1
            break
print(max(ca,cb))

Submission status: Accepted

Solution code:
s = input()
L = ""
for c in s:
    if c!="x":
        L += c
n = len(L)
for i in range(n):
    if L[i] == L[-1-i]:
        continue
    else:
        print(-1)
        exit()
x = 0
il = 0
ir = 0
ans = 0
for c in L[:(n+1)//2]:
    x = 0
    while s[il]=="x":
        x+=1
        il+=1
    il+=1
    while s[-1-ir]=="x":
        x-=1
        ir+=1
    ir+=1
    ans += abs(x)
print(ans)