Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return 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?
判断t是否可以由s打乱构成。时间复杂度O(n),空间复杂度O(n)
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char, int> cnt;
if (s.length() != t.length()) return false;
for (auto x: s) {
cnt[x]++;
}
for (auto x: t) {
cnt[x]--;
if (cnt[x] < 0) return false;
}
return true;
}
};