LeetCode 242:有效的字母异位词

假期动手写的第一题!

解法:采用数字数组计数对消的方法。在题解区叫这种方法为Hash表法,其实仔细想想确实是用一个数组实现的哈希表。 

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length()!=t.length()) return false;

        vector<int> count(26);
        for(int i=0; i<s.length(); ++i) {
            count[s[i]-'a']++;
            count[t[i]-'a']--;
        }

        for(int i=0; i<26; ++i) {
            if (count[i]!=0) return false;
        }
        return true;
    }
};
发布了97 篇原创文章 · 获赞 11 · 访问量 2478

猜你喜欢

转载自blog.csdn.net/chengda321/article/details/104115339