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 排序s;
2 排序t;
3 比较排序后的s和t,相同->返回true; 不同->返回false;
public class Solution {
public bool IsAnagram(string s, string t) {
//任意为空,返回false
if(s==null||t==null)
return false;
//字符串长度不等,返回false
if(s.Length!=t.Length)
return false;
//排序
List<char> sList = s.ToList();
sList.Sort();
List<char> tList = t.ToList();
tList.Sort();
//逐个对比
for(int i=0;i<sList.Count();i++)
{
if(sList[i]!=tList[i])
return false;
}
return true;
}
}