We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int test(string s) {
if (s.length() % 2 == 1) return -1;
int n = s.length();
map mp;
for (int i = 0; i < n / 2; i++) mp[s[i]]++;
for (int i = n / 2; i < n; i++) if (mp[s[i]] > 0) mp[s[i]]--;
int dem = 0;
for (auto it : mp) dem += it.second;
return dem;
}
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
cout << test(s) << endl;
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Anagram
You are viewing a single comment's thread. Return to all comments →
include
using namespace std;
int test(string s) { if (s.length() % 2 == 1) return -1; int n = s.length(); map mp; for (int i = 0; i < n / 2; i++) mp[s[i]]++; for (int i = n / 2; i < n; i++) if (mp[s[i]] > 0) mp[s[i]]--; int dem = 0; for (auto it : mp) dem += it.second; return dem; }
int main() { int t; cin >> t; while (t--) { string s; cin >> s; cout << test(s) << endl; } return 0; }