题目来源【Leetcode】
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.
这道题就用数组来做,先将参照的字符串的每个字母出现的次数记录,再从给的字符串开始一个一个减,如果出现负数,则肯定不是;
class Solution {
public:
bool isAnagram(string s, string t) {
int sl = s.length();
int tl = t.length();
if(sl != tl) return false;
int a[200];
memset(a,0,sizeof(a));
for(int i = 0; i < sl; i++){
a[s[i]]++;
}
for(int i = 0; i < tl; i++){
a[t[i]]--;
if(a[t[i]] < 0) return false;
}
return true;
}
};