c++ - Optimizing very often used anagram function -


i have written function determines whether 2 words anagrams. word anagram of word b if can build word b out of rearranging letters, e.g.:

lead anagram of deal 

this function:

bool is_anagram(std::string const & s1, std::string const & s2) {     auto check = [](std::string const & x)     {         std::map<char, unsigned> counter;         for(auto const & c : x)         {             auto = counter.find(c);             if(it == counter.end())                 counter[c] = 1;             else                 ++counter[c];         }         return counter;     };      return check(s1) == check(s2); } 

this works fine, number of words increases (and function used several million times within application), became major bottleneck of application.

does have idea of how speed function up?

the map creation , call std::map::find within iteration, quite expensive.

in case, can use fact std::string behaves in many regards std::vector<char>, meaning can sort 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 2 maps creating, taking copy of strings (passing them value instead of const reference) , sorting them, so

sort("lead") => "adel" sort("deal") => "adel" 

this change should speed algorithm quite bit. 1 more thing may affect performance if tend compare arbitrary words:

bool is_anagram(std::string s1, std::string s2) {     if(s1.length() != s2.length())         return false;     /* above */ } 

if length of 2 strings differs, cannot anagram. std::string::length() fast operation (it needs store string size anyway), save hustle of o(n*log(n)) sorting 2 strings.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -