s=input() if len(s)%2==0 and s.count("a")%2==1: print(-1) else: t=[] for i in range(len(s)//2): if s[i]>s[-i-1]: t.append(1) elif s[i]<s[-i-1]: t.append(-1) else: t.append(0) i=0 j=0 r=0 n=len(t) while i<n: #print(i) while i<n and t[i]==0: i+=1 if i==n: break j=i+1 tt=-t[i] while j<n and i<j: if t[j]==0: r+=j-i i+=1 elif t[j]==tt: r+=2*(j-i)-1 i+=2 j+=1 if j==n: break n-=i if len(s)%2==1: r+=(n*(n+1))//2 else: r+=(n*n)//2 print(r)
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 32 33 34 35 36 37 38 39 40 41 42 | s=input() if len(s)%2==0 and s.count("a")%2==1: print(-1) else: t=[] for i in range(len(s)//2): if s[i]>s[-i-1]: t.append(1) elif s[i]<s[-i-1]: t.append(-1) else: t.append(0) i=0 j=0 r=0 n=len(t) while i<n: #print(i) while i<n and t[i]==0: i+=1 if i==n: break j=i+1 tt=-t[i] while j<n and i<j: if t[j]==0: r+=j-i i+=1 elif t[j]==tt: r+=2*(j-i)-1 i+=2 j+=1 if j==n: break n-=i if len(s)%2==1: r+=(n*(n+1))//2 else: r+=(n*n)//2 print(r) |