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: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


s = S()
no_x = ''.join(s.split('x'))
if no_x[::-1] != no_x:
    print(-1)
else:
    L = []
    cnt = 0
    if not no_x:
        print(-1)
        exit()
    for i in s:
        if i == 'x':
            cnt += 1
        else:
            L += [cnt]
            cnt = 0
    L += [cnt]
    ans = 0 
    for i in range(len(no_x) + 1):
        ans += max(L[i], L[len(no_x) - i])
    print(ans // 2)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


s = S()
no_x = ''.join(s.split('x'))
if no_x[::-1] != no_x:
    print(-1)
else:
    L = []
    cnt = 0
    for i in s:
        if i == 'x':
            cnt += 1
        else:
            L += [cnt]
            cnt = 0
    L += [cnt]
    ans = 0
    for i in range(len(no_x) + 1):
        ans += max(L[i], L[len(no_x) - i])
    print(ans // 2)

Submission status: Wrong Answer

Solution code:
s = list(input())
s1=s[::-1]
ans = 0
cnt = 0
cnt1 = 0
while cnt<len(s) and cnt1<len(s) and cnt!=len(s)-1-cnt1:
  if s[cnt]!=s1[cnt1]:
    if s[cnt]=="x" and s1[cnt1]!="x":
      cnt+=1
      ans+=1
    elif s1[cnt1]=="x" and s[cnt]!="x":
      cnt1+=1
      ans+=1
    elif s1[cnt1]=="x" and s[cnt]=="x":
      cnt+=1
      cnt1+=1
    elif s[cnt]!="x" and s1[cnt1]!="x":
      print(-1)
      exit()
  elif s[cnt]==s1[cnt1]:
    cnt+=1
    cnt1+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
s1=s[::-1]
ans = 0
cnt = 0
cnt1 = 0
while cnt<len(s) and cnt1<len(s) and cnt!=len(s)-1-cnt1:
  #print(s[cnt],s1[cnt1])
  if s[cnt]!=s1[cnt1]:
    if s[cnt]=="x" and s1[cnt1]!="x":
      cnt+=1
      ans+=1
    if s1[cnt1]=="x" and s[cnt]!="x":
      cnt1+=1
      ans+=1
    if s1[cnt1]=="x" and s[cnt]=="x":
      cnt+=1
      cnt1+=1
    if s[cnt]!="x" and s1[cnt1]!="x":
      print(-1)
      exit()
  elif s[cnt]==s1[cnt1]:
    cnt+=1
    cnt1+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
ans = []
res=0
if len(le)%2==1:
  midlle=le[len(le)//2]
  for i in lex:
    ans.append(abs(i-midlle))
  ans.sort()
  ans1 = 1
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)
else:
  midlle=(le[len(le)//2]+le[len(le)//2-1])/2
  #print(midlle)
  for i in lex:
    ans.append(abs(i-midlle))
  #print(ans)
  ans1 = 1
  for i in range(len(ans)-1):
    if i==0:
      continue
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
ans = []
res=0
if len(le)%2==1:
  midlle=le[len(le)//2]
  for i in lex:
    ans.append(abs(i-midlle))
  ans.sort()
  ans1 = 1
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)
else:
  midlle=(le[len(le)//2]+le[len(le)//2-1])//2
  for i in lex:
    ans.append(abs(i-midlle))
  ans1 = 1
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
ans = []
res=0
if len(le)%2==1:
  midlle=le[len(le)//2]
  for i in lex:
    ans.append(abs(i-midlle))
  ans.sort()
  ans1 = 1
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)
else:
  midlle=(le[len(le)//2]+le[len(le)//2-1])//2
  for i in lex:
    ans.append(abs(i-midlle))
  ans1 = 0
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
ans = []
res=0
if len(le)%2==1:
  midlle=le[len(le)//2]
  for i in lex:
    ans.append(abs(i-midlle))
  ans.sort()
  ans1 = 0
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)
else:
  midlle=(le[len(le)//2]+le[len(le)//2-1])//2
  for i in lex:
    ans.append(abs(i-midlle))
  ans1 = 0
  for i in range(len(ans)-1):
    ans1+=1
    if ans[i]==ans[i+1]:
      ans1-=1
  print(ans1)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
ans = []
res=0
if len(le)%2==1:
  midlle=le[len(le)//2]
  for i in lex:
    if not i-midlle in ans:
      res+=1
      ans.append(abs(i-midlle))
    else:
      res-=1
  print(res)
else:
  midlle=(le[len(le)//2]+le[len(le)//2-1])//2
  for i in lex:
    if not i-midlle in ans:
      if i-midlle==0:
        continue
      res+=1
      ans.append(abs(i-midlle))
    else:
      res-=1
  print(res)

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
if len(le)%2==1:
  midlle=le[len(le)//2]
else:
  midlle=le[len(le)//2]-0.5
ans =[]
for i in lex:
  if abs(i-midlle)==0.5:
    continue
  ans.append(abs(i-midlle))
ans = list(set(ans))
print(len(ans))

Submission status: Wrong Answer

Solution code:
s = input()
s2= list(s)
if s2==s2[::-1]:
  print(0)
  exit()
s1 = []
le = []
lex = []
cnt= 0
for i in list(s):
  if i!="x":
    le.append(cnt)
    s1.append(i)
  else:
    lex.append(cnt)
  cnt+=1
if s1!=s1[::-1]:
  print(-1)
  exit()
midlle=le[len(le)//2]
ans =[]
for i in lex:
  ans.append(abs(i-midlle))
ans = list(set(ans))
print(len(ans))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(s: str):
    n = len(s)
    # def f(K: int):
        
        
    def binary_search(left: int, right: int):
        while right-left > 1:
            mid = (left+right)//2
            b = mid+1
            a = mid
            if n%2 == 0:
                a += 1
            sb = ''.join([i for i in s[:b] if i != 'x'])
            sa = ''.join([i for i in s[a:] if i != 'x'])
            # print(sb, sa)
            if sb == sa[::-1]:
                return mid
            else:
                if len(sb) < len(sa):
                    left = mid
                else:
                    right = mid
        return left

    from collections import Counter
    c = Counter([i for i in list(s) if i != 'x'])
    x = [v%2 == 0 for k,v in c.items()].count(False)
    y = [v%2 == 0 for k,v in c.items()].count(True)
    # print([v%2 != 0 for k,v in c.items()])
    if not(x == 0 or (x == 1 and y == len(c.keys())-1)):
        print(-1)
        exit()

    from math import ceil, floor

    # print(ceil(n/2))
    p = binary_search(0, n-1)
    # print(p)
    # print(s[:p], s[p:])
    # recovery
    print(max(p, n-1-p))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(s: str):
    n = len(s)
    # def f(K: int):
        
        
    def binary_search(left: int, right: int):
        while right-left > 1:
            mid = (left+right)//2
            b = mid+1
            a = mid
            if n%2 == 0:
                a += 1
            sb = ''.join(sorted([i for i in s[:b] if i != 'x']))
            sa = ''.join(sorted([i for i in s[a:] if i != 'x']))
            # print(sb, sa)
            if sb == sa:
                return mid
            else:
                if len(sb) < len(sa):
                    left = mid
                else:
                    right = mid
        return left

    from collections import Counter
    c = Counter([i for i in list(s) if i != 'x'])
    x = [v%2 == 0 for k,v in c.items()].count(False)
    y = [v%2 == 0 for k,v in c.items()].count(True)
    # print([v%2 != 0 for k,v in c.items()])
    if not(x == 0 or (x == 1 and y == len(c.keys())-1)):
        print(-1)
        exit()

    from math import ceil, floor

    # print(ceil(n/2))
    p = binary_search(0, n-1)
    # print(p)
    # print(s[:p], s[p:])
    print(n-1-p)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    N = len(S)
    T = S.replace("x", "")
    M = len(T)
    # print(T[:M//2], T[(M+1)//2:])
    if T[:M//2] != T[(M+1)//2:][::-1]:
        return print(-1)
    idx_l = 0
    cnt = 0
    idx_r = 0
    for i in range(N):
        if S[i] == "x":
            continue
        cnt += 1
        if M % 2 == 1 and cnt == (M+1)//2:
            idx_l = i
            idx_r = i
            break
        if cnt == M//2:
            idx_l = i
        elif cnt > (M+1)//2:
            idx_r = i
            break
    from collections import defaultdict
    dic = defaultdict(int)
    for d, i in enumerate(range(idx_l, -1, -1)):
        if S[i] == "x":
            dic[d] += 1
    for d, i in enumerate(range(idx_r, N)):
        if S[i] == "x":
            dic[d] += 1
    print(sum(d for d in dic.values() if d == 1))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()

t = []
for si in s:
    if si != "x":
        t.append(si)

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

l = (-(-len(t)//2))
ans = [0]*l
k = 0
for i, v in enumerate(t):
    for j in range(k, len(s)):
        if s[j] == v:
            k = j + 1
            break
        else:
            ans[i%l] += 1

k = 0
for i, v in enumerate(t):
    for j in range(k, len(s))[::-1]:
        if s[j] == v:
            k = j + 1
            break
        else:
            ans[i%l] -= 1
            
ans = list(map(abs, ans))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
s = input()

t = []
for si in s:
    if si != "x":
        t.append(si)

if t != t[::-1]:
    print(-1)

l = (-(-len(t)//2))
ans = [0]*l
k = 0
for i, v in enumerate(t):
    for j in range(k, len(s)):
        if s[j] == v:
            k = j + 1
            break
        else:
            ans[i%l] += 1

k = 0
for i, v in enumerate(t):
    for j in range(k, len(s))[::-1]:
        if s[j] == v:
            k = j + 1
            break
        else:
            ans[i%l] -= 1
            
ans = list(map(abs, ans))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
S=input()
T=S.replace("x","")
if all(s==t for s,t in zip(T,T[::-1])):
    ans=0
    i=0
    j=len(S)-1
    while i<j:
        pi=i
        pj=j
        while i<len(S) and S[i]=="x":
            i+=1
        while j>=0 and S[j]=="x":
            j-=1
        if i>j:
            break
        ans+=abs(i-pi-j+pj)
        i+=1
        j-=1
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  i_f=0
  i_b=len(s)-1

  ans=0
  while True:
    if i_f>=i_b:
      return ans

    print(i_f,i_b)
    if s[i_f]==s[i_b]:
      i_f+=1
      i_b-=1
    else:
      if s[i_f]=='x':
        i_f+=1
        ans+=1
      elif s[i_b]=='x':
        i_b-=1
        ans+=1
      else:
        return -1

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import sys,collections

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# a〜z -- START --
alf=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# a〜z --- END ---

def checkCircle(x):
  y=list(x)
  y.reverse()

  if x==''.join(y):
    return True
  return False

def main():
  s=S()
  if checkCircle(s):
    return 0

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

  if not checkCircle(t):
    return -1

  l1=[0]*26

  check_ind=len(t)//2
  if len(t)%2==1:
    check_ind+=1

  nanbanme=0
  alphabet=''

  for i,x in enumerate(t):
    if i+1==check_ind:
      alphabet=x
      nanbanme=l1[alf.index(x)]+1
      break
    else:
      l1[alf.index(x)]+=1

  # print(nanbanme,alphabet)

  l2=[0]*26
  ans=0
  for i,x in enumerate(s):
    l2[alf.index(x)]+=1
    if x==alphabet and l2[alf.index(x)]==nanbanme:
      if i>=len(s):
        break

      a=s[:i+1]
      b=s[i:]

      # print(a)
      # print(b)

      a=list(a)
      a.reverse()

      ia=ib=0
      len_a=len(a)
      len_b=len(b)

      while True:
        if len_a<=ia:
          if len_b<=ib:
            return ans
          else:
            return ans+len_b-ib
        else:
          if len_b<=ib:
            return ans+len_a-ia

        if a[ia]==b[ib]:
          ia+=1
          ib+=1
        else:
          if a[ia]=='x':
            ia+=1
          else:
            ib+=1
          ans+=1

  s=list(s)
  s.reverse()
  for i,x in enumerate(s):
    l2[alf.index(x)]+=1
    if x==alphabet and l2[alf.index(x)]==nanbanme:
      if i>=len(s):
        break

      a=s[:i+1]
      b=s[i:]

      a=list(a)
      a.reverse()

      ia=ib=0
      len_a=len(a)
      len_b=len(b)

      while True:
        if len_a<=ia:
          if len_b<=ib:
            return ans
          else:
            return ans+len_b-ib
        else:
          if len_b<=ib:
            return ans+len_a-ia

        if a[ia]==b[ib]:
          ia+=1
          ib+=1
        else:
          if a[ia]=='x':
            ia+=1
          else:
            ib+=1
          ans+=1

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import sys,collections

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# a〜z -- START --
alf=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# a〜z --- END ---

def checkCircle(x):
  y=list(x)
  y.reverse()

  if x==''.join(y):
    return True
  return False

def main():
  s=S()
  if checkCircle(s):
    return 0

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

  if not checkCircle(t):
    return -1

  l1=[0]*26

  check_ind=len(t)//2
  if len(t)%2==1:
    check_ind+=1

  nanbanme=0
  alphabet=''

  for i,x in enumerate(t):
    if i+1==check_ind:
      alphabet=x
      nanbanme=l1[alf.index(x)]+1
      break
    else:
      l1[alf.index(x)]+=1

  # print(nanbanme,alphabet)

  l2=[0]*26
  ans=0
  for i,x in enumerate(s):
    l2[alf.index(x)]+=1
    if x==alphabet and l2[alf.index(x)]==nanbanme:
      if i>=len(s):
        break

      a=s[:i]
      if len(t)%2==0:
        b=s[i+2:]
      else:
        b=s[i+1:]

      a=list(a)
      a.reverse()

      ia=ib=0
      len_a=len(a)
      len_b=len(b)

      while True:
        if len_a<=ia:
          if len_b<=ib:
            return ans
          else:
            return ans+len_b-ib
        else:
          if len_b<=ib:
            return ans+len_a-ia

        if a[ia]==b[ib]:
          ia+=1
          ib+=1
        else:
          if a[ia]=='x':
            ia+=1
          else:
            ib+=1
          ans+=1

  s=list(s)
  s.reverse()
  for i,x in enumerate(s):
    l2[alf.index(x)]+=1
    if x==alphabet and l2[alf.index(x)]==nanbanme:
      if i>=len(s):
        break

      a=s[:i]
      if len(t)%2==0:
        b=s[i+2:]
      else:
        b=s[i+1:]

      a=list(a)
      a.reverse()

      ia=ib=0
      len_a=len(a)
      len_b=len(b)

      while True:
        if len_a<=ia:
          if len_b<=ib:
            return ans
          else:
            return ans+len_b-ib
        else:
          if len_b<=ib:
            return ans+len_a-ia

        if a[ia]==b[ib]:
          ia+=1
          ib+=1
        else:
          if a[ia]=='x':
            ia+=1
          else:
            ib+=1
          ans+=1

# main()
print(main())

Submission status: Wrong Answer

Solution code:
s = input()
t = s.replace('x', '')
if all(t[i] == t[len(t) - i - 1] for i in range(len(t) // 2)):
    print(len(s) - len(t))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
s=input()
s_exit_x=''
ans = 0
for n,i in enumerate(s):
    if i !='x':
        s_exit_x += i
if s_exit_x != s_exit_x[::-1]:
    print(-1)
else:
    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
        else:
            right -=1
            ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

N = len(S)
tcnt =[0] * 128
for c in S:
    tcnt[ord(c)] += 1 if c != 'x' else 0
odd = 0
for i in range(128):
    if tcnt[i] % 2 == 1:
        if odd != 0:
            print(-1)
            exit()
        else:
            odd = i

cl = 0
cr = 0
cnt = [0] * 128
for i in range(N):
    cnt[ord(S[i])] += 1 if S[i] != 'x' else 0
    if cnt[ord(S[i])] > (tcnt[ord(S[i])] + 1) // 2:
        cr = i
        break
    elif S[i] != 'x':
        cl = i

if odd != 0:
    cr = cl

ans = 0
while cl >= 0 and cr < N:
    l = cl - 1
    while l >= 0 and S[l] == 'x':
        l -= 1
    lx = cl - l - 1
    r = cr + 1
    while r < N and S[r] == 'x':
        r += 1
    rx = r - cr - 1
    ans += abs(lx - rx)

    a = ord(S[l]) if l >= 0 else 0
    b = ord(S[r]) if r < N else 0
    if a != b:
        print(-1)
        exit()
    else:
        cl = l
        cr = r

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
remove_x = S.replace('x','')

count = 0
index = 0
left = 0
right = 0
ans = 0

if remove_x != remove_x[::-1]:
    print(-1)
else:
    if len(remove_x) % 2 == 0:
        center = len(remove_x) // 2
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index
        right = index + 1
    else:
        center = len(remove_x) // 2 + 1
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index - 1
        right = index + 1 
    
    s_len = len(S)
    #print(f'{left}, {right}')
    while left >= 0 and right < s_len:
        if S[left] != S[right]:
            if S[left] == 'x':
                ans += 1
                left -= 1
            elif S[right] == 'x':
                ans += 1
                right += 1
        else:
            left -= 1
            right += 1

    if right != s_len:
        ans += s_len - right
    else:
        ans += left + 1

    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
remove_x = S.replace('x','')

count = 0
index = 0
left = 0
right = 0
ans = 0

if remove_x != remove_x[::-1]:
    print(-1)
else:
    if len(remove_x) % 2 == 0:
        center = len(remove_x) // 2
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index
        right = index + 1
    else:
        center = len(remove_x) // 2 + 1
        for i, s in enumerate(S):
            if s != 'x':
                count += 1
            if count == center:
                index = i
                break
        left = index - 1
        right = index + 1 
    
        s_len = len(S)
        #print(f'{left}, {right}')
        while left >= 0 and right < s_len:
            if S[left] != S[right]:
                if S[left] == 'x':
                    ans += 1
                    left -= 1
                elif S[right] == 'x':
                    ans += 1
                    right += 1
            else:
                left -= 1
                right += 1

        if right != s_len:
            ans += s_len - right
        else:
            ans += left + 1

        print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
L = list(S)
left_idx = 0
right_idx = N - 1
ret = 0
while left_idx <= N // 2 and right_idx >= N // 2:
    l = S[left_idx]
    r = S[right_idx]
    if l == r:
        left_idx += 1
        right_idx -= 1
    elif l == "x":
        ret += 1
        left_idx += 1
    elif r == "x":
        ret += 1
        right_idx -= 1
    else:
        ret = -1
        break

print(ret)

Submission status: Wrong Answer

Solution code:
s = input()
ns = s.replace("x","")
l = len(ns)

if ns[:l//2] == ns[(l+1)//2:]:
    ans = 0
    left = 0
    right = len(s) - 1
    while left <= right:
        if s[left] != s[right]:
            if s[left] == "x":
                right += 1
            else:
                left -= 1
            ans += 1
        left += 1
        right -= 1
else:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
s = readline().rstrip().decode('utf-8')

t = ""
for i in s:
    if not i == "x":
        t += i

if t[::] != t[::-1]:
    print(-1)

else:
    ans = 0
    left,right = 0,-1
    while True:
        if left > len(s)//2 or right < -len(s)//2 - 1:
            break
        if s[left] == "x" and s[right] != "x":
            ans += 1
            left += 1
        elif s[left] != "x" and s[right] == "x":
            ans += 1
            right -= 1
        else:
            left += 1
            right -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
acc = s.replace('x', "")

if s == s[::-1]:
    ans = 0
elif acc == acc[::-1]:
    ans = s.count('x')
else:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
acc = s.replace('x', "")

if acc == acc[::-1]:
    ans = s.count('x')
else:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()

l = s.replace("x", "")
ll = len(l) // 2
lr = len(l) - ll
if l[:ll] != l[lr:][::-1]:
    print(-1)
    sys.exit()

xcnt = [0] * lr
c = 0
cnt = 0
i = 0
while c != lr:
    if s[i] != "x":
        xcnt[c] = cnt
        c += 1
        cnt = 0
    else:
        cnt += 1
    i += 1
    
c = 0
cnt = 0
i = len(s) - 1
while c != lr:
    if s[i] != "x":
        xcnt[c] -= cnt
        xcnt[c] = abs(xcnt[c])
        c += 1
    else:
        cnt += 1
    i -= 1

print(sum(xcnt))

Submission status: Wrong Answer

Solution code:
import sys
s = input()

l = s.replace("x", "")
ll = len(l) // 2
lr = len(l) - ll
if l[:ll] != l[lr:]:
    print(-1)
    sys.exit()

xcnt = [0] * lr
c = 0
cnt = 0
i = 0
while c != lr:
    if s[i] != "x":
        xcnt[c] = cnt
        c += 1
        cnt = 0
    else:
        cnt += 1
    i += 1
    
c = 0
cnt = 0
i = len(s) - 1
while c != lr:
    if s[i] != "x":
        xcnt[c] -= cnt
        xcnt[c] = abs(xcnt[c])
        c += 1
    else:
        cnt += 1
    i -= 1

print(sum(xcnt))

Submission status: Wrong Answer

Solution code:
S = input()

from collections import deque

s_deque = deque(S)

count = 0

while len(s_deque) > 1:
    # print(s_deque)
    if s_deque[0] == s_deque[-1]:
        s_deque.popleft()
        s_deque.pop()
    elif s_deque[0] == "x":
        count += 1
        s_deque.append("x")
    elif s_deque[-1] == "x":
        count += 1
        s_deque.appendleft("x")
    if "x" not in s_deque:
        break

if len(s_deque) == 1:
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
q = deque(S)
print(q)
c = 0
while len(q):
  l = q[0]
  r = q[len(q)-1]
  if l == r:
    if len(q) != 1:
      q.pop()
    q.popleft()
  elif l == 'x':
    q.append('x')
    c += 1
  elif r == 'x':
    q.appendleft('x')
    c += 1
  else:
    print(-1)
    exit()
print(c)

Submission status: Wrong Answer

Solution code:
s = input()
l, r = 0, 0
for i in range(len(s)):
  if s[i-l] != "x" and s[-i-1+r] != "x" and s[i-l] != s[-i-1+r]:
    print(-1)
    exit()
  if i-l == len(s)-i-1+r:
    break
  if s[i-l] == s[-i-1+r]:
    continue
  if s[i-l] == "x" and s[-i-1+r] != "x":
    r += 1
  elif s[i-l] != "x" and s[-i-1+r] == "x":
    l += 1
print(l+r)

Submission status: Wrong Answer

Solution code:
s = input()
l, r = 0, 0
for i in range(len(s)):
  if s[i-l] != "x" and s[-i-1+r] != "x" and s[i-l] != s[-i-1+r]:
    print(-1)
    exit()
  if i-l == len(s)-i-1+r:
    break
  if s[i-l] == "x" and s[-i-1+r] != "x":
    r += 1
  elif s[i-l] != "x" and s[-i-1+r] == "x":
    l += 1
print(l+r)

Submission status: Wrong Answer

Solution code:
s = input()
l, r = 0, 0
for i in range(len(s)):
  if i-l == len(s)-i-1+r:
    break
  if s[i-l] == "x" and s[-i-1+r] != "x":
    r += 1
  elif s[i-l] != "x" and s[-i-1+r] == "x":
    l += 1
  elif s[i-l] != "x" and s[-i-1+r] != "x" and s[i-l] != s[-i-1+r]:
    print(-1)
    exit()
print(l+r)

Submission status: Wrong Answer

Solution code:
s = input()
l, r = 0, 0
if s[0] != s[-1] and s[0] != "x" and s[-1] != "x":
  print(-1)
  exit()
for i in range(len(s)):
  if i-l == len(s)-i-1+r:
    break
  if s[i-l] == "x" and s[-i-1+r] != "x":
    r += 1
  elif s[i-l] != "x" and s[-i-1+r] == "x":
    l += 1
print(l+r)

Submission status: Wrong Answer

Solution code:
s = input()
l, r = 0, 0
if s[0] != s[-1] and s[0] != "x" and s[-1] != "x":
  print(-1)
  exit()
for i in range(len(s)):
  if len(s)%2==1 and i == len(s)//2+1:
    break
  if s[i-l] == "x" and s[-i-1+r] != "x":
    r += 1
  elif s[i-l] != "x" and s[-i-1+r] == "x":
    l += 1
print(l+r)