题目描述:
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = “anagram”, t = “nagaram”
输出: true
示例 2:
输入: s = “rat”, t = “car”
输出: false
说明:
你可以假设字符串只包含小写字母。
进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xar9lv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解答:
bool isAnagram(char * s, char * t){
int i = 0;
char stest[26] = {0};
char ttest[26] = {0};
int slen = 0;
int tlen = 0;
slen = strlen(s);
tlen = strlen(t);
if(slen != tlen){
return false;
}
for(i = 0;i < slen;i++){//统计字符串对应的字母个数
stest[s[i] - 'a']++;
ttest[t[i] - 'a']++;
}
for(i = 0;i < 26;i++){
if(stest[i] != ttest[i]){
return false;
}
}
return true;
}
运行结果:
Notes:
统计字符串中字母个数,个数相同则表示为字母异位词。