The map creation and your call to std::map::find
within the iteration,
is quite expensive.
In this case,
you can use the fact that std::string
behaves in many regards like a
std::vector<char>
, meaning that you can sort it using std::sort
:
bool is_anagram(std::string s1, std::string s2)
{
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
return s1 == s2;
}
Instead of the two maps that you are creating, I am taking a copy of the strings
(passing them by value instead of const reference) and sorting them, so
sort("lead") => "adel"
sort("deal") => "adel"
This change should already speed your algorithm up by quite a bit. One more
thing that may greatly affect the performance if you tend to compare arbitrary
words:
bool is_anagram(std::string s1, std::string s2)
{
if(s1.length() != s2.length())
return false;
/* as above */
}
If the length of the two strings differs, it obviously cannot be an anagram.
std::string::length()
is a very fast operation (it needs to store the
string size anyway), so we save us the hustle of O(N*log(N))
from
sorting the two strings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…