题目:
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.
思路:
判断给定两个字符串是否为相同字母不同排列的单词。最简单的办法就是调用stl的排序函数sort给两个字符串s和t排序,然后比较是否相等即可,复杂度为O(nlogn)。
程序:
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.empty() && t.empty())
return true;
else if (s.empty() || t.empty())
return false;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if (s == t)
return true;
return false;
}
};