242. 有效的字母异位词
242. 有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram"
输出: true
输出: true
示例 2:
输入: s = "rat", t = "car"
输出: false
输出: false
说明:
你可以假设字符串只包含小写字母。
你可以假设字符串只包含小写字母。
代码:
class Solution { public: bool isAnagram(string s, string t) { if(s.size()!=t.size()) return 0; int hash_s[26] = {0}, hash_t[26] = {0}; for(const auto &c:s) { hash_s[c-'a']++; } for(const auto &c:t) { hash_t[c-'a']++; } for(int i=0; i<26; i++) { if(hash_t[i]!=hash_s[i]) return 0; } return 1; } };
posted on 2020-08-13 16:43 Little-Prince 阅读(64) 评论(0) 编辑 收藏 举报