Edit 2:
After more testing, I have to agree with the consensus that counting characters in an array is a better choice:
bool is_perm3(std::string const &a, std::string const &b) {
std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s1;
std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s2;
if (a.length() != b.length())
return false;
for (unsigned char c : a)
++s1[c];
for (unsigned char c : b)
++s2[c];
for (std::size_t i=0; i<s1.size(); i++)
if (s1[i] != s2[i])
return false;
return true;
}
I remain puzzled (and frankly, rather bothered) but the poor performance of std::vector
vs. std::array
in his test. We'd expect array
to have less initial overhead, but the vector
was pre-allocated, so speed differences after being set up should be quite minimal (but in this case definitely are not).