这道题标签是eazy,可以排序判断字符串相等,但是只击败了33%的C++编程者,博主自然是不甘心的,于是采用统计词频,并且略施小计,一开始先校验长度,这样就击败了99%。:)
下面是代码,注意到字母Hash表,其它都比较容易了
class Solution { public: bool isAnagram(const string s, const string t) { vector<int> count(26, 0); int size; if(s.size() == t.size()){ size = s.size(); } else{ return false; } const int sizee = size; for(int i = 0; i < sizee; i ++){ count[s[i]-'a'] ++; count[t[i]-'a'] --; } for(int i = 0; i < 26; i ++) if(count[i] != 0) return false; return true; } };
OK,See You Next Chapter!