w = list(input()) na = w.count('a'); nb = len(w) - na if len(w) % 2 == 0 and na % 2 != 0: print(-1); exit(0) rc = lc = 0 moves = 0 for i in range(len(w)//2): if rc == 0: while w[-i-rc-1] == w[-i-1]: rc+=1 if lc == 0: while w[lc] == w[0]: lc+=1 #print(*w,sep='',end='') #print(" rc={} lc={}".format(rc,lc)) if w[i] != w[-i-1]: if rc < lc: w[-i-rc-1]=w[-i-1] w[-i-1]=w[i] moves += rc else: w[i+lc]=w[i] w[i]=w[-i-1] moves += lc rc-=1 lc-=1 #print(*w,sep='',end='') #print(" moves={}".format(moves)) print(moves)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | w = list(input()) na = w.count('a'); nb = len(w) - na if len(w) % 2 == 0 and na % 2 != 0: print(-1); exit(0) rc = lc = 0 moves = 0 for i in range(len(w)//2): if rc == 0: while w[-i-rc-1] == w[-i-1]: rc+=1 if lc == 0: while w[lc] == w[0]: lc+=1 #print(*w,sep='',end='') #print(" rc={} lc={}".format(rc,lc)) if w[i] != w[-i-1]: if rc < lc: w[-i-rc-1]=w[-i-1] w[-i-1]=w[i] moves += rc else: w[i+lc]=w[i] w[i]=w[-i-1] moves += lc rc-=1 lc-=1 #print(*w,sep='',end='') #print(" moves={}".format(moves)) print(moves) |