问题描述:
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例:
输入: s = "anagram", t = "nagaram" 输出: true
输入: s = "rat", t = "car" 输出: false
思路:
使用整型数组,记录每个字母出现的次数。
代码:
class Solution {
public:
bool isAnagram(string s, string t) {
int x[26]={0};
for(auto c:s)
x[c-'a'] +=1;
for(auto c:t)
x[c-'a'] -=1;
for(int i=0;i<26;i++){
if(x[i] != 0)
return false;
}
return true;
}
};
本文介绍了一个使用C++实现的算法,用于判断两个字符串是否为字母异位词。通过统计每个字母出现的次数,该算法能有效地解决这一问题。
764

被折叠的 条评论
为什么被折叠?



