Anagram

#include <bits/stdc++.h>

using namespace std;

int anagaram(string s){
    // Complete this function
    map<char,int> s1;
    map<char,int> s2;
    int len = s.length();
    if(len%2 != 0 ){
        return -1;
    }
    for(int i = 0; i < len ;i++){
        s1[s[i]]=0;
        s2[s[i]]=0;
    }
    int count=0;
    int half = len / 2;//half = 4(0~7,8个元素)
    for(int i = 0 ;i < half;i++){
        s1[s[i]]++;
        s2[s[i + half]]++;
    }
    map<char,int>::iterator it;
    for(it = s1.begin();it!=s1.end();it++)
    {
        int tmp = it->second-s2[it->first];
        if(tmp>=0)
            count += tmp;
    }
    return count;
}

int main() {
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        string s;
        cin >> s;
        int result = anagaram(s);
        cout << result << endl;
    }
    return 0;
}
 
 
原文地址:https://www.cnblogs.com/lyf-sunicey/p/8483344.html