使用c++红黑树map实现 Key:字符 Value:次数:

读入字符串s的每一位,将对应位加一。

读入字符串c的每一位,将对应位减一。

如果发现最后的Value不是0,说明不是“异位词”,输出-1;否则输出字符串长度(异位词最后长度一定相同)。

#include <map>
class Solution {
public:
    int isCongruent(string s, string c) {
        int ans=0;
        map<char, int> m;
        for(char i:s) m[i]++;
        for(char i:c) m[i]--;
        for(auto [i,j]:m) if(j!=0) return -1;
        return s.length();
    }
};