Files
leetcode-master/problems/0242.有效的字母异位词.md
youngyangyang04 be23f16a7b Update
2020-07-21 19:44:28 +08:00

869 B
Raw Blame History

题目地址

https://leetcode-cn.com/problems/valid-anagram/

思路

使用哈希法来判断 s 中的字母是否 都在t中且t中的字符也都在s中

代码

class Solution {
public:
    bool isAnagram(string s, string t) {
        int record[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            record[s[i]-'a']++;
        }
        for (int i = 0; i < t.size(); i++) {
            record[t[i]-'a']--;
        }
        for (int i = 0; i < 26; i++) {
            if (record[i] != 0) {
                return false;
            }
        }
        return true;
    }
};

更过算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。