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:
#WA
s = list(input())
ns = ''
cnt = 0
for i in s:
    if i != 'x':
        ns += i
    else:cnt += 1

if ns == ns[::-1]:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    ss = ""
    for i in s:
        if i != "x":
            ss += i

    if ss != ss[::-1]:
        print(-1)
    else:
        cnt = 0
        cnt_x = [0] * (len(ss) + 1)
        pos = 0
        for i in range(len(s)):
            if s[i] not in set(ss):
                cnt += 1

            if s[i] in set(ss) or i == len(s) - 1:
                cnt_x[pos] = cnt
                cnt = 0
                pos += 1

        res = max(cnt_x) * len(cnt_x) - sum(cnt_x)
        print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
s=input()
d=s.replace("x","")
rd=d[::-1]
if d!=rd:print(-1)
else:
    ans=0
    x,y=0,len(s)-1
    while x<y:
        if s[x]==s[y]:x+=1;y-=1
        if s[x]=="x":ans+=1;x+=1
        else:ans+=1;y-=1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
ans = 0
r = 0
l = len(s)-1
while r < l:
  if s[r] == s[l]:
    r += 1
    l -= 1
  elif s[r] == "x":
    ans += 1
    r += 1
  elif s[l] == "x":
    ans += 1
    l -= 1
  else:
    print(-1)
    exit()

print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

s = input()
n = len(s)
ns = []
no = []

for i in range(n):
    if s[i] != "x":
        ns.append(s[i])
        no.append(i)
        
no = [-1] + no + [n]
        
m = len(no) 
sa = []
for i in range(m-1):
    sa.append(no[i+1] - no[i] - 1)
        
if ns != ns[::-1]:
    print(-1)
    sys.exit()
    
ans = 0
if m%2 == 1:
    mm = m//2
    te = no[mm]
    ans = 0
    for i in range(mm):
        ans += max(sa[i],sa[m-2-i])
else:
    mm = m//2
    te = no[mm]
    ans = 0
    for i in range(mm-1):
        ans += max(sa[i],sa[m-2-i])
        
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

s = input()
snotx = "".join([ch for ch in s if ch != 'x'])

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

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

# 回数を合わせる
# print(s)
# print(snotx)

# xのrun-length
rl = []
i = 0
while i < len(s): 
    j = i
    while j < len(s) and s[i] == s[j]:
        j += 1
    # print(s[i], i, j)
    if s[i] == 'x':
        rl.append(j - i)
    i = j
# print(rl)

# run-lengthの左右を見て合わせる
res = 0
i = 0
while i < len(rl) // 2:
    res += max(rl[i], rl[len(rl) - i - 1]) - min(rl[i], rl[len(rl) - i - 1])
    i += 1
print(res)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    s = input()
    t = ""
    for i in s:
        if i != "x":
            t += i
    if t != t[::-1]:
        print(-1)
        return
    print(len(s)-len(t))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
"""
import collections
s=list(str(input()))

s=collections.Counter(s)
print(s)
x=s['x']
print(x)
count=0
kisuu=[]
for ss,v in s.items():
    
    if v%2!=0:
        kisuu.append(ss)
        count+=1

print(kisuu)
print(count)
if 'x' in kisuu:
    count-=1
print(count)
if count<=1:
    print()
    """

b=str(input())
x=0
x=b.count('x')
#print(a)
a=list(b.split('x'))
#print(x)
s=""
for i in range(len(a)):
    s+=a[i]
#print(s)

mannnaka=0
mannnaka2=0

if len(s)==0:
    print(0)
        
    exit()
if len(s)==1:
    #print(x)
    for i in range(len(b)):
        if b[i]==s:
            mannnaka=i
            break
    print(abs(b[0:mannnaka].count('x')-b[mannnaka:len(b)].count('x')))
    exit()
