mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-08-06 01:20:05 +08:00
869 B
869 B
题目地址
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」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。