leetcode 242. Valid Anagram

这个题只存储26个字母的,之前用的256个字符,所以可以直接用s[i]这种作为坐标,但现在只存储在26个中,坐标值是0到25,必须减去'a'才行,不减的话可能是100多的assic码

class Solution {
public:
    bool isAnagram(string s, string t) {
        int length1 = s.size();
        int length2 = t.size();
        if(length1 <= 0 || length2 <= 0)
            return true;
        vector<int> res(26,0);
        for(int i = 0;i < length1;i++)
            res[s[i] - 'a'] += 1;
        for(int j = 0;j < length2;j++)
            res[t[j] - 'a'] -= 1;
        for(int i =0;i < 26;i++){
            if(res[i] != 0)
                return false;
        }
        return true;
    }
};

https://blog.csdn.net/fly_yr/article/details/49886391

原文地址:https://www.cnblogs.com/ymjyqsx/p/9652683.html