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 ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))
def plis(a): return " ".join(list(map(str, a)))


s = input()
ans = 0
s_nox = s.replace('x', '')
if(s_nox != s_nox[::-1]): ans = -1
else:
    need = (len(s_nox)+1) // 2
    s = list(s)
    s_rev = s[::-1]
    si = 0
    srevi = 0
    si_tmp = -1
    srevi_tmp = -1
    for i in range(need):
        si_tmp = 0
        srevi_tmp = 0
        while(s[si] == 'x'):
            si += 1
            si_tmp += 1
        while(s_rev[srevi] == 'x'):
            srevi += 1
            srevi_tmp += 1
        ans += abs(si_tmp - srevi_tmp)
        si += 1
        srevi += 1
print(ans)

Solution (Python):
s = input()

s_wo_x = s.replace("x", "")
for i in range(len(s_wo_x)):
    if s_wo_x[i] != s_wo_x[len(s_wo_x)-i-1]:
        print("-1")
        break
else:
    i = 0
    j = len(s)-1
    ans = 0
    while(i < j):
        if s[i] == "x" and s[j] != "x":
            i += 1
            ans += 1
        elif s[i] != "x" and s[j] == "x":
            j -= 1
            ans += 1
        elif s[i] == s[j]:
            i += 1
            j -= 1
            
    print(ans)

Solution (Python):



s = raw_input()








status = 0

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



print status

Solution (Python):
import sys
s = raw_input()

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

if(ss == ss[::-1]):
    pass

else:
    print "-1"
    sys.exit()


count = 0

right = 0
left = len(s)-1

for i in range(len(s)*2):

    if ( s[right] != s[left]):
        count += 1
        if(s[right]!="x"):
            right -= 1

        else:
            left += 1
            
    right += 1
    left -= 1
    if(right>=left):
        print count
        sys.exit()

Solution (Python):
def ch(s):
	a = True
	for k, x in enumerate(s):
		if x != s[-k-1]:
			a = False
			break
	return a

