import sys s=input() if (s.count("a") == 0 or s.count("b") == 0): print(0) sys.exit(0) if (s.count("a") % 2 == 1 and s.count("b") % 2 == 1): print(-1) sys.exit(0) i,j=0,len(s)-1 ev="a" if (s.count("b") % 2 == 0): ev = "b" L=j res=0 while (i<j): while(i < j and s[i]!=ev): i += 1 while(j > i and s[j]!=ev): j -= 1 res += abs(i - (L - j)) i += 1 j -= 1 print(res)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import sys s=input() if (s.count("a") == 0 or s.count("b") == 0): print(0) sys.exit(0) if (s.count("a") % 2 == 1 and s.count("b") % 2 == 1): print(-1) sys.exit(0) i,j=0,len(s)-1 ev="a" if (s.count("b") % 2 == 0): ev = "b" L=j res=0 while (i<j): while(i < j and s[i]!=ev): i += 1 while(j > i and s[j]!=ev): j -= 1 res += abs(i - (L - j)) i += 1 j -= 1 print(res) |