Straightforward 48ms C++ solution with unordered_map


  • 1
    D
    class Solution {
    public:
        vector<string> anagrams(vector<string> &strs) {
            vector<string> result;
            unordered_map<string, vector<string> > hash;
            for (const string& s : strs) {
                string sorted_s(s);
                sort(sorted_s.begin(), sorted_s.end());
                hash[sorted_s].emplace_back(s);
            }
            
            for (const auto& p : hash) {
                if (p.second.size() > 1) {
                    for (const auto& s : p.second) {
                        result.emplace_back(s);
                    }
                }
            }
            return result;
        }
    };

  • 0
    L

    How do you deal with the lexicographical order issue in each list? I didn't see it from the code.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.