排序法

字符串排序后直接判等即可

python实现

class Solution:
    def isCongruent(self , s: str, c: str) -> int:
        return len(s) if sorted(s) == sorted(c) else -1

c++实现

class Solution {
public:
    int isCongruent(string s, string c) {
        sort(s.begin(), s.end());
        sort(c.begin(), c.end());
        if(s==c){
            return s.size();
        }else{
            return -1;
        }
    }
};

寻找删除法

遍历s,在c中找是否有该字符,如果有,就删掉这个字符。 如果最后c能删干净,则两个字符是字母异位词

c++实现

class Solution {
public:
    int isCongruent(string s, string c) {
        // write code here
        int lens=s.size(), lenc=c.size();
        int position;
        if(lens!=lenc) return -1;
        for(const char ch : s){
            position = c.find(ch);
            if(position == c.npos){
                break;
            }else{
                c.erase(position, 1);
            }
        }
        return c.size()==0 ? s.size() : -1;
    }
};