Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
We can use rolling hash and got O(N*K) complexity

lejafilip

Jan 3, 2025

    vector<vector<string>> groupAnagrams(vector<string>& strs) {         std::unordered_map<size_t, std::vector<std::string>> exist;         std::vector<std::vector<std::string>> result;         for(const auto& str : strs)         {             std::vector<int> hist(26,0);             for(const auto& c : str)             {                 hist[c - 'a']++;             }             size_t key = 0;             for(const auto& c : hist)             {                 key *= 31;                 key += c;             }                         exist[key].push_back(str);         }         for(auto& [_, anagrams] : exist)         {             result.emplace_back(std::move(anagrams));         }         return result;     }

0

0

Comments
Comments

On this page