題目:
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) {
map<char, int> m;
int len1 = s.length(), len2 = t.length();
if (len1 != len2)return false;
for (int i = 0; i < len1; i++) {
m[s[i]] += 1;
m[t[i]] -= 1;
}
for (auto a = m.begin(); a != m.end(); a++) {
if (a->second != 0)return false;
}
return true;
}
};