Skip to content

Instantly share code, notes, and snippets.

@varundey
Created August 14, 2017 07:25
Show Gist options
  • Save varundey/c7191098fbf1e50b6ead951068151d42 to your computer and use it in GitHub Desktop.
Save varundey/c7191098fbf1e50b6ead951068151d42 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void solve()
{
string s;
cin >> s;
int n = s.size();
if ( n&1 ) cout << -1 << endl;
else {
int h1[26], h2[26];
for ( int i = 0; i < 26; i++ ) h1[i] = h2[i] = 0;
for ( int i = 0; i < n/2; i++ ) {
h1[s[i]-'a']++;
h2[s[(n/2)+i]-'a']++;
}
int ans = 0;
for ( int i = 0; i < 26; i++ ) ans += abs( h1[i]-h2[i]);
ans >>= 1;
cout << ans << endl;
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int testcase;
cin >> testcase;
for ( int t = 1; t <= testcase; t++ ) {
solve();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment