C++ code, defeat 90.35%


  • 0
    Y
    class Solution {
    public:
        inline char* hash(const std::string& str) {
            memset(comp, 1, sizeof(char) * 26);
            for (auto& u : str) {
                ++comp[u - 'a'];
            }
            return comp;
        }
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string, vector<string>> hashMap;
            vector<vector<string>> ret;
            comp[26] = '\0';
            for (auto& u : strs) {
                hashMap[hash(u)].push_back(u);
            }
            for (auto u = hashMap.begin(); u != hashMap.end(); ++u) {
                    ret.push_back(u->second);
            }
            return ret;
        }
        char comp[27];
    };
    

Log in to reply
 

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