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:
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
问题分析:通过读题理解题目含义,所要实现的算法就是判断二个字符串是否由相同个数的相同字符所构成。这里想到的一种解法就是先对字符串排序,然后一一匹配相同位置的字符是否一致,若不一致就返回假,理解到这一层,就可写出如下解法:这里用到了迭代器和sort
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()==0&&t.size()==0) return true;
else if(s.size()!=t.size()) return false;
else{
sort(s.begin(),s.end());
sort(t.begin(),t.end());
for(int i=0;i<s.size();i++){
if(s[i]!=t[i])
return false;
}
r