if len(s)%2==1:
    asd=s[len(s)//2+1:len(s)]
    #print(asd)
    asd=asd[::-1]
   # print(s[0:len(s)//2],asd)
    if s[0:len(s)//2]==asd:
        saa=s[len(s)//2]
        for i in range(len(b)):
            if b[i]==saa:
                mannnaka=i
                break
        print(abs(b[0:mannnaka].count('x')-b[mannnaka:len(b)].count('x')))
        
    else:
        print(-1)
else:
    asd=s[len(s)//2:len(s)]
    asd=asd[::-1]
   # print(s[0:len(s)//2],asd)

    if s[0:len(s)//2]==asd:
        saa=s[len(s)//2]
        for i in range(len(b)):
            if b[i]==saa:
                mannnaka=i
                break
        print(abs(b[0:mannnaka].count('x')-b[mannnaka+1:len(b)].count('x')))
        
        
        
        #print(x)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
"""
import collections
s=list(str(input()))

s=collections.Counter(s)
print(s)
x=s['x']
print(x)
count=0
kisuu=[]
for ss,v in s.items():
    
    if v%2!=0:
        kisuu.append(ss)
        count+=1

print(kisuu)
print(count)
if 'x' in kisuu:
    count-=1
print(count)
if count<=1:
    print()
    """

a=str(input())
x=0
x=a.count('x')
#print(a)
a=list(a.split('x'))
#print(x)
s=""
for i in range(len(a)):
    s+=a[i]
#print(s)
if len(s)==0:
    print(x)
if len(s)==1:
    print(x)
    exit()
if len(s)%2==1:
    asd=s[len(s)//2+1:len(s)]
    asd=asd[::-1]
    if s[0:len(s)//2]==asd:
        print(x)
    else:
        print(-1)
else:
    asd=s[len(s)//2:len(s)]
    asd=asd[::-1]
    if s[0:len(s)//2]==asd:
        print(x)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = [s[i] for i in range(len(s)-1,-1,-1)]
k = "".join(k)
if s == k:
    print(0)
    exit()
x_count = s.count('x')
s = s.replace('x', '')
k = [s[i] for i in range(len(s)-1,-1,-1)]
k = "".join(k)
if k == s:
    print(x_count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
"""
import collections
s=list(str(input()))

s=collections.Counter(s)
print(s)
x=s['x']
print(x)
count=0
kisuu=[]
for ss,v in s.items():
    
    if v%2!=0:
        kisuu.append(ss)
        count+=1

print(kisuu)
print(count)
if 'x' in kisuu:
    count-=1
print(count)
if count<=1:
    print()
    """

a=str(input())
x=0
x=a.count('x')
#print(a)
a=list(a.split('x'))
#print(x)
s=""
for i in range(len(a)):
    s+=a[i]
#print(s)
if len(s)==0:
    print(x)
if len(s)==1:
    print(x)
    exit()
if len(s)%2==1:
    asd=s[len(s)//2+1:len(s)]
    asd=asd[::-1]
    if s[0:len(s)//2]==s[len(s)//2+1:len(s)]:
        print(x)
    else:
        print(-1)
else:
    asd=s[len(s)//2:len(s)]
    asd=asd[::-1]
    if s[0:len(s)//2]==asd:
        print(x)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
x_count = s.count('x')
s = s.replace('x', '')
k = [s[i] for i in range(len(s)-1,-1,-1)]
k = "".join(k)
if k == s:
    print(x_count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
"""
import collections
s=list(str(input()))

s=collections.Counter(s)
print(s)
x=s['x']
print(x)
count=0
kisuu=[]
for ss,v in s.items():
    
    if v%2!=0:
        kisuu.append(ss)
        count+=1

print(kisuu)
print(count)
if 'x' in kisuu:
    count-=1
print(count)
if count<=1:
    print()
    """

a=str(input())
x=0
x=a.count('x')
#print(a)
a=list(a.split('x'))
#print(x)
s=""
for i in range(len(a)):
    s+=a[i]
#print(s)
if len(s)==0:
    print(x)
if len(s)==1:
    print(x)
    exit()
if len(s)%2==1:
    if s[0:len(s)//2]==s[len(s)//2+1:len(s)]:
        print(x)
    else:
        print(-1)
else:
    if s[0:len(s)//2]==s[len(s)//2:len(s)]:
        print(x)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

def main():
    S = input()
    l = len(S)

    T = S.replace('x', '')
    if T != T[::-1]:
        print(-1)
        exit()
    '''
    st, end = 0, l-1
    cnt = 0
    while True:
        if S[st] == S[end]:
            if st+1!=end:
                st += 1
                end -= 1
            else:
                st=end
        elif S[st] == 'x':
            st += 1
            cnt += 1
        elif S[end] == 'x':
            end -= 1
            cnt += 1
        if st==end:
            break
    print(cnt)
    '''
    zero_list = []
    cnt = 0
    before = ''
    for i in range(l):
        if S[i] == 'x':
            cnt += 1
        elif i>0 and S[i]!=S[i-1]:
            zero_list.append(cnt)
            cnt = 0
        elif i==0:
            zero_list.append(cnt)
    zero_list.append(cnt)

    lz = len(zero_list)
    ans = 0
    for i in range(lz//2):
        ans += abs(zero_list[i] - zero_list[lz-1-i])
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
from collections import defaultdict #d = defaultdict(int) d[key] += value

def main():
    S = input()
    if S == S[::-1]:
        print(0)
        exit()
    l = len(S)
    T = S.replace('x', '')
    if T != T[::-1]:
        print(-1)
        exit()
    else:
        al = T[len(T)//2]
    pos_list = []
    for i in range(l):
        if S[i] == al:
            pos_list.append(i)
    cnt = 0
    if len(pos_list)%2 == 1:
        mid = pos_list[len(pos_list)//2]
        if mid>0:
            st = mid-1
        else:
            st = 0
        if mid<l-1:
            end = mid+1
        else:
            end = l-1
    else:
        mids = pos_list[len(pos_list)//2-1 : len(pos_list)//2+1]
        if mids[0]>0:
            st = mids[0]-1
        else:
            st = 0
        if mids[1]<l-1:
            end = mids[1]+1
        else:
            end = l-1
    flag = 0
    while True:
        if st==0 and end==l-1:
            flag = 1
        if S[st] == S[end]:
            if st > 0:
                st -= 1
            if end < l-1:
                end += 1
        elif S[st] == 'x':
            if st > 0:
                st -= 1
            cnt += 1
        elif S[end] == 'x':
            if end < l-1:
                end += 1
            cnt += 1
        if flag:
            break
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s=list(input())
def kaibun(s):
    if s==list(reversed(s)):
        return True
    else:
        return False
if kaibun(s):
    print("0")
    exit(0)

nonex=[]
time=[]
count=0
for i in s:
    if i!="x":
        nonex.append(i)
        count+=1
    else:
        time.append(count)
if kaibun(nonex) == False:
    print("-1")
    exit(0)
tmp=-(-(count+1)//2)
xlist=[0 for i in range(tmp)]
for i in time:
    if i>=-(-(count+1)//2):
        xlist[(i+1)//2%tmp]-=1
    else:
        xlist[i]+=1
ans=0
for i in xlist:
    ans+=abs(i)
if len(nonex)%2==0:
    ans-=abs(xlist[-1])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    t = s[::-1]
    if s==t:
        print(0)
        return
    ans = s.count('x')
    if ans==0:
        print("-1")
        return
    print(ans)

    # s = list(input())

    # t = s[len(s)//2:]
    # t = t[::-1]
    # s = s[:len(s)//2]

    # l = 0
    # r = 0
    # ans = 0
    # for i in range(len(s)):
    #     if s[l+i]==t[r+i]:
    #         continue
    #     if s[l+i]!='x' and s[l+i]!='x':
    #         ans = 0
    #         break
    #     if s[l+i] == 'x':


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    t = s[::-1]
    if s==t:
        print(0)
        return
    ans = s.count('x')
    if ans==0:
        print("-1")
    print(ans)
    # s = list(input())

    # t = s[len(s)//2:]
    # t = t[::-1]
    # s = s[:len(s)//2]

    # l = 0
    # r = 0
    # ans = 0
    # for i in range(len(s)):
    #     if s[l+i]==t[r+i]:
    #         continue
    #     if s[l+i]!='x' and s[l+i]!='x':
    #         ans = 0
    #         break
    #     if s[l+i] == 'x':


if __name__ == "__main__":
    main()

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(10 ** 7)

s = input()
cnt_1 = 0
cnt_2 = len(s) - 1
ans = 0
end = len(s) // 2
for i in range(cnt_2 + 1):
    if s[cnt_1] == s[cnt_2]:
        cnt_1 += 1
        cnt_2 -= 1
    elif s[cnt_1] == 'x':
        cnt_1 += 1
        ans += 1
    elif s[cnt_2] == 'x':
        cnt_2 -= 1
        ans += 1
    else:
        print(-1)
        exit()
    if end == cnt_1 and end == cnt_2:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())

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

if a == a[::-1]:
    print(s.count('x'))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    d = deque(s)
    a, b = [], []
    cnt = 0
    x = y = ''
    x_skip = y_skip = 0
    while d:
        if not x_skip:
            x = d.popleft()
        if len(d) == 0:
            if x == y != 'x':
                cnt += abs(len(a)-len(b))
            break
        if 0 < len(d)and not y_skip:
            y = d.pop()

        if x == y != 'x':
            cnt += abs(len(a)-len(b))
            a, b = [], []
            x_skip = y_skip = 0
            continue

        if x == 'x':
            a.append(x)
        else:
            x_skip = 1

        if y == 'x':
            b.append('x')
        else:
            y_skip = 1
    else:
        cnt += abs(len(a)-len(b))

    print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    d = deque(s)
    a, b = [], []
    cnt = 0
    x = y = ''
    x_skip = y_skip = 0
    while d:
        if not x_skip:
            x = d.popleft()
        if len(d) == 0 and x == 'x':
            break
        if 0 < len(d)and not y_skip:
            y = d.pop()

        if x == y != 'x':
            cnt += abs(len(a)-len(b))
            a, b = [], []
            x_skip = y_skip = 0
            continue

        if x == 'x':
            a.append(x)
        else:
            x_skip = 1

        if y == 'x':
            b.append('x')
        else:
            y_skip = 1
    else:
        cnt += abs(len(a)-len(b))

    print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    d = deque(s)
    a, b = [], []
    cnt = 0
    x = y = ''
    x_skip = y_skip = 0
    while d:
        if not x_skip:
            x = d.popleft()
        if len(d) == 0:
            break
        if not y_skip:
            y = d.pop()

        if x == y != 'x':
            cnt += abs(len(a)-len(b))
            a, b = [], []
            x_skip = y_skip = 0
            continue

        if x == 'x':
            a.append(x)
        else:
            x_skip = 1

        if y == 'x':
            b.append('x')
        else:
            y_skip = 1
    else:
        cnt += abs(len(a)-len(b))

    print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
s2 = s.replace('x', '')
if s2 != s2[::-1]:
    print(-1)
else:
    d = deque(s)
    a, b = [], []
    cnt = 0
    x = y = ''
    x_skip = y_skip = 0
    hoge = 0
    while d:
        if not x_skip:
            x = d.popleft()
        if 0 < len(d) and not y_skip:
            y = d.pop()

        if x == y != 'x':
            cnt += abs(len(a)-len(b))
            a, b = [], []
            x_skip = y_skip = 0
            continue

        if x == 'x':
            a.append(x)
        else:
            x_skip = 1

        if y == 'x':
            b.append('x')
        else:
            y_skip = 1
    else:
        cnt += abs(len(a)-len(b))

    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
medidxl = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == (len(t) - 1) // 2:
        medidxl = i
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        #print('OK', i, j)
        i += 1
        j -= 1
    elif s[i] == 'x':
        #print('a', i, j)
        i += 1
        ans += 1
    elif s[j] == 'x':
        #print('b', i, j)
        j -= 1
        ans += 1
ans += medidx - medidxl - 1 if medidx > medidxl else 0
#print(ans)
ans += medidxl - i
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
i = 0
j = len(s) - 1
flag = True
while i < j:
    if s[i] != s[j]:
        flag = False
        break
    i += 1
    j -= 1
if flag:
    print(0)
    exit()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        i += 1
        j -= 1
    elif s[i] == 'x':
        i += 1
        ans += 1
    elif s[j] == 'x':
        j -= 1
        ans += 1
if len(t) % 2 == 0:
    medidx -= 1
ans += medidx - i
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
if len(set(list(s))) == 1:
    print(0)
    exit()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        i += 1
        j -= 1
    elif s[i] == 'x':
        i += 1
        ans += 1
    elif s[j] == 'x':
        j -= 1
        ans += 1
if len(t) % 2 == 0:
    medidx -= 1
ans += medidx - i if i < medidx else 0
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
if len(set(list(s))) == 1:
    print(0)
    exit()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        i += 1
        j -= 1
    elif s[i] == 'x':
        i += 1
        ans += 1
    elif s[j] == 'x':
        j -= 1
        ans += 1
ans += medidx - i
if len(t) % 2 == 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        i += 1
        j -= 1
    elif s[i] == 'x':
        i += 1
        ans += 1
    elif s[j] == 'x':
        j -= 1
        ans += 1
ans += medidx - i
if len(t) % 2 == 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import re 

s = input()
sd = s.replace("x","")

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

# 回文にできるかどうか
if not kaibun(sd):
  print(-1)
else:
  # できそうなら、x以外の文字でsplitして、両端からxの数を合わせていく
  l = [spl.count("x") for spl in re.split('[^x]', s)]
  ll = len(l)
  
  print(sum([max(l[i],l[ll-i-1]) for i in range(ll//2)]))

Submission status: Wrong Answer

Solution code:
s = input()
sd = s.replace("x","")

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

  
import re 
  
if not kaibun(sd):
  print(-1)
else:
  l = [spl.count("x") for spl in re.split('[^x]', s)] 
  ll = len(l)
  print(sum([max(l[i],l[ll-i-1]) for i in range(ll//2)]))

Submission status: Wrong Answer

Solution code:
def main():
    string = input()
    length = len(string)
    string = string.replace("x", "")
    new_length = len(string)
    answer = True
    for i in range(new_length // 2):
        if string[i] != string[new_length - 1 - i]:
            answer = False
            break
    print(length - new_length if answer else -1)


if __name__ == '__main__':
    main()

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
    print(no_x)
    if not no_x:
        print(0)
        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):
        abs(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
    if not no_x:
        print(0)
        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)