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: Runtime Error

Solution code:
def kaibun(S):
    frag = 1
    start = 0
    last = len(S)-1
    if last == 0:
        return frag
    while(abs(start - last) >= 1):
        if S[start] != S[last]:
            frag = 0
            break
        start += 1
        last -= 1
    return frag

S = input()
s = ""
position = []
for i in range(len(S)):
    if S[i] != "x":
        s += S[i]
        position.append(i)

if kaibun(s) == 0:
    print(-1)
else:
    mid = position[len(s)//2]
    count = 0
    start = 0
    last = len(S)-1
    while(start != mid or last != mid):
        if S[start] != S[last]:
            if S[start] == "x":
                start += 1
                count += 1
            else:
                last -= 1
                count += 1
        else:
            start += 1
            last -= 1
    print(count)

Submission status: Runtime Error

Solution code:
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 (palindrome(t) == False):
    print(-1)
    return

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

  if odd == 1:  # ax[b]a

    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: # a[x]a

    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

  st = ''
  for ch in s2:
    st = ch + st

  # print(s1, st)
  ans = 0
  i = 0
  j = 0
  while (i < len(s1) and j < len(st)):
    c1 = s1[i]
    c2 = st[j]
    if c1 == c2:
      i += 1
      j += 1
      continue

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

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






if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
inputs = input()

a = inputs

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

Submission status: Runtime Error

Solution code:
import math as mt
import numpy as np

string = input()
use = string.split('x')
word =''
for x in use:
    word += x
i = 0
n = len(word) - 1
while i < n:
    if word[i] != word[n]:
        print(-1)
        exit()
    i += 1
    n -= 1
if len(word) % 2 == 0:
    fh = []
    i = 0
    mid = int(len(word)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i == mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    if len(lh) != len(fh):
        print(12222)
        exit()
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

else:
    fh = []
    i = 0
    mid = int((len(word) - 1)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i > mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    if len(lh) != len(fh):
        print(12222)
        exit()
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

Submission status: Runtime Error

Solution code:
import math as mt
import numpy as np

string = input()
use = string.split('x')
word =''
for x in use:
    word += x
i = 0
n = len(word) - 1
while i < n:
    if word[i] != word[n]:
        print(-1)
        exit()
    i += 1
    n -= 1
if len(word) % 2 == 0:
    fh = []
    i = 0
    mid = int(len(word)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i == mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(0)
    # print(np.sum(np.abs(np.array(fh)-np.array(lh))))

else:
    fh = []
    i = 0
    mid = int((len(word) - 1)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i > mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(0)
    #print(np.sum(np.abs(np.array(fh)-np.array(lh))))

Submission status: Runtime Error

Solution code:
S = input()
st = 0
la = len(S) - 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':
    result += 1
    la += 1
  elif S[st] == 'x' and S[la] != 'x':
    result += 1
    st += 1
  else
    result = -1
    break

 print(result)

Submission status: Runtime Error

Solution code:
import math as mt
import numpy as np

string = input()
use = string.split('x')
word =''
for x in use:
    word += x
i = 0
n = len(word) - 1
while i < n:
    if word[i] != word[n]:
        print(-1)
        exit()
    i += 1
    n -= 1
for x in 1000000000:
    y = 1+1
if len(word) % 2 == 0:
    fh = []
    i = 0
    mid = int(len(word)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i == mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

else:
    fh = []
    i = 0
    mid = int((len(word) - 1)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i > mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1

    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

Submission status: Runtime Error

Solution code:
import math as mt
import numpy as np

string = input()
use = string.split('x')
word =''
for x in use:
    word += x
i = 0
n = len(word) - 1
while i < n:
    if word[i] != word[n]:
        print(-1)
        exit()
    i += 1
    n -= 1

if len(word) % 2 == 0:
    fh = []
    i = 0
    mid = int(len(word)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i == mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

else:
    fh = []
    i = 0
    mid = int((len(word) - 1)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i > mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    if len(fh) != len(lh):
        for x in 1000000000:
            y = 1+1
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
import math as mt
import numpy as np

string = input()
use = string.split('x')
word =''
for x in use:
    word += x
i = 0
n = len(word) - 1
while i < n:
    if word[i] != word[n]:
        print(-1)
        exit()
    i += 1
    n -= 1

if len(word) % 2 == 0:
    fh = []
    i = 0
    mid = int(len(word)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i == mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

else:
    fh = []
    i = 0
    mid = int((len(word) - 1)/2)
    count = 0
    n = len(string) - 1
    for x in string:
        if x == word[i]:
            i += 1
            fh.append(count)
            count = 0
            if i > mid:
                break
        else:
            count += 1
    i = len(word) - 1
    lh = []
    count = 0
    while i != mid - 1:
        if string[n] == word[i]:
            i -= 1
            lh.append(count)
            count = 0
        else:
            count += 1
        n -= 1
    print(np.sum(np.abs(np.array(fh)-np.array(lh))))

Submission status: Runtime Error

Solution code:
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()

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)

Submission status: Runtime Error

Solution code:
import functools

def check(s):
    if len(s)==0 or len(s)==1:
        return 0
    head = s[0]
    tail = s[-1]
    if head ==tail:
        return check(s[1:-1])
    if head =="x":
        return 1+check(s[1:])
    if tail =="x":
        return 1+check(s[:-1])
    return -1

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

    # total = functools.reduce(lambda a, b: a * (2 if b % 2 == 1 else 1),A)
    # print(pow(3 ,len(A)-1)*total)
    # # print(123)
    # b = [int(a) for a in input().split(" ")]
    # print(b)
    # [input() for a in range(3)]


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

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

pl = 0
pr = len(S)

res = 0

while pl < pr:
    cnt = 0

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

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

    res += abs(cnt)

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

    pl += 1
    pr -= 1

print(res)

Submission status: Runtime Error

Solution code:
def main():
    s = list(input())
    wox = [i for i in s if i != "x"]
    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())