Description
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input:
s = "anagram", t = "nagaram"
Output:
true
Example 2:
Input:
s = "rat", t = "car"
Output:
false
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
分析
题目的意思是:判断字符串s和t是否是相同字母异序词。
- 这道题我想的是hash表的方式,空间复杂度稍稍高了点;另一种解法是用了一个大小为26的一维数组,统计每个字符的频率就行了。
代码
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.length()!=t.length()){
return false;
}
vector<int> v(26,0);
for(int i=0;i<s.length();i++){
v[s[i]-'a']++;
}
for(int j=0;j<t.length();j++){
v[t[j]-'a']--;
if(v[t[j]-'a']<0){
return false;
}
}
return true;
}
};
参考文献
[LeetCode] Valid Anagram 验证变位词