题目描述:
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?
判断两个数是否为字母异位词,直接利用哈希表存储字母出现的次数,再比较即可。
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> v1(26,0);
vector<int> v2(26,0);
if(s.size()!=t.size()) return false;
for(int i=0;i<s.size();i++)
{
v1[s[i]-'a']++;
v2[t[i]-'a']++;
}
for(int i=0;i<26;i++) if(v1[i]!=v2[i]) return false;
return true;
}
};