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 → xa x bxa → xaxbxa x       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 → x oxxx → x xoxxx → x xxoxxx

Solution (Python):






def f(str):
    numOfX = 0
    numOfXs = []
    letters = []
    
    for c in str:
        if c == 'x':
            numOfX += 1
        else:
            letters.append(c)
            numOfXs.append(numOfX)
            numOfX = 0
            
    numOfXs.append(numOfX)

    if any(letters[i] != letters[-i-1] 
           for i in range(len(letters)//2)):
        return -1

    return sum(abs(numOfXs[i] - numOfXs[-i-1]) 
            for i in range(len(numOfXs)//2))

def test():
    assert f('xabxa') == 2
    assert f('ab') == -1
    assert f('a') == 0
    assert f('oxxx') == 3


def main():
    str = input()
    print(f(str))

if __name__ == '__main__':
    main()

Solution (Python):


def f(str):
    numOfX = 0
    numOfXs = []
    letters = []
    
    for c in str:
        if c == 'x':
            numOfX += 1
        else:
            letters.append(c)
            numOfXs.append(numOfX)
            numOfX = 0
            
    numOfXs.append(numOfX)

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

    a = 0
    for i in range(len(numOfXs)//2):
        a += abs(numOfXs[i] - numOfXs[-i-1])
            
    return a

def test():
    assert f('xabxa') == 2
    assert f('ab') == -1
    assert f('a') == 0
    assert f('oxxx') == 3


def main():
    str = input()
    print(f(str))

if __name__ == '__main__':
    main()

Solution (Python):
s = input()

count = 0
left, right = 0, len(s)-1
while True:
    if s[left] == s[right]:
        left, right = left+1, right-1
    elif s[left] == 'x':
        left = left+1
        count += 1
    elif s[right] == 'x':
        right = right-1
        count += 1
    else :
        count = -1
        break
    if left >= right:
        break
print(count)

Solution (Python):
txt=raw_input()
i=0
j=len(txt)-1
x_count = 0
while i<j:
	if txt[i]==txt[j]:
		i+=1
		j-=1
	elif txt[i]=='x':
		i+=1
		x_count+=1
	elif txt[j]=='x':
		j-=1
		x_count+=1
	else:
		print -1
		exit(0)
print x_count

Solution (Python):
s = list(input())
count = 0
l = 0
r = len(s)-1







while l != r:
    if s[l] != s[r]:
        if s[l] != "x" and s[r] != "x":
            count = -1
            break
        elif s[l] == "x" and s[r] != "x":
            l += 1
            count += 1
        elif s[r] == "x" and s[l] != "x":
            r -= 1
            count += 1
    elif s[l] == s[r]:
        if abs(l-r) == 1:
            break
        else:
            l += 1
            r -= 1

print(count)

Solution (Python):
s=raw_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' and s[j]!='x':
        ans=-1
        break
    elif s[i]=='x':
        ans+=1
        i+=1
    elif s[j]=='x':
        ans+=1
        j-=1
print ans

Solution (Python):
s = input()
t = [a for a in s if a!="x"]
u = []
tmp = 0
for a in s:
    if a == "x":
        tmp += 1
    else:
        u.append(tmp)
        tmp = 0
u.append(tmp)

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

Solution (Python):
s = input()

cnt = 0
ans = 0

if len(s) == 1:
    cnt = 0
else:
    l = 0
    k = 0
    while True:
        if s[l] == s[-k-1]:
            l += 1
            k += 1
            if l + k >= len(s)-1:
                break
        elif s[l] == 'x':
            l += 1
            cnt += 1
            if l + k >= len(s)-1:
                break
        elif s[-k-1] == 'x':
            k += 1
            cnt += 1
            if l + k >= len(s)-1:
                break
        else:
            ans = -1
            break

if ans == -1:
    print(ans)
else:
    print(cnt)

Solution (Python):
import sys

s = input()
s_no_x = s.replace('x', '')
s_no_x_inv = s_no_x[::-1]

if not s_no_x == s_no_x_inv:
    print(-1)
    sys.exit()

count = 0
l = 0
r = len(s) - 1
while l < r:
    if l >= r:
        break
    if s[l] == s[r]:
        l += 1
        r -= 1
    if not s[l] == s[r]:
        count += 1
        if s[l] == 'x':
            l += 1
        else:
            r -= 1
print(count)

Solution (Python):
data = input()
tmp = data.replace("x", "")
c1 , c2, num = 0, -1, 0
if tmp == tmp[::-1]:
    while(c1 < len(data) + c2):
        if data[c1] == data[c2]:
            c1+=1
            c2-=1
        elif data[c2]=="x":
            c2-=1
            num+=1
        else:
            c1+=1
            num+=1
else:
    num = -1
print(num)

Solution (Python):


import sys
import os
import math
import itertools
import heapq
import numpy as np
from collections import OrderedDict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
sys.setrecursionlimit(10000)

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]


class Dijkstra():
    def dijkstra(self, adj, start, goal=None):
        '''
        ダイクストラアルゴリズムによる最短経路を求めるメソッド
        入力
        adj: adj[i][j]の値が頂点iから頂点jまでの距離(頂点iから頂点jに枝がない場合，値はfloat('inf'))となるような2次元リスト(正方行列)
        start: 始点のID
        goal: オプション引数．終点のID
        出力
        goalを引数に持つ場合，startからgoalまでの最短経路を格納したリストを返す
        持たない場合は，startから各頂点までの最短距離を格納したリストを返す
        d = Dijkstra()
        d.dijkstra([[float('inf'), 2, 4, float('inf'), float('inf')], [2, float('inf'), 3, 5, float('inf')], [4, 3, float('inf'), 1, 4], [float('inf'), 5, 1, float('inf'), 3], [float('inf'), float('inf'), 4, 3, float('inf')]], 0)
        [0, 2, 4, 5, 8] 
        d.dijkstra([[float('inf'), 2, 4, float('inf'), float('inf')], [2, float('inf'), 3, 5, float('inf')], [4, 3, float('inf'), 1, 4], [float('inf'), 5, 1, float('inf'), 3], [float('inf'), float('inf'), 4, 3, float('inf')]], 0, goal=4)
        [0, 2, 4] 
        '''
        num = len(adj)  
        dist = [float('inf') for i in range(num)]  
        prev = [float('inf') for i in range(num)]  

        dist[start] = 0
        q = []  
        heapq.heappush(q, (0, start))  

        while len(q) != 0:
            prov_cost, src = heapq.heappop(q)  

            
            if dist[src] < prov_cost:
                continue

            
            for dest in range(num):
                cost = adj[src][dest]
                if cost != float('inf') and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost  
                    heapq.heappush(q, (dist[dest], dest))  
                    prev[dest] = src  

        if goal is not None:
            return self.get_path(goal, prev)
        else:
            return dist

    def get_path(self, goal, prev):
        '''
        始点startから終点goalまでの最短経路を求める
        '''
        path = [goal]  
        dest = goal

        
        while prev[dest] != float('inf'):
            path.append(prev[dest])
            dest = prev[dest]

        
        return list(reversed(path))


class EndLoop(Exception):
    pass


def debug_print(L):
    for col in L:
        print(col)


def debug_print_joined(L):
    for col in L:
        print("".join(col))


def transpose_matrix(L):
    transposed = list(map(list, zip(*L)))
    return transposed

def kaibun(L):
    L2 = L.copy()
    L2.reverse()
    if L == L2:
        return True
    else:
        return False


def solve():
    s = deque(input())
    flag = True
    cnt = 0
    while s:
        if s[0] == s[-1]:
            s.pop()
            if not s:
                break
            s.popleft()
        else:
            if s[0] == "x":
                s.append("x")
                cnt += 1
            elif s[-1] == "x":
                s.appendleft("x")
                cnt += 1
            else:
                print("-1")
                flag = False
                break

    if flag:
        print(cnt)


solve()

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

Solution (Python):
from collections import deque
import sys

if __name__ == '__main__':
    s = list(input())
    que = deque(s)

    ans = 0
    while len(que) != 0:
        if que[0] == que[-1]:
            if len(que) == 1:
                que.pop()
            else:
                que.popleft()
                que.pop()
        elif que[0] != "x" and que[-1] != "x":
            print(-1)
            sys.exit()
        else:
            if que[0] == "x":
                que.append("x")
                ans += 1
            elif que[-1] == "x":
                que.appendleft("x")
                ans += 1

    print(ans)

Solution (Python):
s = input()
ns = s.replace('x', '')
if s == s[::-1]:
    print(0)
elif ns != ns[::-1]:
    print(-1)
else:
    cnt = 0
    i = 0
    j = len(s)-1
    while(i<j):
        if s[i]=='x' and s[j]!='x':
            cnt += 1
            j += 1
        elif s[i]!='x' and s[j]=='x':
            cnt += 1
            i -= 1
        i += 1
        j -= 1
    print(cnt)

Solution (Python):
s = input()
ns = s.replace('x', '')
if s == s[::-1]:
	print(0)
elif ns != ns[::-1]:
	print(-1)
else:
	cnt = 0
	i = 0
	j = len(s)-1
	while(i<j):
		if s[i]=='x' and s[j]!='x':
			cnt += 1
			j += 1
		elif s[i]!='x' and s[j]=='x':
			cnt += 1
			i -= 1
		i += 1
		j -= 1
	print(cnt)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    s = input()
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()

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

print(ans)

Solution (Python):


S = input()
r = len(S) - 1
l = 0
cnt = 0
f = True
while l <= r:
    if S[l] == S[r]:
        l += 1
        r -= 1
    elif S[l] == 'x':
        cnt += 1
        l += 1
    elif S[r] == 'x':
        cnt += 1
        r -= 1
    else:
        f = False
        break
if f:
    print(cnt)
else:
    print(-1)

Solution (Python):
s = input()
ans = 0
p = 0
q = len(s) - 1
while p < q:
	if s[p] == s[q]:
		p += 1
		q -= 1
	elif s[p] == "x":
		p += 1
		ans += 1
	elif s[q] == "x":
		q -= 1
		ans += 1
	else:
		ans = -1
		break
print(ans)

Solution (Python):
s = list(str(input()))
l = 0
n = len(s)-1
ans = 0
while l < n:
    if(s[l] == s[n]):
        l += 1
        n -= 1
    elif(s[l] == 'x'):
        l += 1
        ans += 1
    elif(s[n] == 'x'):
        n -= 1
        ans += 1
    elif(s[l] != 'x' and s[n] != 'x'):
        ans = -1
        break
print(ans)

Solution (Python):

s = input()

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

Solution (Python):
s = input()
len_s = len(s)
l_index = 0
r_index = len_s - 1
counter = 0
while l_index < r_index:
    if s[l_index] == s[r_index]:
        l_index += 1
        r_index -= 1
    elif s[l_index] == "x":
        counter += 1
        l_index += 1
    elif s[r_index] == "x":
        counter += 1
        r_index -=1
    else:
        counter = -1
        break
print(counter)

Solution (Python):
import sys
S = str(input())
i = 0
j = len(S) - 1
c = 0
while(True):
    if i >= j:
        break
    if (S[i] == S[j]):
        i += 1
        j -= 1
    else:
        if (S[i] == 'x'):
            i += 1
            c += 1
        elif (S[j] == 'x'):
            j -= 1
            c += 1
        else:
            print('-1')
            sys.exit()
print(c)

Solution (Python):
use = input()

total = 0
p = 0
p1 = len(use) - 1
while p < p1:
	if use[p] == use[p1]:
		p += 1
		p1 -= 1
	elif use[p] == "x":
		p += 1
		total += 1
	elif use[p1] == "x":
		p1 -= 1
		total += 1
	else:
		total = -1
		break
print(total)

Solution (Python):
s = input()

if s == s[::-1]:
    print(0)
else:
    t = s.replace('x', '')
    if t != t[::-1]:
        print(-1)
    else:
        f = 0
        b = len(s) - 1
        count = 0
        while True:
            if s[f] == s[b]:
                f += 1
                b -= 1
            else:
                count += 1
                if s[f] == "x":
                    f += 1
                elif s[b] == "x":
                    b -= 1
            if f >= b:
                break
        print(count)

Solution (Python):
S = input()

l = 0
r = len(S) - 1
ret = 0
while l <= r:
    if S[l] == S[r]:
        l += 1
        r -= 1
    elif S[l] == 'x' and S[r] != 'x':
        ret += 1
        l += 1
    elif S[l] != 'x' and S[r] == 'x':
        ret += 1
        r -= 1
    else:
        print(-1)
        exit()

print(ret)

Solution (Python):
n = input()

nc = n.replace("x","")

ncr = nc[::-1]

ans = -1

if ncr == nc:
    ans = 0
    nl = list(n)
    lenn = len(n)
    i = 0
    j = 1
    ri = 0
    le = 0
    while(1):
        if i >= lenn-(j):break
        if n[i] == n[lenn-(j)]:
            i += 1
            j += 1
        else:
            if n[i] == "x":
                i +=1
            else:
                j +=1
            ans += 1

print(ans)

Solution (Python):



"""code-fes-2017-qual-c-c"""


import sys

OP_NUM = 0

def solve(s):
    """Solve."""
    global OP_NUM

    if len(s) == 0:
        return ""
    if len(s) == 1:
        return ""

    if s[0] == 'x':
        if s[-1] == 'x':
            return s[1:-1]
        OP_NUM += 1
        return s[1:]

    if s[0] == s[-1]:
        return s[1:-1]
    if 'x' == s[-1]:
        OP_NUM += 1
        return s[:-1]

    return None


def main():
    """Main function."""
    s = input()

    while s:
        s = solve(s)
        if s is None:
            print(-1)
            sys.exit(0)

    print(OP_NUM)

if __name__ == '__main__':
    sys.exit(main())

Solution (Python):


s = input()

i = 0
j = len(s)-1
result = 0
while i < j:
    if s[i] == s[j]:
        i += 1
        j -= 1
    elif s[i] == "x":
        result += 1
        i += 1
    else:
        result += 1
        j -= 1

if i == j:
    if s[:i].replace("x", "") == s[i+1:][::-1].replace("x", ""):
        print(result)
    else:
        print(-1)
else:
    if s[:j+1].replace("x", "") == s[i:][::-1].replace("x", ""):
        print(result)
    else:
        print(-1)

Solution (Python):
import numpy as np
s = input()
s_list = list(s)
s_array = np.array(s_list)
s_without_x_ar = s_array[s_array != "x"]

if (s_without_x_ar != s_without_x_ar[::-1]).any():
    print(-1)
else:
    kw_count = len(s_without_x_ar)
    if kw_count == 0:
        print(0)
    elif kw_count % 2 == 0:
        half_s_wox_left = s_without_x_ar[:int(kw_count/2)]
        lastw_count = len(half_s_wox_left[half_s_wox_left==half_s_wox_left[-1]])
        check_last_idx = np.where(s_array == half_s_wox_left[-1])[0][lastw_count-1]
        half_array = s_array[:check_last_idx+1]
        
        
        a = np.where(half_array!="x")[0]
        a = np.insert(a,0,-1)
        left_count_ar = a[1:] - a[:-1] - 1


        
        half_s_wox_right = s_without_x_ar[int(kw_count/2):]
        half_s_wox_right_back = half_s_wox_right[::-1]
        s_array_back = s_array[::-1]

        lastw_count = len(half_s_wox_right_back[half_s_wox_right_back==half_s_wox_right_back[-1]])
        check_last_idx = np.where(s_array_back == half_s_wox_right_back[-1])[0][lastw_count-1]

        half_array = s_array_back[:check_last_idx+1]
        
        a = np.where(half_array!="x")[0]
        a = np.insert(a,0,-1)
        right_count_ar = a[1:] - a[:-1] - 1
        
        print(np.abs(right_count_ar - left_count_ar).sum())
        
    else:
        half_s_wox_left = s_without_x_ar[:int(kw_count//2+1)]
        lastw_count = len(half_s_wox_left[half_s_wox_left==half_s_wox_left[-1]])
        check_last_idx = np.where(s_array == half_s_wox_left[-1])[0][lastw_count-1]
        half_array = s_array[:check_last_idx+1]
        
        
        a = np.where(half_array!="x")[0]
        a = np.insert(a,0,-1)
        left_count_ar = a[1:] - a[:-1] - 1


        
        half_s_wox_right = s_without_x_ar[int(kw_count//2):]
        half_s_wox_right_back = half_s_wox_right[::-1]
        s_array_back = s_array[::-1]

        lastw_count = len(half_s_wox_right_back[half_s_wox_right_back==half_s_wox_right_back[-1]])
        check_last_idx = np.where(s_array_back == half_s_wox_right_back[-1])[0][lastw_count-1]

        half_array = s_array_back[:check_last_idx+1]
        
        a = np.where(half_array!="x")[0]
        a = np.insert(a,0,-1)
        right_count_ar = a[1:] - a[:-1] - 1
        
        print(np.abs(right_count_ar - left_count_ar).sum())

Solution (Python):
S = input()

st = 0
en = len(S) - 1
ans = 0
while st < en:
    if S[st] == S[en]:
        st += 1
        en -= 1
    elif S[st] == 'x':
        ans += 1
        st += 1
    elif S[en] == 'x':
        ans += 1
        en -= 1
    else:
        ans = -1
        break

print(ans)

Solution (Python):
import math as mt
import numpy as np

string = input()
collect = []
word = ''
count = 0
n = len(string)
i = 0
for x in string:
    if x != 'x':
        word += x
        collect.append(count)
        if i == n-1:
            collect.append(0)
        count = 0
    elif i == n-1:
        count += 1
        collect.append(count)
    else:
        count += 1
    i += 1

nl = len(word) - 1
i = 0
while i < nl:
    if word[i] != word[nl]:
        print(-1)
        exit()
    i += 1
    nl -=  1
nl = len(collect) - 1
i = 0
out = 0
while i < nl:
    out += np.abs(collect[i] - collect[nl])
    i += 1
    nl -= 1
print(out)

Solution (Python):
s = input()


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

for i in range( len(s) ):
	if r <= l:
		break
	if s[l] == s[r]:
		l += 1
		r -= 1
	else:
		if (s[l] != 'x' and s[r] != 'x'):
			ans = -1
			break
		else:
			ans += 1
			if s[l] == 'x':
				l += 1
			else:
				r -= 1
print(ans)

Solution (Python):
s = raw_input()
sr = s[::-1]
sx = s.replace("x","")

if s == sr:
  print 0
  exit(0)

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

x1 = []
x2 = []
pos1 = 0
pos2 = 0

for a in s:
  if a != "x":
    x1.append(pos1)
    pos1 = 0
  else:
    pos1 += 1
x1.append(pos1)

for a in sr:
  if a != "x":
    x2.append(pos2)
    pos2 = 0
  else:
    pos2 += 1
x2.append(pos2)

count = 0
for a in range(len(x1)):
  count += abs(x1[a]-x2[a])

print count/2

Solution (Python):
a=input()
b=a[::-1]
count=0
no=1
la=0
lb=0

while la+lb+2<=len(a):

    if a[la]==b[lb]:
        la=la+1
        lb=lb+1
    else:
        if a[la]=="x":
            count=count+1
            la=la+1
        elif b[lb]=="x":
            count=count+1
            lb=lb+1
        else:
            print(-1)
            no=0
            break
if no==1:
    print(count)

Solution (Python):
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
ss=s.replace("x","")
if ss!=ss[::-1]:
 print(-1)
else:
 e=[]
 for i in range(len(ss)):
  e.append(ss[i])
 j=0
 k=0
 d1=[]
 jj=0
 kk=0
 d2=[]
 sr=s[::-1]
 for i in range(len(s)):
  if k<len(e) and s[i]==e[k] :
    d1.append(s[j:i])
    j=i+1
    k+=1
  if kk<len(e) and sr[i]==e[kk] :
    d2.append(sr[jj:i])
    jj=i+1
    kk+=1
 d1.append(s[j:])
 d2.append(sr[jj:]) 
 dd=0  
 for i in range(len(d1)//2):
   dd+=abs(len(d1[i])-len(d2[i]))
 print(dd)

Solution (Python):




"""
Created on Mon Oct  9 22:12:40 2017

@author: kitagenbakurou
"""
import numpy as np
import copy

def insert(pos, s, x):
  return x.join([s[:pos], s[pos:] ])

s = input()
N = len(s)

i = 0
j = N - 1
cnt = 0
flag = True
while i < j : 
    if s[i] != s[j] : 
        if s[i] == "x" : 
            cnt += 1
            
            i += 1
            j += 0
        elif s[j] == "x" : 
            cnt += 1
            
            i +=0
            j -= 1
        else : 
            flag = False
            break
    else : 
        i += 1
        j -= 1

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

Solution (Python):
S = input()
s = []
roop = True
index = [0]
cnt = 0
for i in S:
    if not(i == "x"):
        s.append(i)
        index.append(0)
    else:
        index[-1] += 1
for i in range(len(s) // 2):
    if s[i] != s[- i - 1]:
        roop = False
if roop:
    for i in range(len(index) // 2):
        if index[i] - index[- i - 1] >= 0:
            cnt += index[i] - index[- i - 1]
        else:
            cnt += index[- i - 1] - index[i]
    print(cnt)
else:
    print(-1)

Solution (Python):
s = input()
a = [0]*26
flag = 0
for i in s:
	a[ord(i) - ord('a')]+=1;
for i in range(len(a)):
	if i == ord('x') - ord('a'):
		continue;
	flag += (a[i]%2)
if flag > 1:
	print(-1)
else:
	left = 0;
	right = len(s) - 1;
	ans = 0
	while(left < right):
		if s[left] == s[right]:
			left+=1;
			right-=1;
		else:
			if s[left] == 'x':
				ans+=1;
				left += 1;
			elif s[right] == 'x':
				ans+= 1;
				right -= 1;
			else:
				flag = -1;
				break;
	if flag == -1:
		print(-1)
	else:
		print(ans);

Solution (Python):
def main():
    S = input()

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

    i1 = 0
    i2 = len(S) - 1
    cnt = 0
    while True:
        if S[i1] == 'x' and S[i2] != 'x':
            cnt += 1
            i1 += 1
        elif S[i1] != 'x' and S[i2] == 'x':
            cnt += 1
            i2 -= 1
        else:
            i1 += 1
            i2 -= 1
        if i2 - i1 < 1:
            break

    print(cnt)

main()

Solution (Python):
S = input()
n, m = 0, 1
s = []
patern = 0
for i in range(len(S)):
    s.append(S[i])
while n + m < len(s):
    if s[n] is 'x':
        if s[-m] is not 'x':
            m -= 1
            patern += 1
    else:
        if s[-m] is 'x':
            n -= 1
            patern += 1
        elif s[-m] is not s[n]:
            patern = -1
            break
    n += 1
    m += 1
if patern != -1:
    print(patern)
else:
    print(patern)

Solution (Python):
S = input()
n, m = 0, 1
s = []
patern = 0
for i in range(len(S)):
    s.append(S[i])
while n + m < len(s):
    if s[n] is 'x':
        if s[-m] is not 'x':
            s.append('x')
            patern += 1
    else:
        if s[-m] is 'x':
            n -= 1
            patern += 1
        elif s[-m] is not s[n]:
            patern = -1
            break
    n += 1
    m += 1
if patern != -1:
    print(patern)
else:
    print(patern)

Solution (Python):


s = input()
sx = s.replace('x','')
IsPossible = True
for i in range(int(len(sx)/2)):
    if(sx[i] != sx[-(i+1)]) : IsPossible = False

ans = 0
if IsPossible:
    l, r = 0, 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
    print(ans)
else:
    print(-1)

Solution (Python):
def palindrome(s):
  for i in range(len(s) // 2):
    if s[i] != s[-i -1]:
      return False
  return True

def main():
  s = input()
  t = ''
  for ch in s:
    if ch != 'x':
      t += ch

  if len(t) == 0:
    print(0)
    return

  if (palindrome(t) == False):
    print(-1)
    return

  cnt = 0
  odd = len(t) & 1
  mid = len(t) // 2

  if odd == 1:  

    for i in range(len(s)):
      if s[i] != 'x':
        cnt += 1
        if cnt == mid + 1:
          s1 = s[      : i]
          s2 = s[i + 1 :  ]
          break    

  else: 

    for i in range(len(s)):
      if s[i] != 'x':
        cnt += 1
        if cnt == mid:
          s1 = s[  : i + 1]
        if cnt == mid + 1:
          s2 = s[i :  ]
          break

  
  ans = 0
  i = 0
  j = 0
  while (i < len(s1) and j < len(s2)):
    c1 = s1[i]
    c2 = s2[-j-1]
    if c1 == c2:
      i += 1
      j += 1
      continue

    ans += 1
    if c1 == 'x':
      i += 1
    else:
      j += 1

  ans += (len(s1) - i) + (len(s2) - j)
  print(ans)






if __name__ == '__main__':
  main()

Solution (Python):
s = input()
i = 0
j = len(s)-1
ist = 0

while i < j:
    if s[i] == s[j]:
        i += 1
        j -= 1
    elif s[i] == 'x':
        ist += 1
        i += 1
    elif s[j] == 'x':
        ist += 1
        j -= 1
    else:
        print(-1)
        exit()

print(ist)

Solution (Python):
s = input()

def kaibun(string):
    if string==string[::-1]:
        return True
    else:
        return False

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

if kaibun(a) == True:
    cnt = 0
    i = 0
    j = len(s) - 1
    while i < j:
        if s[i] != s[j]:
            if s[i] == 'x':
                cnt += 1
                i += 1
            elif s[j] == 'x':
                cnt += 1
                j -= 1
        else:
            i += 1
            j -= 1

    print(cnt)
else:
    print('-1')

Solution (Python):
S = input()
N = len(S)
st = 0
la = N - 1
result = 0

while(st < la):
  if S[st] == 'x' and S[la] == 'x':
    st += 1
    la -= 1
    continue
  if S[st] != 'x' and S[la] == 'x':
    la -= 1
    result += 1
  elif S[st] == 'x' and S[la] != 'x':
    st += 1
    result += 1
  elif S[st] == S[la]:
    st += 1
    la -= 1
  else:
    result = -1
    break

print(result)

Solution (Python):
def kaibun(s):
	for i in range( len(s) //2 ):
		
		if (s[i] != s[(-1*i) -1]):
			return False
	return True

s = input()		


if kaibun(s):
	print(0)
	exit(0)

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

if not kaibun(t):
	print(-1)
	exit(0)


a=0
b=len(s)-1
ans=0

while a < b:
	A=s[a]
	B=s[b]
	
	
	if A==B:
		a+=1
		b-=1
		continue
	
	if B=='x':
		b-=1
		ans+=1
		continue
	
	a+=1
	ans+=1


print(ans)

Solution (Python):
import re
s = input()
s_without_x = s.replace("x", "")
if s_without_x[::-1] != s_without_x:
    print(-1)
else:
    length = len(s_without_x)
    idx = 0
    where = [0 for _ in range(length + 1)]
    for i in range(len(s)):
        now = s[i]
        if now == "x":
            where[idx] += 1
        else:
            idx += 1
    res = 0
    rev = where[::-1]
    for i in range(len(rev)):
        res += abs(where[i] - rev[i])
    print(int(res / 2))

Solution (Python):
inputs = input()

a = inputs

ans = 0
while len(a) > 1:
    if a[0] == a[-1]:
        a = a[1:-1]
    elif a[0] == 'x' and a[-1] != 'x':
        ans += 1
        a += 'x'
    elif a[0] != 'x' and a[-1] == 'x':
        ans += 1
        a = 'x' + a
    elif a[0] != a[-1]:
        ans = -1
        break
        
print(ans)

Solution (Python):
s = raw_input()

replaced = s.replace('x', '')
l = len(replaced)
halfl = int(l / 2)

if l > 1 and replaced[:halfl] != replaced[-halfl:][::-1]:
  print "-1"
  exit()

xset = {}
i = 0
for c in s:
  if c == 'x':
    d = min(i, l-i)
    if i != l-i:
      xset[d] = xset.get(d, 0) + [-1, 1][i < l-i]
  else:
    i += 1

ans = 0
for v in xset.itervalues():
  ans += abs(v)
print ans

Solution (Python):
s = raw_input()

def precheck():
    res = ""
    for c in s:
        if c != "x":
            res += c
    if not res:
        return 1
    if res != "".join(reversed(res)):
        return 0
    return res



def calc(ns):
    lf = ""
    rf = ""
    
    vtype = len(ns) % 2

    pos = 0

    cnt = 0
    while cnt < len(ns)/2:
        lf += s[pos]
        if s[pos] == "x":
            pass
        else:
            cnt+=1
        pos+=1
    
    if vtype:
        while s[pos] == "x":
            lf += s[pos]
            pos+=1
        pos+=1
    else:
         while s[pos] == "x":
            pos+=1

   

    rf = "".join(reversed(s[pos:]))


    lx = 0
    rx = 0
    res = 0
    while 1:

        if lx >= len(lf):
            res += len(rf)-rx
            break
        if rx >= len(rf):
            res += len(lf)-lx
            break

        rrr = rf[rx] == "x"
        lll = lf[lx] == "x"

        if rrr and lll:
            rx+=1
            lx+=1
        elif rrr and not lll:
            rx += 1
            res += 1
        elif not rrr and lll:
            lx += 1
            res += 1
        else:
            rx += 1
            lx += 1
    return res

pc = precheck()
if pc == 1:
    print 0
elif not pc:
    print -1
else:
    print calc(pc)

Solution (Python):
import sys
import re
import math

S = input()
Sd = re.sub('x', '', S)
Sd_rev = Sd[::-1]

if Sd != Sd_rev:
    print(-1)
    sys.exit()

x_n = re.split('[abcdefghijklmnopqrstuvwyz]', S)
res = 0
for i in range(0, len(x_n)):
    i_rev = len(x_n) - i - 1
    res += int(math.fabs(len(x_n[i]) - len(x_n[i_rev])))
print(res // 2)

Solution (Python):


s=input()
p=''.join(s.split('x'));
if p==p[::-1]:
    a=[0]
    for i in range(len(s)):
        if s[i]=='x':
            a[-1]+=1
        else:
            a.append(0)

    for i in range(len(a)//2):
        a[i]=abs(a[i]-a[-i-1])
    print(sum(a[:len(a)//2]))
else:
    print(-1)

Solution (Python):
import functools


def check(s):
    result = 0
    sss = s

    while True:
        if len(sss) <= 1:
            break
        head = sss[0]
        tail = sss[-1]
        if head == tail:
            sss = sss[1:-1]
            continue
        if head == "x":
            result += 1
            sss = sss[1:]
            continue
        if tail == "x":
            result += 1
            sss = sss[:-1]
            continue
        return -1
    return result

def main():
    n = check(input())
    print(n)

    
    
    
    
    
    


if __name__ == "__main__":
    main()

Solution (Python):

s=list(input())


l=0
r=len(s)-1

count=0
while True:
    
    if s[l]!=s[r]:
        if s[l]=="x":
            
            l+=1
        elif s[r]=="x":
            
            r-=1
            
        else:
            res=-1
            break

        


        count+=1
    else:
        r-=1
        l+=1

    if l>=r:
        res=count
        break


print(res)

Solution (Python):

s = input()

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

print(cnt)