Description:
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.
实现代码如下:
1 class Solution { 2 public: 3 bool isAnagram(string s, string t) { 4 unordered_map<char, int> map1; 5 if(s.length() != t.length()) return false; 6 for(int i = 0; i < s.length(); i++) { 7 map1[s[i]]++; 8 } 9 for(int i = 0; i < t.length(); i++) { 10 map1[t[i]]--; 11 } 12 for(auto it = map1.begin(); it!= map1.end(); it++) { 13 if((*it).second != 0) { 14 return false; 15 } 16 } 17 return true; 18 } 19 };