s = input()
ns = s.replace("x", "")
if ch(ns):
	a = [0]*(len(ns)+1)
	k = 0
	b = True
	for i in s:
		if b:
			if i == 'x':
				a[k] += 1
			else:
				b = False
				k += 1
		else:
			if i == 'x':
				b = True
				a[k] += 1
			else:
				k += 1
	k = 0
	for i in range(len(a)//2):
		if a[i] != a[-i-1]:
			k += abs(a[i] - a[-i-1])
	print(k)
else:
	print(-1)

Solution (Python):
S = input()
N = len(S)
non_x = []
s_notx = []
for i, s in enumerate(S):
    if s != 'x':
        non_x.append(i)
        s_notx.append(s)
s_notx = ''.join(s_notx)
if s_notx != s_notx[::-1]:
    print(-1)
    quit()
if not non_x:
    print(0)
    quit()
ans = 0
L = len(non_x)
if L % 2 == 0:
    left, right = L // 2 - 1, L // 2
else:
    left = right = L // 2
left, right = left - 1, right + 1
for i in range(left + 1):
    l, r = left - i, right + i
    ans += abs((non_x[l + 1] - non_x[l]) - (non_x[r] - non_x[r - 1]))
ans += abs(non_x[0] - (N - non_x[-1] - 1))
print(ans)

Solution (Python):
def solve(s) :
    f = 0
    b = len(s)-1
    ans = 0
    while True :
        if abs(f-b) <= 1 and s[f] == s[b]:
            return ans
        elif s[f] != s[b] :
            if s[f] == 'x' :
                f += 1
                ans += 1
            elif s[b] == 'x' :
                b -= 1
                ans += 1
            else :
                return -1
        else :
            f += 1
            b -= 1

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

main()

Solution (Python):
S = input()

if len(set(S)) == 1:
    print(0)

else:
    top = 0
    bottom = len(S) - 1
    count = 0
    while top < bottom:
        if S[top] == S[bottom]:
            top += 1
            bottom -= 1
        else:
            if S[top] == "x":
                top += 1
                count += 1
            elif S[bottom] == "x":
                bottom -= 1
                count += 1
            else:
                print(-1)
                break
    else:
        print(count)

Solution (Python):

s = input()

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

if a == a[::-1]:
	pass
else:
	print("-1")
	exit()

a = s
count1 = 0
count2 = len(a)-1
count = 0
while(count1 < count2):
	if a[count1] == a[count2]:
		count1 += 1
		count2 -= 1
	if a[count1] == "x" and a[count2] != "x":
		count += 1
		count1 += 1
	if a[count1] != "x" and a[count2] == "x":
		count += 1
		count2 -= 1


print(count)

Solution (Python):
import sys

S = sys.stdin.readline().strip()

N = len(S)

pl = 0
pr = N

res = 0

while pl < pr:
    cnt = 0

    while pl < N and S[pl] == 'x':
        cnt += 1
        pl += 1

    while 0 < pr and S[pr - 1] == 'x':
        cnt -= 1
        pr -= 1

    res += abs(cnt)

    if 0 < pr and pl < N and S[pl] != S[pr - 1]:
        print(-1)
        sys.exit()

    pl += 1
    pr -= 1

print(res)

Solution (Python):

s = list(input())

x_list = []
x_cnt = 0
nonx_list = []

for i in range(len(s)):
    if s[i] == 'x':
        x_cnt += 1
    else:
        nonx_list.append(s[i])
        x_list.append(x_cnt)
        x_cnt = 0

x_list.append(x_cnt)

if "".join(nonx_list) == "".join(reversed(nonx_list)):    
    res = 0
    for i in range(len(x_list) // 2):
        res += max(x_list[i], x_list[len(x_list)-i-1]) - min(x_list[i], x_list[len(x_list)-i-1])
    print(res)
else:
    print(-1)

Solution (Python):


import math
import fractions
import heapq
import collections
import re
import array
import bisect

from collections import Counter, defaultdict


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]


def palindrome(s):
    for i in range(len(s)):
        if not s[i] == s[-i-1]:
            return False
    return True


def main():
    s = input().strip()
    i = 0
    sdx = ""
    x = defaultdict(int)
    for c in s:
        if c == "x":
            x[i] += 1
        else:
            sdx += c
            i += 1
    if not palindrome(sdx):
        return -1

    cnt = 0
    k = list(x.keys())
    for d in k:
        cnt += max(0, x[d] - x[i-d])
    return cnt


if __name__ == "__main__":
    print(main())

Solution (Python):
def solve(s):
    r = s[::-1]
    l = len(s)
    sn, rn = 0, 0
    ins = 0

    while True:
        if sn + rn > l:
            return ins
        sc = s[sn]
        rc = r[rn]
        if sc == rc:
            sn += 1
            rn += 1
            continue
        if sc == 'x':
            sn += 1
            ins += 1
            continue
        if rc == 'x':
            rn += 1
            ins += 1
            continue
        return -1


print(solve(input()))

Solution (Python):
s = list(input())

c = []
a = [0]
for i in range(len(s)):
    if s[i] != 'x':
        a.append(i)
        c.append(s[i])
a.append(len(s) - 1)
ans = 0
for i in range(len(c)):
    if c[i] != c[-i - 1]:
        ans = -1
        break
if ans != -1:
    x = []
    for i in range(len(a) - 1):
        x.append(a[i + 1] - a[i])
    for i in range(len(x) // 2):
        ans += abs(x[i] - x[-i - 1])
print(ans)

Solution (Python):
def solve():
    s = raw_input()
    n = len(s)
    ans = 0
    i, j = 0, n - 1
    while i < j:
        if s[i] != s[j]:
            if s[i] == 'x':
                ans += 1
                i += 1
            elif s[j] == 'x':
                ans += 1
                j -= 1
            else:
                return -1
        else:
            i += 1
            j -= 1
    return ans

print solve()

Solution (Python):
S = input()
l = 0
r = 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:
        cnt = -1
        break
print(cnt)

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

Solution (Python):




S = list(input())


i = 0
j = len(S) - 1

insert = 0

while True:
	if i >= j:
		break

	elif S[i] == S[j]:
		i += 1
		j -= 1

	elif S[i] == 'x':
		i += 1
		insert += 1

	elif S[j] == 'x':
		j -= 1
		insert += 1

	else:
		insert = -1
		break

print(insert)

Solution (Python):
from collections import deque

s = deque(input())

cnt = 0
while len(s) > 1:
  a,b = s[0],s[-1]
  if a == b:
    s.pop()
    s.popleft()
  elif a == 'x':
    cnt += 1
    s.popleft()
  elif b == 'x':
    cnt += 1
    s.pop()
  else:
    break
else:
  print(cnt)
  exit()

print(-1)

Solution (Python):
def main():
    s = list(input())
    wox = [i for i in s if i != "x"]
    if len(wox) == 0: return 0
    wowr = list(reversed(wox))
    for i in range(len(wox)//2+1):
        if wox[i] != wowr[i]:
            return -1
    sr = list(reversed(s))
    si = ri = 0
    ret = 0
    while si < len(s) and ri < len(sr):
        if s[si] == sr[ri]:
            si += 1
            ri += 1
        elif s[si] == "x":
            si += 1
            ret += 1
        else:
            ri += 1
            ret += 1
    return (ret + len(s) - min(si,ri))//2
print(main())

Solution (Python):
s = raw_input()
l = 0
r = len(s)-1
c = 0
while l<r:
  if s[l]==s[r]:
    l += 1
    r -= 1
  elif s[l]=='x' and s[r]!='x':
    l += 1
    c += 1
  elif s[l]!='x' and s[r]=='x':
    r -= 1
    c += 1
  elif s[l]!='x' and s[r]!='x':
    print -1
    exit(0)
print c

Solution (Python):
from collections import deque
def main():
    s = deque(raw_input().strip())
    ans = 0
    while len(s) > 1:
        st = s[0]
        nd = s[-1]
        if st == nd:
            s.pop()
            s.popleft()
        elif st == 'x':
            s.popleft()
            ans += 1
        elif nd == 'x':
            s.pop()
            ans += 1
        else:
            print -1
            return
    print ans
main()

Solution (Python):
S = raw_input()

def solve(S):
    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:
            return -1
    return ans
            

print solve(S)

Solution (Python):
S = input()

comp = []
other = []
cnt = 0
for c in S:
    if c == 'x':
        cnt += 1
    else:
        other.append(c)
        comp.append(cnt)
        cnt = 0
comp.append(cnt)


L = len(other)

dame = False
for i in range(L // 2):
    if other[i] != other[L - i - 1]:
        dame = True

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

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string

S = input()
SwoX = S.replace('x', '')

if SwoX != SwoX[::-1]:
    print(-1)
else:
    i = 0
    j = len(S)-1
    ans = 0
    while(i < j):
        if (S[i] == S[j] == 'x') or (S[i] != 'x' and S[j] != 'x'):
            i += 1
            j -= 1
        else:
            ans += 1 
            if S[i] == 'x':
                i += 1
            else:
                j -= 1
    print(ans)

Solution (Python):
s= 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":
        left+=1
        ans +=1
    elif s[right]=="x":
        right-=1
        ans +=1
    else:
        print(-1)
        exit(0)

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    s = S()
    l = len(s)
    i = 0
    j = l-1
    r = 0
    while i<j:
        if s[i] == s[j]:
            i+=1
            j-=1
        elif s[i] == 'x':
            r += 1
            i+=1
        elif s[j] == 'x':
            r += 1
            j -= 1
        else:
            return -1
    return r


print(main())

Solution (Python):
s = input()
n = len(s)

l = 0; r = n-1

ans = 0
while l < 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)

Solution (Python):

s = input()
l = 0
r = len(s) - 1
cnt = 0
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:
        cnt = -1
        break
print(cnt)